/*
 * Copyright 2009 New Atlanta Communications, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.newatlanta.appengine.datastore;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

/**
 * A utility class that wraps an Iterable<Entity> and treats it as an
 * Iterable<Key>; this is useful when doing keys-only queries to treat
 * the return value as an Iterable<Key>
 * 
 * @author <a href="mailto:vbonfanti@gmail.com">Vince Bonfanti</a>
 */
public class EntityKeyCollection extends AbstractCollection<Key> {
    
    private Iterable<Entity> entityIterable;
   
    public static EntityKeyCollection wrap( Iterable<Entity> entityIterable ) {
        return new EntityKeyCollection( entityIterable );
    }
    
    private EntityKeyCollection( Iterable<Entity> entityIterable ) {
        this.entityIterable = entityIterable;
    }

    @Override
    public Iterator<Key> iterator() {
        return new EntityKeyIterator( entityIterable.iterator() );
    }

    @Override
    public int size() {
        if ( entityIterable instanceof Collection<?> ) {
            return ((Collection<?>)entityIterable).size();
        }
        throw new UnsupportedOperationException();
    }
    
    private class EntityKeyIterator implements Iterator<Key> {
        
        private Iterator<Entity> entityIterator;
        
        private EntityKeyIterator( Iterator<Entity> entityIterator ) {
            this.entityIterator = entityIterator;
        }

        @Override
        public boolean hasNext() {
            return entityIterator.hasNext();
        }

        @Override
        public Key next() {
             Entity entity = entityIterator.next();
             return entity.getKey();
        }

        @Override
        public void remove() {
            entityIterator.remove();
        }
    }
}

@@ -907,24 +907,26 @@
 raps an 
-Iterable
+Collection
 %3CEntity%3E
@@ -950,24 +950,26 @@
  an%0D%0A * 
-Iterable
+Collection
 %3CKey%3E; t
@@ -1047,24 +1047,26 @@
 e as an 
-Iterable
+Collection
 %3CKey%3E%0D%0A 
@@ -1228,24 +1228,26 @@
 private 
-Iterable
+Collection
 %3CEntity%3E
@@ -1248,33 +1248,27 @@
 ntity%3E entit
-yIterable
+ies
 ;%0D%0A   %0D%0A    
@@ -1307,24 +1307,26 @@
 n wrap( 
-Iterable
+Collection
 %3CEntity%3E
@@ -1327,33 +1327,27 @@
 ntity%3E entit
-yIterable
+ies
  ) %7B%0D%0A      
@@ -1385,25 +1385,19 @@
 n( entit
-yIterable
+ies
  );%0D%0A   
@@ -1439,24 +1439,26 @@
 ection( 
-Iterable
+Collection
 %3CEntity%3E
@@ -1464,24 +1464,26 @@
 %3E entity
-Iterable
+Collection
  ) %7B%0D%0A  
@@ -1502,34 +1502,30 @@
 ntit
-yIterable = entityIterable
+ies = entityCollection
 ;%0D%0A 
@@ -1629,25 +1629,19 @@
 r( entit
-yIterable
+ies
 .iterato
@@ -1709,176 +1709,28 @@
     
-if ( entityIterable instanceof Collection%3C?%3E ) %7B%0D%0A            return ((Collection%3C?%3E)entityIterable).size();%0D%0A        %7D%0D%0A        throw new UnsupportedOperationException
+return entities.size
 ();%0D
@@ -1852,36 +1852,32 @@
 tity%3E entityIter
-ator
 ;%0D%0A        %0D%0A   
@@ -1935,20 +1935,16 @@
 tityIter
-ator
  ) %7B%0D%0A  
@@ -1968,20 +1968,16 @@
 tityIter
-ator
  = entit
@@ -1981,20 +1981,16 @@
 tityIter
-ator
 ;%0D%0A     
@@ -2081,20 +2081,16 @@
 tityIter
-ator
 .hasNext
@@ -2159,61 +2159,8 @@
  %7B%0D%0A
-             Entity entity = entityIterator.next();%0D%0A
     
@@ -2181,16 +2181,27 @@
 n entity
+Iter.next()
 .getKey(
@@ -2290,20 +2290,16 @@
 tityIter
-ator
 .remove(

use Collection rather than Iterable (Collection implements Iterable)