Java Code Examples for com.google.common.collect.Iterators#cycle()

The following examples show how to use com.google.common.collect.Iterators#cycle() . 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: SampleTweet.java    From trident-tutorial with Apache License 2.0 6 votes vote down vote up
public SampleTweet() throws IOException {
    ObjectMapper om = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    ImmutableList.Builder<String> b = ImmutableList.builder();

    InputStreamReader reader = new InputStreamReader(this.getClass().getResourceAsStream("sample_tweet.json"));
    try {
        String tweetArray = CharStreams.toString(reader);
        ArrayNode parsed = (ArrayNode)om.readTree(tweetArray);
        for (JsonNode tweet : parsed) {
            StringWriter sw = new StringWriter();
            om.writeTree(factory.createGenerator(sw), tweet);
            b.add(sw.toString());
        }
        sampleTweet = Iterators.cycle(b.build());
    } finally {
        reader.close();
    }
}
 
Example 2
Source File: RatioMessageBuffer.java    From storm-dynamic-spout with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void recalculateOrdering() {
    // clear ordering
    order.clear();

    // Create new ordering, probably a better way to do this...
    for (Map.Entry<VirtualSpoutIdentifier, Boolean> entry : allIds.entrySet()) {
        // If throttled
        if (entry.getValue()) {
            order.add(entry.getKey());
        } else {
            // Add entries at ratio
            for (int x = 0; x < ratio; x++) {
                order.add(entry.getKey());
            }
        }
    }

    // create new iterator that cycles endlessly
    virtualSpoutIdentifierIterator = Iterators.cycle(order);
}
 
Example 3
Source File: TestAssignment.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void manyFiles() throws Exception {
  List<CompleteWork> chunks = generateChunks(1000);

  Iterator<NodeEndpoint> incomingEndpointsIterator = Iterators.cycle(endpoints);

  List<NodeEndpoint> incomingEndpoints = Lists.newArrayList();

  final int width = 28 * 30;
  for (int i = 0; i < width; i++) {
    incomingEndpoints.add(incomingEndpointsIterator.next());
  }

  ListMultimap<Integer, CompleteWork> mappings = AssignmentCreator.getMappings(incomingEndpoints, chunks);
  System.out.println(mappings.keySet().size());
  for (int i = 0; i < width; i++) {
    Assert.assertTrue("no mapping for entry " + i, mappings.get(i) != null && mappings.get(i).size() > 0);
  }
}
 
Example 4
Source File: SwtBarChart.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void setSelection(@NonNull Set<@NonNull Object> set) {
    super.setSelection(set);

    /* Set color of selected symbol */
    Iterator<Color> colorsIt = Iterators.cycle(COLORS);
    Iterator<Color> lightColorsIt = Iterators.cycle(COLORS_LIGHT);

    for (ISeries series : getChart().getSeriesSet().getSeries()) {
        /* Series color */
        Color lightColor = NonNullUtils.checkNotNull(lightColorsIt.next());
        Color color = NonNullUtils.checkNotNull(colorsIt.next());

        if (set.isEmpty()) {
            /* Put all symbols to the normal colors */
            ((IBarSeries) series).setBarColor(color);
        } else {
            /*
             * Fill with light colors to represent the deselected state. The
             * paint listener is then responsible for drawing the cross and
             * the dark colors for the selection.
             */
            ((IBarSeries) series).setBarColor(lightColor);
        }
    }
}
 
Example 5
Source File: BenchmarkNodeScheduler.java    From presto with Apache License 2.0 5 votes vote down vote up
@Benchmark
@OperationsPerInvocation(SPLITS)
public Object benchmark(BenchmarkData data)
{
    List<RemoteTask> remoteTasks = ImmutableList.copyOf(data.getTaskMap().values());
    Iterator<MockRemoteTaskFactory.MockRemoteTask> finishingTask = Iterators.cycle(data.getTaskMap().values());
    Iterator<Split> splits = data.getSplits().iterator();
    Set<Split> batch = new HashSet<>();
    while (splits.hasNext() || !batch.isEmpty()) {
        Multimap<InternalNode, Split> assignments = data.getNodeSelector().computeAssignments(batch, remoteTasks).getAssignments();
        for (InternalNode node : assignments.keySet()) {
            MockRemoteTaskFactory.MockRemoteTask remoteTask = data.getTaskMap().get(node);
            remoteTask.addSplits(ImmutableMultimap.<PlanNodeId, Split>builder()
                    .putAll(new PlanNodeId("sourceId"), assignments.get(node))
                    .build());
            remoteTask.startSplits(MAX_SPLITS_PER_NODE);
        }
        if (assignments.size() == batch.size()) {
            batch.clear();
        }
        else {
            batch.removeAll(assignments.values());
        }
        while (batch.size() < SPLIT_BATCH_SIZE && splits.hasNext()) {
            batch.add(splits.next());
        }
        finishingTask.next().finishSplits((int) Math.ceil(MAX_SPLITS_PER_NODE / 50.0));
    }

    return remoteTasks;
}
 
