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

The following are Jave code examples for showing how to use forEachRemaining() of the java.util.Iterator class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: SingletonIterator.java   View Source Code Vote up 12 votes
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 2
Project: openjdk-jdk10   File: SubList.java   View Source Code Vote up 8 votes
@Test(dataProvider = "all")
public void testIteratorForEachRemaining(List<Integer> list, int from, int to) {
    List<Integer> subList = list.subList(from, to);
    for (int k = 0; k < 16; ++k) {
        int r = from + rnd.nextInt(1 + to - from);
        Iterator<Integer> it = subList.iterator();
        for (int i = from; i < to; ++i) {
            assertTrue(it.hasNext());
            if (i == r) {
                Iterator<Integer> jt = list.listIterator(r);
                it.forEachRemaining(x ->
                    assertTrue(jt.hasNext() && x == jt.next()));
                break;
            }
            assertEquals(list.get(i), it.next());
        }
        it.forEachRemaining(x -> fail());
    }
}
 
Example 3
Project: HardVox   File: HardVox.java   View Source Code Vote up 5 votes
@EventHandler
public void serverStarting(FMLServerStartingEvent event) {
    Iterator<CommandModule> modules = Iterators.forArray(
            new RegionCommands(),
            new OperationCommands(),
            new SessionCommands());
    modules.forEachRemaining(m -> m.addCommands(event::registerServerCommand));
}
 
Example 4
Project: Arcadia-Spigot   File: MineFieldGame.java   View Source Code Vote up 5 votes
@Override
public void onPreStart() {
    this.startPosition = Utils.parseLocation((String) this.getGameMap().fetchSetting("startPosition"));
    for(Player player : Bukkit.getOnlinePlayers()) {
        if(!this.getAPI().getGameManager().isAlive(player)) continue;
        player.teleport(startPosition);
        player.setGameMode(GameMode.ADVENTURE);
    }
    this.targetPosition = Utils.parseLocation((String) this.getGameMap().fetchSetting("targetPosition"));
    this.towards = (String) this.getGameMap().fetchSetting("targetTowards");
    ((RelativeDistanceSidebar) this.getSidebar()).setTarget(targetPosition, towards);
    Cuboid minefield = new Cuboid(Utils.parseLocation((String) this.getGameMap().fetchSetting("minefieldBoundsA")),
            Utils.parseLocation((String) this.getGameMap().fetchSetting("minefieldBoundsB")));
    Iterator<Block> blocks = minefield.iterator();
    Random random = new Random();
    blocks.forEachRemaining(block -> {
        Block next = blocks.next();
        if(next.getRelative(BlockFace.UP).getType() == Material.AIR) {
            if(random.nextFloat() < 0.6F) {
                next.setType(Material.STONE_PLATE);
            }
        }
    });
    this.glass = new Cuboid(Utils.parseLocation((String) this.getGameMap().fetchSetting("glassBoundsA")),
            Utils.parseLocation((String) this.getGameMap().fetchSetting("glassBoundsB")));
    Iterator<Block> glassBlocks = glass.iterator();
    while(glassBlocks.hasNext()) {
        glassBlocks.next().setType(Material.GLASS);
    }
}
 
Example 5
Project: guava-mock   File: AbstractIteratorTester.java   View Source Code Vote up 5 votes
public void testForEachRemaining() {
  for (int i = 0; i < expectedElements.size() - 1; i++) {
    List<E> targetElements = new ArrayList<E>();
    Iterator<E> iterator = newTargetIterator();
    for (int j = 0; j < i; j++) {
      targetElements.add(iterator.next());
    }
    iterator.forEachRemaining(targetElements::add);
    if (knownOrder == KnownOrder.KNOWN_ORDER) {
      assertEquals(expectedElements, targetElements);
    } else {
      Helpers.assertEqualIgnoringOrder(expectedElements, targetElements);
    }
  }
}
 
Example 6
Project: skript-mirror   File: ExprSpread.java   View Source Code Vote up 5 votes
private Object[] toArray(Iterator<?> iter) {
  List<Object> list = new ArrayList<>();
  iter.forEachRemaining(list::add);
  return list.toArray();
}
 
Example 7
Project: Randores2   File: PotionEffectAbility.java   View Source Code Vote up 5 votes
@Override
public List<Ability> generate(Random random, AbilityStage stage, AbilityType type) {
    Potion potion;
    int ticks;
    int amplifier;

    if(type == AbilityType.ARMOR_PASSIVE) {
        ticks = 20 * 15;
    } else {
        ticks = Generator.ticks.randomElement(random);
    }

    Iterator<Potion> potionIterator = Potion.REGISTRY.iterator();
    List<Potion> potions = new ArrayList<>();
    potionIterator.forEachRemaining(potions::add);

    int index = random.nextInt(potions.size());

    do {
        potion = potions.get(index);
        index++;
        if(index >= potions.size()) {
            index = 0;
        }
    } while (!goodFor(potion, type));

    if(potion.isInstant()) {
        ticks = 1;
    }

    amplifier = Generator.amplifier.randomElement(random);

    return Items.buildList(new PotionEffectAbility(potion, ticks, amplifier));
}
 
