Java Code Examples for java.util.Iterator#forEachRemaining()

The following examples show how to use java.util.Iterator#forEachRemaining() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InMemorySourceVertex.java    From incubator-nemo with Apache License 2.0 6 votes vote down vote up
@Override
public List<Readable<T>> getReadables(final int desiredNumOfSplits) throws Exception {

  final List<Readable<T>> readables = new ArrayList<>();
  final long sliceSize = initializedSourceData.spliterator().getExactSizeIfKnown() / desiredNumOfSplits;
  final Iterator<T> iterator = initializedSourceData.iterator();

  for (int i = 0; i < desiredNumOfSplits; i++) {
    final List<T> dataForReader = new ArrayList<>();

    if (i == desiredNumOfSplits - 1) { // final iteration
      iterator.forEachRemaining(dataForReader::add);
    } else {
      for (int j = 0; j < sliceSize && iterator.hasNext(); j++) {
        dataForReader.add(iterator.next());
      }
    }

    readables.add(new InMemorySourceReadable<>(dataForReader));
  }
  return readables;
}
 
Example 2
Source File: CommandStatusReportPublisher.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
protected CommandStatusReportsProto getReport() {
  Map<Long, CommandStatus> map = this.getContext()
      .getCommandStatusMap();
  Iterator<Long> iterator = map.keySet().iterator();
  CommandStatusReportsProto.Builder builder = CommandStatusReportsProto
      .newBuilder();

  iterator.forEachRemaining(key -> {
    CommandStatus cmdStatus = map.get(key);
    // If status is still pending then don't remove it from map as
    // CommandHandler will change its status when it works on this command.
    if (!cmdStatus.getStatus().equals(Status.PENDING)) {
      builder.addCmdStatus(cmdStatus.getProtoBufMessage());
      map.remove(key);
    }
  });
  return builder.getCmdStatusCount() > 0 ? builder.build() : null;
}
 
Example 3
Source File: DeploymentFailsForFirestoreNativeIT.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 6 votes vote down vote up
@After
public void destroyBucket() throws IOException {
  Storage storage = StorageOptions.newBuilder()
      .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(firestoreNativeConfiguration)))
      .build().getService();
  log.debug("Deleting files from " + bucketName);
  // must delete all the files within the bucket before we can delete the bucket
  Iterator<Blob> list = storage.list(bucketName,
      Storage.BlobListOption.prefix("")).iterateAll()
      .iterator();
  list.forEachRemaining(blob -> blob.delete());

  storage.delete(bucketName);

  log.info(bucketName + "bucket deleted");
}
 
Example 4
Source File: SingletonIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void testForEachRemaining() {
    Iterator<String> it = Collections.singleton("TheOne").iterator();
    AtomicInteger cnt = new AtomicInteger(0);

    it.forEachRemaining(s -> {
        assertEquals("TheOne", s);
        cnt.incrementAndGet();
    });

    assertEquals(cnt.get(), 1);
    assertFalse(it.hasNext());

    try {
        String str = it.next();
        fail("Should throw NoSuchElementException at end");
    } catch (NoSuchElementException ex) {
        // ignore;
    }
}
 
Example 5
Source File: SingletonIterator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void testForEachRemaining() {
    Iterator<String> it = Collections.singleton("TheOne").iterator();
    AtomicInteger cnt = new AtomicInteger(0);

    it.forEachRemaining(s -> {
        assertEquals("TheOne", s);
        cnt.incrementAndGet();
    });

    assertEquals(cnt.get(), 1);
    assertFalse(it.hasNext());

    try {
        String str = it.next();
        fail("Should throw NoSuchElementException at end");
    } catch (NoSuchElementException ex) {
        // ignore;
    }
}
 
Example 6
Source File: ServiceLoaderUtils.java    From oxygen with Apache License 2.0 6 votes vote down vote up
/**
 * 加载服务列表
 *
 * @param clazz 类
 * @param loader 类加载器
 * @param <T> 泛型
 * @return services
 */