Example 6
Source File: _group.java    From mesh with MIT License 5 votes vote down vote up
public static MapValue invoke(final ListValue keys, final ListValue vals)
{
    if (vals.size() == 0)
        return PersistentMap.EMPTY;

    final PersistentMap result = PersistentMap.fresh();

    final Iterator<?> keyiter = keys.size() >= vals.size() ?
        keys.iterator() : Iterators.cycle(keys);

    for (final Object val : vals)
    {
        final Object key = keyiter.next();

        final PersistentList keyvals = (PersistentList)result.get(key);

        if (keyvals == null)
        {
            result.assocUnsafe(key, PersistentList.single(val));
        }
        else
        {
            result.assocUnsafe(key, keyvals.appendUnsafe(val));
        }
    }

    return result;
}
 
Example 7
Source File: StressTestClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
WeightedTestCaseSelector(List<TestCaseWeightPair> testCaseWeightPairs) {
  Preconditions.checkNotNull(testCaseWeightPairs, "testCaseWeightPairs");
  Preconditions.checkArgument(testCaseWeightPairs.size() > 0);

  List<TestCases> testCases = new ArrayList<>();
  for (TestCaseWeightPair testCaseWeightPair : testCaseWeightPairs) {
    for (int i = 0; i < testCaseWeightPair.weight; i++) {
      testCases.add(testCaseWeightPair.testCase);
    }
  }

  shuffle(testCases);

  this.testCases = Iterators.cycle(testCases);
}
 
Example 8
Source File: Level.java    From Quiz with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns next unsolved exercise in cycle for specified
 * id.
 * 
 * @param id
 *            previous exercise id
 * @param forward
 *            search forward if true, backward if false
 * @return next unsolved exercise
 */
public Exercise getUnsolvedInCycle(int id, boolean forward) {
	if (getExercises() == null || getExercises().isEmpty()) {
		return null;
	}
	List<Exercise> exercises = null;
	if (forward) {
		exercises = new ArrayList<Exercise>(getExercises());
	} else {
		exercises = Lists.reverse(new ArrayList<Exercise>(getExercises()));
	}
	Iterator<Exercise> ie = Iterators.cycle(exercises);
	int count = 0;
	boolean isAfterSpecified = false;
	// Iterate over cycle.
	while (count <= exercises.size() + id) {
		Exercise e = ie.next();
		/* Search for next unsolved exercise only if
		 * specified by method parameter was reached
		 * or if parameter equals 0.
		 */
		if ((isAfterSpecified || id == 0) && !e.isSolved()) {
			return e;
		}
		/* Exercise specified by method parameter is 
		 * reached. Now next exercise can be searched. 
		 */
		if (id == e.getId()) {
			isAfterSpecified = true;
		}
		count++;
	}
	return null;
}
 
Example 9
Source File: HttpHosts.java    From hbc with Apache License 2.0 5 votes vote down vote up
/**
 * All httpHosts must start with the http scheme("http:// or https://")
 */
public HttpHosts(Iterable<String> addresses) {
  Preconditions.checkNotNull(addresses);
  Preconditions.checkArgument(!Iterables.isEmpty(addresses));
  for (String address : addresses) {
    if (!address.toLowerCase().startsWith(HttpConstants.HTTP_SCHEME + "://") &&
        !address.toLowerCase().startsWith(HttpConstants.HTTPS_SCHEME + "://")) {
      throw new IllegalArgumentException("Address doesn't have an http scheme: " + address);
    }
  }
  List<String> copy = Lists.newArrayList(addresses);
  Collections.shuffle(copy);
  this.hosts = Iterators.cycle(copy);
}
 
