View Javadoc
1   package org.djutils.event.collection;
2   
3   import java.util.ListIterator;
4   
5   import org.djutils.event.EventType;
6   import org.djutils.metadata.MetaData;
7   
8   /**
9    * EventProducingListIterator provides an iterator embedding the ListIterator, which fires an event when an object has been
10   * removed. Note that one does not have to subscribe specifically to the events of the EventProducingListIterator, as the
11   * EventProducing collection subscribes to the EventProducingListIterator's remove events and fires these again to its
12   * subscribers.
13   * <p>
14   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
16   * distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
18   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
19   * https://simulation.tudelft.nl/dsol/manual</a>.
20   * </p>
21   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @param <T> the type of elements to iterate on
23   */
24  public class EventProducingListIterator<T> extends EventProducingIterator<T> implements ListIterator<T>
25  {
26      /** OBJECT_ADDED_EVENT is fired on adding of entries. */
27      public static final EventType OBJECT_ADDED_EVENT = new EventType("OBJECT_ADDED_EVENT", MetaData.EMPTY);
28  
29      /** OBJECT_CHANGED_EVENT is fired on changing of entries. */
30      public static final EventType OBJECT_CHANGED_EVENT = new EventType("OBJECT_CHANGED_EVENT", MetaData.EMPTY);
31  
32      /**
33       * constructs a new EventProducingListIterator, embedding the parent ListIterator.
34       * @param wrappedIterator embedded iterator.
35       */
36      public EventProducingListIterator(final ListIterator<T> wrappedIterator)
37      {
38          super(wrappedIterator);
39      }
40  
41      @Override
42      protected ListIterator<T> getWrappedIterator()
43      {
44          return (ListIterator<T>) super.getWrappedIterator();
45      }
46  
47      @Override
48      public boolean hasPrevious()
49      {
50          return getWrappedIterator().hasPrevious();
51      }
52  
53      @Override
54      public T previous()
55      {
56          return getWrappedIterator().previous();
57      }
58  
59      @Override
60      public int nextIndex()
61      {
62          return getWrappedIterator().nextIndex();
63      }
64  
65      @Override
66      public int previousIndex()
67      {
68          return getWrappedIterator().previousIndex();
69      }
70  
71      @Override
72      public void set(final T e)
73      {
74          getWrappedIterator().set(e);
75          fireEvent(OBJECT_CHANGED_EVENT);
76      }
77  
78      @Override
79      public void add(final T e)
80      {
81          getWrappedIterator().add(e);
82          fireEvent(OBJECT_ADDED_EVENT);
83      }
84  
85  }