public <T> List<T> loadServices(Class<T> clazz, ClassLoader loader) {
  List<T> list = new ArrayList<>();
  ServiceLoader<T> serviceLoader;
  if (loader != null) {
    serviceLoader = ServiceLoader.load(clazz, loader);
  } else {
    serviceLoader = ServiceLoader.load(clazz);
  }
  Iterator<T> it = serviceLoader.iterator();
  if (it.hasNext()) {
    it.forEachRemaining(list::add);
    return list;
  }
  if (loader == null) {
    // for tccl
    serviceLoader = ServiceLoader.load(clazz, ServiceLoaderUtils.class.getClassLoader());
    it = serviceLoader.iterator();
    if (it.hasNext()) {
      it.forEachRemaining(list::add);
      return list;
    }
  }
  return list;
}
 
Example 7
Source File: SPARQLServiceConverter.java    From hypergraphql with Apache License 2.0 6 votes vote down vote up
private String getSelectRoot_GET_BY_ID(JsonNode queryField) {

        Iterator<JsonNode> urisIter = queryField.get(ARGS).get(URIS).elements();

        Set<String> uris = new HashSet<>();

        urisIter.forEachRemaining(uri -> uris.add(uri.asText()));

        String targetName = queryField.get(TARGET_NAME).asText();
        String targetURI = schema.getTypes().get(targetName).getId();
        String graphID = ((SPARQLEndpointService) schema.getQueryFields().get(queryField.get(NAME).asText()).service()).getGraph();
        String nodeId = queryField.get(NODE_ID).asText();
        String selectTriple = toTriple(toVar(nodeId), RDF_TYPE_URI, uriToResource(targetURI));
        String valueSTR = valuesClause(nodeId, uris);
        String filterSTR = filterClause(nodeId, uris);

        JsonNode subfields = queryField.get(FIELDS);
        String subQuery = getSubQueries(subfields);

        return selectQueryClause(valueSTR + selectTriple + subQuery, graphID);
    }
 
Example 8
Source File: SingletonIterator.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testForEachRemaining() {
    Iterator<String> it = Collections.singleton("TheOne").iterator();
    AtomicInteger cnt = new AtomicInteger(0);

    it.forEachRemaining(s -> {
        assertEquals("TheOne", s);
        cnt.incrementAndGet();
    });

    assertEquals(cnt.get(), 1);
    assertFalse(it.hasNext());

    try {
        String str = it.next();
        fail("Should throw NoSuchElementException at end");
    } catch (NoSuchElementException ex) {
        // ignore;
    }
}
 
Example 9
Source File: TheRequestApocalypse.java    From amodeus with GNU General Public License v2.0 5 votes vote down vote up
public TheRequestApocalypse toNoMoreThan(int maxRequests, long seed) {
    final long legCount = LegCount.of(population, AmodeusModeConfig.DEFAULT_MODE);
    GlobalAssert.that(maxRequests <= legCount);
    if (legCount == maxRequests)
        return this;

    /** shuffle list of {@link Person}s */
    List<Person> list = new ArrayList<>(population.getPersons().values());
    Collections.shuffle(list, new Random(seed));
    Iterator<Person> iterator = list.iterator();

    // skip all persons that should completely remain in the population
    Person person = iterator.next();
    int totReq = 0;
    long req = LegCount.of(person, AmodeusModeConfig.DEFAULT_MODE);
    while (totReq + req <= maxRequests) {
        totReq += req;
        person = iterator.next();
        req = LegCount.of(person, AmodeusModeConfig.DEFAULT_MODE);
    }

    // create new person if needed to fill requests
    int split = maxRequests - totReq;
    if (split != 0) {
        Person splitPerson = SplitUp.of(population, person, split, AmodeusModeConfig.DEFAULT_MODE);
        req = LegCount.of(splitPerson, AmodeusModeConfig.DEFAULT_MODE);
        totReq += req;
        GlobalAssert.that(totReq == maxRequests);
        population.addPerson(splitPerson);
    }

    // remove all remaining persons
    iterator.forEachRemaining(p -> population.removePerson(p.getId()));
    population.removePerson(person.getId());
    GlobalAssert.that(LegCount.of(population, AmodeusModeConfig.DEFAULT_MODE) == maxRequests);
    return this;
}
 