Example 10
Source File: SwtBarChart.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void paintControl(@Nullable PaintEvent event) {
    if (event == null) {
        return;
    }

    /* Don't draw if there's no selection */
    if (getSelection().getPoints().isEmpty()) {
        return;
    }

    /* Create iterators for the colors */
    Iterator<Color> colors = Iterators.cycle(COLORS);
    Iterator<Color> lights = Iterators.cycle(COLORS_LIGHT);

    GC gc = event.gc;

    /* Redraw all the series */
    for (ISeries swtSeries : getChart().getSeriesSet().getSeries()) {
        IBarSeries series = (IBarSeries) swtSeries;
        Color color = checkNotNull(colors.next());
        Color light = checkNotNull(lights.next());

        /* Redraw all the rectangles */
        for (int i = 0; i < series.getBounds().length; i++) {
            gc.setBackground(light);

            /* Check if the rectangle is selected */
            for (SwtChartPoint point : getSelection().getPoints()) {
                if (point.getSeries() == series && point.getIndex() == i) {
                    gc.setBackground(color);
                    break;
                }
            }

            gc.fillRectangle(series.getBounds()[i]);
        }
    }
}
 
Example 11
Source File: _assoc.java    From mesh with MIT License 5 votes vote down vote up
public static MapValue invoke(final ListValue keys, final ListValue vals)
{
    if (vals.size() == 0)
        return PersistentMap.EMPTY;

    final PersistentMap result = PersistentMap.fresh();

    final Iterator<?> valiter = vals.size() >= keys.size() ?
        vals.iterator() : Iterators.cycle(vals);

    for (final Object key : keys)
        result.assocUnsafe(key, valiter.next());

    return result;
}
 
Example 12
Source File: CompareHistogramsWithOtherLibraries.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Iteration)
public void setup() {
    final Random r = new Random(1234567891L);
    dataIterator = Iterators.cycle(
            Stream.generate(() -> Math.round(Math.exp(2.0 + r.nextGaussian()))).limit(1048576)
                    .collect(Collectors.toList()));
}
 
Example 13
Source File: KafkaUnboundedReader.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private void nextBatch() throws IOException {
  curBatch = Collections.emptyIterator();

  ConsumerRecords<byte[], byte[]> records;
  try {
    // poll available records, wait (if necessary) up to the specified timeout.
    records =
        availableRecordsQueue.poll(
            RECORDS_DEQUEUE_POLL_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS);
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    LOG.warn("{}: Unexpected", this, e);
    return;
  }

  if (records == null) {
    // Check if the poll thread failed with an exception.
    if (consumerPollException.get() != null) {
      throw new IOException("Exception while reading from Kafka", consumerPollException.get());
    }
    return;
  }

  partitionStates.forEach(p -> p.recordIter = records.records(p.topicPartition).iterator());

  // cycle through the partitions in order to interleave records from each.
  curBatch = Iterators.cycle(new ArrayList<>(partitionStates));
}
 
Example 14
Source File: StressTestClient.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
WeightedTestCaseSelector(List<TestCaseWeightPair> testCaseWeightPairs) {
  Preconditions.checkNotNull(testCaseWeightPairs, "testCaseWeightPairs");
  Preconditions.checkArgument(testCaseWeightPairs.size() > 0);

  List<TestCases> testCases = new ArrayList<>();
  for (TestCaseWeightPair testCaseWeightPair : testCaseWeightPairs) {
    for (int i = 0; i < testCaseWeightPair.weight; i++) {
      testCases.add(testCaseWeightPair.testCase);
    }
  }

  shuffle(testCases);

  this.testCases = Iterators.cycle(testCases);
}
 
Example 15
Source File: CircularRoutingDataSource.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CircularRoutingDataSource( List<DataSource> targetDataSources )
{
    this.dataSourceIterator = Iterators.cycle( Collections.synchronizedList( targetDataSources ) );
}
 
Example 16
Source File: LoadTestAction.java    From nomulus with Apache License 2.0 4 votes vote down vote up
private Function<String, String> listNameReplacer(final String toReplace, List<String> choices) {
  final Iterator<String> iterator = Iterators.cycle(choices);
  return xml -> xml.replace(toReplace, iterator.next());
}
 
Example 17
Source File: DeterministicStringGenerator.java    From nomulus with Apache License 2.0 4 votes vote down vote up
public DeterministicStringGenerator(@Named("alphabetBase64") String alphabet, Rule rule) {
  super(alphabet);
  iterator = Iterators.cycle(charactersOf(alphabet));
  this.rule = rule;
}
 
