Class UnitIterator

  • All Implemented Interfaces:
    java.util.Iterator<Unit>

    public class UnitIterator
    extends java.lang.Object
    implements java.util.Iterator<Unit>
    An Iterator of Units that can be made active.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Player owner
      The player that owns the units.
      private java.util.function.Predicate<Unit> predicate
      The admission predicate.
      private java.util.List<Unit> units
      The current cache of units.
    • Constructor Summary

      Constructors 
      Constructor Description
      UnitIterator​(Player owner, java.util.function.Predicate<Unit> predicate)
      Creates a new UnitIterator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()
      Unit next()
      void remove()
      boolean remove​(Unit u)
      Removes a specific unit from this unit iterator.
      void reset()
      Reset the iterator.
      boolean setNext​(Unit unit)
      Set the next valid unit.
      private void update()
      Update the internal units list with units that satisfy the predicate.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • owner

        private final Player owner
        The player that owns the units.
      • predicate

        private final java.util.function.Predicate<Unit> predicate
        The admission predicate.
      • units

        private final java.util.List<Unit> units
        The current cache of units.
    • Constructor Detail

      • UnitIterator

        public UnitIterator​(Player owner,
                            java.util.function.Predicate<Unit> predicate)
        Creates a new UnitIterator.
        Parameters:
        owner - The Player that needs an iterator of it's units.
        predicate - A Predicate for deciding whether a Unit should be included in the Iterator or not.
    • Method Detail

      • update

        private final void update()
        Update the internal units list with units that satisfy the predicate.
      • setNext

        public boolean setNext​(Unit unit)
        Set the next valid unit.
        Parameters:
        unit - The Unit to put at the front of the list.
        Returns:
        True if the operation succeeds.
      • remove

        public boolean remove​(Unit u)
        Removes a specific unit from this unit iterator.
        Parameters:
        u - The Unit to remove.
        Returns:
        True if the unit was removed.
      • reset

        public void reset()
        Reset the iterator.
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<Unit>
      • next

        public Unit next()
        Specified by:
        next in interface java.util.Iterator<Unit>
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<Unit>