Example 10
Source File: HGraphQLConverter.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
private String getSubQuery(JsonNode fieldsJson, String parentType) {

        Set<String> subQueryStrings = new HashSet<>();

        if (schema.getTypes().containsKey(parentType)) {
            subQueryStrings.add("_id");
            subQueryStrings.add("_type");
        }

        if (fieldsJson==null || fieldsJson.isNull()) {
            if (subQueryStrings.isEmpty()) {
                return "";
            } else {
                return querySTR(String.join(" ", subQueryStrings));
            }
        } else {

            Iterator<JsonNode> fields = fieldsJson.elements();

            fields.forEachRemaining(field -> {
                ArrayNode fieldsArray = (field.get("fields").isNull()) ? null : (ArrayNode) field.get("fields");
                String arg = (field.get("args").isNull()) ? "" : langSTR((ObjectNode) field.get("args"));
                String fieldString = field.get("name").asText() + arg + " " + getSubQuery(fieldsArray, field.get("targetName").asText());
                subQueryStrings.add(fieldString);
            });
        }

        if (!subQueryStrings.isEmpty()) {
            return querySTR(String.join(" ", subQueryStrings));
        } else {
            return "";
        }
    }
 
Example 11
Source File: MovieResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/rating/{rating}")
@Produces("application/json")
public List<Movie> findByRating(@PathParam("rating") String rating) {
    Iterator<Movie> byRating = movieRepository.findByRating(rating);
    List<Movie> result = new ArrayList<>();
    byRating.forEachRemaining(result::add);
    return result;
}
 
Example 12
Source File: AbstractAggregatingMetricsHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts and aggregates all requested metrics from the given metric stores, and maps the result to a JSON string.
 *
 * @param stores available metrics
 * @param requestedMetrics ids of requested metrics
 * @param requestedAggregationsFactories requested aggregations
 * @return JSON string containing the requested metrics
 */
private AggregatedMetricsResponseBody getAggregatedMetricValues(
		Collection<? extends MetricStore.ComponentMetricStore> stores,
		List<String> requestedMetrics,
		MetricAccumulatorFactory requestedAggregationsFactories) {

	Collection<AggregatedMetric> aggregatedMetrics = new ArrayList<>(requestedMetrics.size());
	for (String requestedMetric : requestedMetrics) {
		final Collection<Double> values = new ArrayList<>(stores.size());
		try {
			for (MetricStore.ComponentMetricStore store : stores) {
				String stringValue = store.metrics.get(requestedMetric);
				if (stringValue != null) {
					values.add(Double.valueOf(stringValue));
				}
			}
		} catch (NumberFormatException nfe) {
			log.warn("The metric {} is not numeric and can't be aggregated.", requestedMetric, nfe);
			// metric is not numeric so we can't perform aggregations => ignore it
			continue;
		}
		if (!values.isEmpty()) {

			Iterator<Double> valuesIterator = values.iterator();
			MetricAccumulator acc = requestedAggregationsFactories.get(requestedMetric, valuesIterator.next());
			valuesIterator.forEachRemaining(acc::add);

			aggregatedMetrics.add(acc.get());
		} else {
			return new AggregatedMetricsResponseBody(Collections.emptyList());
		}
	}
	return new AggregatedMetricsResponseBody(aggregatedMetrics);
}
 
Example 13
Source File: Version1to2.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
private JSONObject getMappedColumnData(final JSONObject nonMappedJson, final Map<String, String> columnMap) {
    final JSONObject mappedJson = new JSONObject();
    Iterator<String> keys = nonMappedJson.keys();
    keys.forEachRemaining(key -> {
        if (columnMap.containsKey(key)) {
            mappedJson.put("" + columnMap.get(key), nonMappedJson.get(key));
        }
    });

    return mappedJson;
}
 
Example 14
Source File: JettyHeadersAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Map<String, String> toSingleValueMap() {
	Map<String, String> singleValueMap = new LinkedHashMap<>(this.headers.size());
	Iterator<HttpField> iterator = this.headers.iterator();
	iterator.forEachRemaining(field -> {
		if (!singleValueMap.containsKey(field.getName())) {
			singleValueMap.put(field.getName(), field.getValue());
		}
	});
	return singleValueMap;
}
 
Example 15
Source File: GenericAccumulatorFactory.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void prepareFinal()
{
    pagesIndex.sort(orderByChannels, orderings);
    Iterator<Page> pagesIterator = pagesIndex.getSortedPages();
    pagesIterator.forEachRemaining(page -> {
        // The last channel of the page is the group id
        GroupByIdBlock groupIds = new GroupByIdBlock(groupCount, page.getBlock(page.getChannelCount() - 1));
        // We pass group id together with the other input channels to accumulator. Accumulator knows which input channels
        // to use. Since we did not change the order of original input channels, passing the group id is safe.
        accumulator.addInput(groupIds, page);
    });
}
 
Example 16
Source File: IteratorDefaults.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testForEach() throws Exception {
    final Integer[] data = new Integer[1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] iterableCollectionClasses = {
            "java.util.ArrayDeque",
            "java.util.ArrayList",
            "java.util.HashSet",
            "java.util.LinkedHashSet",
            "java.util.LinkedList",
            "java.util.PriorityQueue",
            "java.util.TreeSet",
            "java.util.Vector",
            "java.util.concurrent.ConcurrentLinkedDeque",
            "java.util.concurrent.ConcurrentLinkedQueue",
            "java.util.concurrent.ConcurrentSkipListSet",
            "java.util.concurrent.CopyOnWriteArrayList",
            "java.util.concurrent.CopyOnWriteArraySet",
            "java.util.concurrent.LinkedBlockingDeque",
            "java.util.concurrent.LinkedBlockingQueue",
            "java.util.concurrent.LinkedTransferQueue",
            "java.util.concurrent.PriorityBlockingQueue"
    };

    for (final String iterableClass : iterableCollectionClasses) {
        final Iterable<Integer> iterable =
                (Iterable<Integer>) Class.forName(iterableClass).newInstance();
        ((Collection<Integer>) iterable).addAll(source);
        final Iterator<Integer> iterator = iterable.iterator();
        final List<Integer> target = new ArrayList<>(source.size());
        iterator.forEachRemaining(target::add);
        if ("java.util.HashSet".equals(iterableClass)) {
            target.sort((x, y) -> x - y);
            assertEquals(target, source);
        } else {
            assertEquals(target, source);
        }

        // verify that for an iterator that has been advanced via next(),
        // forEach starts from the current location, not zero
        final int OFFSET = 5;
        final List<Integer> reference2 = new ArrayList<>(source).subList(OFFSET, source.size());
        final List<Integer> removed2 = new ArrayList<>(OFFSET);
        final Iterator<Integer> iterator2 = iterable.iterator();
        for (int i=0; i < OFFSET; i++) {
            // advance the iterator by OFFSET, saving iterated elements
            removed2.add(iterator2.next());
        }
        final List<Integer> target2 = new ArrayList<>(reference2.size());
        iterator2.forEachRemaining(target2::add);
        if ("java.util.HashSet".equals(iterableClass)) {
            assertEquals(target2.size(), reference2.size());
            target2.addAll(removed2);
            target2.sort((x, y) -> x - y);
            assertEquals(target2, source);
            assertEquals(target2.subList(OFFSET, source.size()), reference2);
        } else {
            assertEquals(target2, reference2);
        }
    }
}
 
Example 17
Source File: AbstractHttpHeadersTest.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@SafeVarargs
private static <T> void assertIteratorIs(final Iterator<? extends T> iterator, final T... elements) {
    final List<T> list = new ArrayList<>();
    iterator.forEachRemaining(list::add);
    assertThat(list, is(asList(elements)));
}
 
Example 18
Source File: Iterators.java    From gadtry with Apache License 2.0 4 votes vote down vote up
public static <T> void foreach(Iterator<T> iterator, Consumer<T> function)
{
    requireNonNull(iterator);
    iterator.forEachRemaining(function);
}
 
Example 19
Source File: ContainedComponents2ContainedElementsTransformer.java    From recheck with GNU Affero General Public License v3.0 4 votes vote down vote up
private Collection<Attribute> toList( final Iterator<Attribute> attributes ) {
	final List<Attribute> list = new ArrayList<>();
	attributes.forEachRemaining( list::add );
	return list;
}
 
Example 20
Source File: RawKVClient.java    From client-java with Apache License 2.0 3 votes vote down vote up
/**
 * Scan raw key-value pairs from TiKV in range [startKey, endKey)
 *
 * @param startKey raw start key, inclusive
 * @param endKey raw end key, exclusive
 * @param limit limit of key-value pairs scanned, should be less than {@link #MAX_RAW_SCAN_LIMIT}
 * @return list of key-value pairs in range
 */
public List<Kvrpcpb.KvPair> scan(ByteString startKey, ByteString endKey, int limit) {
  Iterator<Kvrpcpb.KvPair> iterator =
      rawScanIterator(conf, clientBuilder, startKey, endKey, limit);
  List<Kvrpcpb.KvPair> result = new ArrayList<>();
  iterator.forEachRemaining(result::add);
  return result;
}