Example 18
Source File: TransactionPruningServiceTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoPruning() throws Exception {
  // Setup plugins
  Configuration conf = new Configuration();
  conf.set(TxConstants.TransactionPruning.PLUGINS,
           "data.tx.txprune.plugin.mockPlugin1, data.tx.txprune.plugin.mockPlugin2");
  conf.set("data.tx.txprune.plugin.mockPlugin1.class",
           "org.apache.tephra.txprune.TransactionPruningServiceTest$MockPlugin1");
  conf.set("data.tx.txprune.plugin.mockPlugin2.class",
           "org.apache.tephra.txprune.TransactionPruningServiceTest$MockPlugin2");
  // Setup schedule to run every second
  conf.setBoolean(TxConstants.TransactionPruning.PRUNE_ENABLE, true);
  conf.setInt(TxConstants.Manager.CFG_TX_MAX_LIFETIME, 10);
  conf.setLong(TxConstants.TransactionPruning.PRUNE_GRACE_PERIOD, 0);

  // Setup mock data
  long m = 1000;
  long n = m * TxConstants.MAX_TX_PER_MS;
  // Current time to be returned
  Iterator<Long> currentTime = Iterators.cycle(120L * m, 220L * m);
  // Transaction objects to be returned by mock tx manager
  Iterator<Transaction> txns =
    Iterators.cycle(new Transaction(100 * n, 110 * n, new long[]{40 * n, 50 * n, 60 * n, 70 * n},
                                    new long[]{80 * n, 90 * n}, 80 * n),
                    new Transaction(200 * n, 210 * n, new long[]{60 * n, 75 * n, 78 * n, 100 * n, 110 * n, 120 * n},
                                    new long[]{80 * n, 90 * n}, 80 * n));
  // Prune upper bounds to be returned by the mock plugins
  Iterator<Long> pruneUpperBoundsPlugin1 = Iterators.cycle(35L * n, -1L);
  Iterator<Long> pruneUpperBoundsPlugin2 = Iterators.cycle(70L * n, 100L * n);

  TestTransactionPruningRunnable.setCurrentTime(currentTime);
  MockTxManager.setTxIter(txns);
  MockPlugin1.setPruneUpperBoundIter(pruneUpperBoundsPlugin1);
  MockPlugin2.setPruneUpperBoundIter(pruneUpperBoundsPlugin2);

  MockTxManager mockTxManager = new MockTxManager(conf);
  TransactionPruningService pruningService = new TestTransactionPruningService(conf, mockTxManager);
  pruningService.startAndWait();
  // This will cause the pruning run to happen three times,
  // but we are interested in only first two runs for the assertions later
  int pruneRuns = TestTransactionPruningRunnable.getRuns();
  pruningService.pruneNow();
  pruningService.pruneNow();
  pruningService.pruneNow();
  TestTransactionPruningRunnable.waitForRuns(pruneRuns + 3, 5, TimeUnit.MILLISECONDS);
  pruningService.stopAndWait();

  // Assert inactive transaction bound
  Assert.assertEquals(ImmutableList.of(110L * n - 1, 210L * n - 1),
                      limitTwo(MockPlugin1.getInactiveTransactionBoundList()));
  Assert.assertEquals(ImmutableList.of(110L * n - 1, 210L * n - 1),
                      limitTwo(MockPlugin2.getInactiveTransactionBoundList()));

  // Invalid entries should not be pruned in any run
  Assert.assertEquals(ImmutableList.of(), MockTxManager.getPrunedInvalidsList());

  // No max invalid tx pruned for any run
  Assert.assertEquals(ImmutableList.of(), limitTwo(MockPlugin1.getMaxPrunedInvalidList()));
  Assert.assertEquals(ImmutableList.of(), limitTwo(MockPlugin2.getMaxPrunedInvalidList()));
}
 
Example 19
Source File: TrackerClientUriProvider.java    From joal with Apache License 2.0 4 votes vote down vote up
public TrackerClientUriProvider(@SuppressWarnings("TypeMayBeWeakened") final List<URI> trackersURI) {
    // TODO: sorted(new PreferHTTPSComparator())
    this.addressIterator = Iterators.cycle(trackersURI);
}
 
Example 20
Source File: AtlasVirtualFile.java    From atlas with Apache License 2.0 4 votes vote down vote up
private void reset() {
    this.allFilesCyclic = Iterators.cycle(this.files.values());
    this.restart();
}