View Javadoc
1   package org.djutils.event.collection;
2   
3   import java.io.Serializable;
4   import java.util.Iterator;
5   
6   import org.djutils.event.EventType;
7   import org.djutils.event.LocalEventProducer;
8   import org.djutils.exceptions.Throw;
9   import org.djutils.metadata.MetaData;
10  
11  /**
12   * The EventProducingIterator provides an iterator embedding the Iterator, which fires an event when an object has been removed.
13   * Note that one does not have to subscribe specifically to the events of the EventProducingIterator, as the EventProducing
14   * collection subscribes to the EventProducingIterator's remove events and fires these again to its subscribers.
15   * <p>
16   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
18   * distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
20   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
21   * https://simulation.tudelft.nl/dsol/manual</a>.
22   * </p>
23   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
24   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @param <T> the type of elements to iterate on
26   */
27  public class EventProducingIterator<T> extends LocalEventProducer implements Iterator<T>, Serializable
28  {
29      /** The default serial version UID for serializable classes. */
30      private static final long serialVersionUID = 20191230L;
31  
32      /** OBJECT_REMOVED_EVENT is fired on removal of entries. */
33      public static final EventType OBJECT_REMOVED_EVENT = new EventType("OBJECT_REMOVED_EVENT", MetaData.NO_META_DATA);
34  
35      /** our parent iterator. */
36      private Iterator<T> wrappedIterator = null;
37  
38      /**
39       * constructs a new EventProducingIterator, embedding the parent Iterator.
40       * @param wrappedIterator Iterator&lt;T&gt;; parent.
41       */
42      public EventProducingIterator(final Iterator<T> wrappedIterator)
43      {
44          Throw.whenNull(wrappedIterator, "parent cannot be null");
45          this.wrappedIterator = wrappedIterator;
46      }
47  
48      @Override
49      public boolean hasNext()
50      {
51          return getWrappedIterator().hasNext();
52      }
53  
54      @Override
55      public T next()
56      {
57          return getWrappedIterator().next();
58      }
59  
60      @Override
61      public void remove()
62      {
63          getWrappedIterator().remove();
64          fireEvent(OBJECT_REMOVED_EVENT);
65      }
66  
67      /**
68       * Return the embedded iterator.
69       * @return parent Iterator&lt;T&gt;; the embedded iterator
70       */
71      protected Iterator<T> getWrappedIterator()
72      {
73          return this.wrappedIterator;
74      }
75  
76  }