Coverage Report - com.allanbank.mongodb.client.SimpleMongoIteratorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleMongoIteratorImpl
100%
20/20
100%
2/2
1.154
 
 1  
 /*
 2  
  * #%L
 3  
  * SimpleMongoIteratorImpl.java - mongodb-async-driver - Allanbank Consulting, Inc.
 4  
  * %%
 5  
  * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
 6  
  * %%
 7  
  * Licensed under the Apache License, Version 2.0 (the "License");
 8  
  * you may not use this file except in compliance with the License.
 9  
  * You may obtain a copy of the License at
 10  
  * 
 11  
  *      http://www.apache.org/licenses/LICENSE-2.0
 12  
  * 
 13  
  * Unless required by applicable law or agreed to in writing, software
 14  
  * distributed under the License is distributed on an "AS IS" BASIS,
 15  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16  
  * See the License for the specific language governing permissions and
 17  
  * limitations under the License.
 18  
  * #L%
 19  
  */
 20  
 package com.allanbank.mongodb.client;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import com.allanbank.mongodb.MongoIterator;
 28  
 import com.allanbank.mongodb.bson.Document;
 29  
 
 30  
 /**
 31  
  * Iterator over the results of fixed collection.
 32  
  * 
 33  
  * @param <T>
 34  
  *            The type being iterated.
 35  
  * 
 36  
  * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
 37  
  *         mutated in incompatible ways between any two releases of the driver.
 38  
  * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
 39  
  */
 40  
 public class SimpleMongoIteratorImpl<T> implements MongoIterator<T> {
 41  
 
 42  
     /** The wrapped iterator. */
 43  
     private final Iterator<T> myIterator;
 44  
 
 45  
     /**
 46  
      * Create a new SimpleMongoIteratorImpl.
 47  
      * 
 48  
      * @param wrapped
 49  
      *            The collection to turn into a {@link MongoIterator}.
 50  
      */
 51  28
     public SimpleMongoIteratorImpl(final Collection<T> wrapped) {
 52  28
         myIterator = wrapped.iterator();
 53  28
     }
 54  
 
 55  
     /**
 56  
      * {@inheritDoc}
 57  
      * <p>
 58  
      * Overridden to return the null as this cursor cannot be restarted.
 59  
      * </p>
 60  
      */
 61  
     @Override
 62  
     public Document asDocument() {
 63  1
         return null;
 64  
     }
 65  
 
 66  
     /**
 67  
      * {@inheritDoc}
 68  
      * <p>
 69  
      * Overridden to do nothing.
 70  
      * </p>
 71  
      */
 72  
     @Override
 73  
     public void close() {
 74  
         // Nothing.
 75  11
     }
 76  
 
 77  
     /**
 78  
      * {@inheritDoc}
 79  
      * <p>
 80  
      * Overridden to return -1.
 81  
      * </p>
 82  
      */
 83  
     @Override
 84  
     public int getBatchSize() {
 85  2
         return -1;
 86  
     }
 87  
 
 88  
     /**
 89  
      * {@inheritDoc}
 90  
      * <p>
 91  
      * Overridden to return true if there are more documents.
 92  
      * </p>
 93  
      */
 94  
     @Override
 95  
     public boolean hasNext() {
 96  40
         return myIterator.hasNext();
 97  
     }
 98  
 
 99  
     /**
 100  
      * {@inheritDoc}
 101  
      * <p>
 102  
      * Overridden to return this iterator.
 103  
      * </p>
 104  
      */
 105  
     @Override
 106  
     public Iterator<T> iterator() {
 107  4
         return this;
 108  
     }
 109  
 
 110  
     /**
 111  
      * {@inheritDoc}
 112  
      * <p>
 113  
      * Overridden to return the next document from the query.
 114  
      * </p>
 115  
      * 
 116  
      * @see java.util.Iterator#next()
 117  
      */
 118  
     @Override
 119  
     public T next() {
 120  18
         return myIterator.next();
 121  
     }
 122  
 
 123  
     /**
 124  
      * {@inheritDoc}
 125  
      * <p>
 126  
      * Overridden to throw an {@link UnsupportedOperationException}.
 127  
      * </p>
 128  
      * 
 129  
      * @see java.util.Iterator#remove()
 130  
      */
 131  
     @Override
 132  
     public void remove() {
 133  1
         throw new UnsupportedOperationException(
 134  
                 "Cannot remove a document via a MongoDB iterator.");
 135  
     }
 136  
 
 137  
     /**
 138  
      * {@inheritDoc}
 139  
      * <p>
 140  
      * Overridden to do nothing as this iterator only supports single batch
 141  
      * results.
 142  
      * </p>
 143  
      */
 144  
     @Override
 145  
     public void setBatchSize(final int batchSize) {
 146  
         // Nothing.
 147  1
     }
 148  
 
 149  
     /**
 150  
      * {@inheritDoc}
 151  
      * <p>
 152  
      * Overridden to do nothing as this iterator only supports single batch
 153  
      * results.
 154  
      * </p>
 155  
      */
 156  
     @Override
 157  
     public void stop() {
 158  
         // Nothing.
 159  1
     }
 160  
 
 161  
     /**
 162  
      * {@inheritDoc}
 163  
      * <p>
 164  
      * Overridden to return the remaining elements as a array.
 165  
      * </p>
 166  
      */
 167  
     @Override
 168  
     public Object[] toArray() {
 169  1
         final List<T> remaining = toList();
 170  
 
 171  1
         return remaining.toArray();
 172  
     }
 173  
 
 174  
     /**
 175  
      * {@inheritDoc}
 176  
      * <p>
 177  
      * Overridden to return the remaining elements as a array.
 178  
      * </p>
 179  
      */
 180  
     @Override
 181  
     public <S> S[] toArray(final S[] to) {
 182  1
         final List<T> remaining = toList();
 183  
 
 184  1
         return remaining.toArray(to);
 185  
     }
 186  
 
 187  
     /**
 188  
      * {@inheritDoc}
 189  
      * <p>
 190  
      * Overridden to return the remaining elements as a list.
 191  
      * </p>
 192  
      */
 193  
     @Override
 194  
     public List<T> toList() {
 195  17
         final List<T> remaining = new ArrayList<T>();
 196  
 
 197  31
         while (hasNext()) {
 198  14
             remaining.add(next());
 199  
         }
 200  
 
 201  17
         return remaining;
 202  
     }
 203  
 }