All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclouds.collect.AdvanceUntilEmptyIterable Maven / Gradle / Ivy

/**
 * Licensed to jclouds, Inc. (jclouds) under one or more
 * contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  jclouds licenses this file
 * to you 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 org.jclouds.collect;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Iterator;

import com.google.common.annotations.Beta;
import com.google.common.base.Objects;
import com.google.common.base.Supplier;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;

/**
 * continues to supply iterables until the last was empty
 * 
 * @param 
 */
@Beta
public class AdvanceUntilEmptyIterable extends FluentIterable> {

   public static  AdvanceUntilEmptyIterable create(Supplier> nextIterable){
      return new AdvanceUntilEmptyIterable(nextIterable);
   }
   
   private final AdvanceUntilEmptyIterator iterator;

   protected AdvanceUntilEmptyIterable(Supplier> nextIterable) {
      this.iterator = new AdvanceUntilEmptyIterator(checkNotNull(nextIterable, "next iterable"));
   }

   @Override
   public Iterator> iterator() {
      return iterator;
   }

   private static class AdvanceUntilEmptyIterator extends AbstractIterator> {

      private final Supplier> nextIterable;
      private transient FluentIterable current;
      private transient boolean unread = true;

      private AdvanceUntilEmptyIterator(Supplier> nextIterable) {
         this.nextIterable = checkNotNull(nextIterable, "next iterable");
      }

      /**
       * {@inheritDoc}
       */
      @Override
      protected FluentIterable computeNext() {
         if (unread)
            try {
               return current = nextIterable.get();
            } finally {
               unread = false;
            }
         else if (current.size() > 0)
            return current = nextIterable.get();
         else
            return endOfData();
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public int hashCode() {
         return Objects.hashCode(current, unread);
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public boolean equals(Object obj) {
         if (this == obj)
            return true;
         if (obj == null || getClass() != obj.getClass())
            return false;
         AdvanceUntilEmptyIterator other = AdvanceUntilEmptyIterator.class.cast(obj);
         return Objects.equal(this.current, other.current) && Objects.equal(this.unread, other.unread);
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public String toString() {
         return Objects.toStringHelper("").omitNullValues().add("current", current).add("unread", unread).toString();
      }
   }
   
   /**
    * Combines all the pages into a single unmodifiable iterable. ex.
    * 
    * 
    * FluentIterable blobs = blobstore.list(...).concat();
    * for (StorageMetadata blob : blobs) {
    *     process(blob);
    * }
    * 
* * @see Iterators#concat */ public FluentIterable concat() { final Iterator> iterator = iterator(); final UnmodifiableIterator> unmodifiable = new UnmodifiableIterator>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override public Iterator next() { return iterator.next().iterator(); } }; return new FluentIterable() { @Override public Iterator iterator() { return Iterators.concat(unmodifiable); } }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy