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

org.jclouds.googlecloud.internal.ListPages Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.googlecloud.internal;

import static shaded.com.google.common.base.Predicates.instanceOf;
import static shaded.com.google.common.collect.Iterables.tryFind;

import java.util.Iterator;
import java.util.List;

import org.jclouds.googlecloud.domain.ListPage;
import org.jclouds.googlecloud.options.ListOptions;
import org.jclouds.javax.annotation.Nullable;

import shaded.com.google.common.collect.AbstractIterator;
import shaded.com.google.common.collect.ImmutableList;
import shaded.com.google.common.collect.Iterators;

public final class ListPages {

   public static  Iterable concat(final Iterator> input) {
      return new Iterable() {
         @Override public Iterator iterator() {
            return Iterators.concat(new AbstractIterator>() {
               @Override protected Iterator computeNext() {
                  return input.hasNext() ? input.next().iterator() : endOfData();
               }
            });
         }
      };
   }

   /** Value of {@code } is a final class in the cloud provider. Rather than playing with reflection, we trust it. */
   @Nullable static  O listOptions(List args) {
      return (O) tryFind(args, instanceOf(ListOptions.class)).orNull();
   }

   static  Iterator> singletonOrEmptyIterator(ListPage input) {
      return input.isEmpty() ? ImmutableList.>of().iterator() : Iterators.singletonIterator(input);
   }

   private ListPages() {
   }
}