View Javadoc
1   package org.djutils.event.util;
2   
3   import java.io.Serializable;
4   import java.util.ListIterator;
5   
6   import org.djutils.event.EventType;
7   import org.djutils.event.IdProvider;
8   import org.djutils.metadata.MetaData;
9   
10  /**
11   * EventProducingListIterator provides an iterator embedding the ListIterator, which fires an event when an object has been
12   * removed. Note that one does not have to subscribe specifically to the events of the EventProducingListIterator, as the
13   * EventProducing collection subscribes to the EventProducingListIterator's remove events and fires these again to its
14   * subscribers.
15   * <p>
16   * Copyright (c) 2002-2022 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.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @param <T> the type of elements to iterate on
25   */
26  public class EventProducingListIterator<T> extends EventProducingIterator<T> implements ListIterator<T>
27  {
28      /** */
29      private static final long serialVersionUID = 20191230L;
30  
31      /** OBJECT_ADDED_EVENT is fired on adding of entries. */
32      public static final EventType OBJECT_ADDED_EVENT = new EventType("OBJECT_ADDED_EVENT", MetaData.EMPTY);
33  
34      /** OBJECT_CHANGED_EVENT is fired on changing of entries. */
35      public static final EventType OBJECT_CHANGED_EVENT = new EventType("OBJECT_CHANGED_EVENT", MetaData.EMPTY);
36  
37      /**
38       * constructs a new EventProducingListIterator, embedding the parent ListIterator.
39       * @param parent ListIterator&lt;T&gt;; embedded iterator.
40       * @param sourceId Serializable; the id by which the EventProducer can be identified by the EventListener
41       */
42      public EventProducingListIterator(final ListIterator<T> parent, final Serializable sourceId)
43      {
44          super(parent, sourceId);
45      }
46  
47      /**
48       * Constructs a new EventProducingListIterator, embedding the parent iterator.
49       * @param parent ListIterator&lt;T&gt;; the parent set.
50       * @param sourceIdProvider IdProvider; the function that produces the id by which the EventProducer can be identified by the
51       *            EventListener
52       */
53      public EventProducingListIterator(final ListIterator<T> parent, final IdProvider sourceIdProvider)
54      {
55          super(parent, sourceIdProvider);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      protected ListIterator<T> getParent()
61      {
62          return (ListIterator<T>) super.getParent();
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public boolean hasPrevious()
68      {
69          return getParent().hasPrevious();
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public T previous()
75      {
76          return getParent().previous();
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public int nextIndex()
82      {
83          return getParent().nextIndex();
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public int previousIndex()
89      {
90          return getParent().previousIndex();
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public void set(final T e)
96      {
97          getParent().set(e);
98          fireEvent(OBJECT_CHANGED_EVENT);
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public void add(final T e)
104     {
105         getParent().add(e);
106         fireEvent(OBJECT_ADDED_EVENT);
107     }
108 
109 }