Example 8
Project: googles-monorepo-demo   File: AbstractIteratorTester.java   View Source Code Vote up 5 votes
public void testForEachRemaining() {
  for (int i = 0; i < expectedElements.size() - 1; i++) {
    List<E> targetElements = new ArrayList<E>();
    Iterator<E> iterator = newTargetIterator();
    for (int j = 0; j < i; j++) {
      targetElements.add(iterator.next());
    }
    iterator.forEachRemaining(targetElements::add);
    if (knownOrder == KnownOrder.KNOWN_ORDER) {
      assertEquals(expectedElements, targetElements);
    } else {
      Helpers.assertEqualIgnoringOrder(expectedElements, targetElements);
    }
  }
}
 
Example 9
Project: openjdk-jdk10   File: WhiteBox.java   View Source Code Vote up 5 votes
void assertIteratorExhausted(Iterator it) {
    if (rnd.nextBoolean()) {
        assertTrue(!it.hasNext());
        assertTrue(isDetached(it));
    }
    if (rnd.nextBoolean()) {
        it.forEachRemaining(e -> { throw new AssertionError(); });
        assertTrue(isDetached(it));
    }
    if (rnd.nextBoolean())
        try { it.next(); fail("should throw"); }
        catch (NoSuchElementException success) {}
}
 
Example 10
Project: openjdk-jdk10   File: SingletonIterator.java   View Source Code Vote up 5 votes
static void assertIteratorExhausted(Iterator<?> it) {
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("should have thrown NoSuchElementException");
    } catch (NoSuchElementException success) { }
    it.forEachRemaining(e -> { throw new AssertionError("action called incorrectly"); });
}
 
Example 11
Project: ontonethub   File: OntonethubOntologiesResource.java   View Source Code Vote up 5 votes
private void addTextToRepresentation(String oldFieldName, 
 		String newFieldName, 
 		Representation oldRepresentation, 
 		Representation newRepresentation, String lang, ValueFactory vf){
 	Iterator<Text> it = oldRepresentation.get(oldFieldName, lang);
 	
 	if(it != null){
 		it.forEachRemaining(obj -> {
	Text t = vf.createText(obj.getText(), lang);
	newRepresentation.set(newFieldName, t);
});
 	}
 }
 
Example 12
Project: ontonethub   File: OntonethubOntologiesResource.java   View Source Code Vote up 5 votes
private void addObjectToRepresentation(String oldFieldName, 
 		String newFieldName, 
 		Representation oldRepresentation, 
 		Representation newRepresentation, ValueFactory vf){
 	Iterator<Object> it = oldRepresentation.get(oldFieldName);
 	if(it != null)
it.forEachRemaining(obj -> {
	newRepresentation.set(newFieldName, obj);
});
 }
 
Example 13
Project: openjdk-jdk10   File: SingletonIterator.java   View Source Code Vote up 5 votes
public void testThrowFromForEachRemaining() {
    Iterator<String> it = Collections.singleton("TheOne").iterator();

    try {
        it.forEachRemaining(s -> { throw new SingletonException(); });
    } catch (SingletonException ignore) { }

    assertIteratorExhausted(it);
}
 
Example 14
Project: ontonethub   File: OntoNetHubImpl.java   View Source Code Vote up 5 votes
private void addObjectToRepresentation(String oldFieldName, 
 		String newFieldName, 
 		Representation oldRepresentation, 
 		Representation newRepresentation, ValueFactory vf){
 	Iterator<Object> it = oldRepresentation.get(oldFieldName);
 	if(it != null)
it.forEachRemaining(obj -> {
	newRepresentation.set(newFieldName, obj);
});
 }
 
Example 15
Project: openjdk-jdk10   File: SingletonIterator.java   View Source Code Vote up 5 votes
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);
    assertIteratorExhausted(it);
}
 
Example 16
Project: oryx2   File: MockSpeedModelManager.java   View Source Code Vote up 4 votes
@Override
public void consume(Iterator<KeyMessage<String,String>> updateIterator, Configuration hadoopConf) {
  updateIterator.forEachRemaining(update -> holder.add(new KeyMessageImpl<>(update.getKey(), update.getMessage())));
}
 
Example 17
Project: OOPIter2   File: ArmyManager.java   View Source Code Vote up 4 votes
public void executeCommands(){
    Iterator<Army> iter = armyList.iterator();
    iter.forEachRemaining(Army::executeCommand);
}
 
Example 18
Project: OOPIter2   File: ArmyManager.java   View Source Code Vote up 4 votes
public void resetCommands(){
    Iterator<Army> iter = armyList.iterator();
    iter.forEachRemaining(Army::resetCommands);
}
 
Example 19
Project: jdk8u-jdk   File: IteratorDefaults.java   View Source Code Vote up 4 votes
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 20
Project: processingGameEngine   File: Renderer.java   View Source Code Vote up 3 votes
public static void render(GUI g){
	g.render();
	
	Iterator<Element> i = g.elements.iterator();
	i.forEachRemaining(Renderer::render);
	
}