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