1 package org.djutils.event.util;
2
3 import java.io.Serializable;
4 import java.util.Iterator;
5
6 import org.djutils.event.EventProducer;
7 import org.djutils.event.EventType;
8 import org.djutils.event.IdProvider;
9 import org.djutils.exceptions.Throw;
10 import org.djutils.metadata.MetaData;
11
12 /**
13 * The EventProducingIterator provides an iterator embedding the Iterator, which fires an event when an object has been removed.
14 * Note that one does not have to subscribe specifically to the events of the EventProducingIterator, as the EventProducing
15 * collection subscribes to the EventProducingIterator's remove events and fires these again to its subscribers.
16 * <p>
17 * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
19 * distributed under a three-clause BSD-style license, which can be found at
20 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
21 * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
22 * https://simulation.tudelft.nl/dsol/manual</a>.
23 * </p>
24 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
25 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26 * @param <T> the type of elements to iterate on
27 */
28 public class EventProducingIterator<T> extends EventProducer implements Iterator<T>
29 {
30 /** The default serial version UID for serializable classes. */
31 private static final long serialVersionUID = 20191230L;
32
33 /** OBJECT_REMOVED_EVENT is fired on removal of entries. */
34 public static final EventType OBJECT_REMOVED_EVENT = new EventType("OBJECT_REMOVED_EVENT", MetaData.NO_META_DATA);
35
36 /** our parent iterator. */
37 private Iterator<T> parent = null;
38
39 /** the function that produces the id by which the EventProducer can be identified. */
40 private final IdProvider sourceIdProvider;
41
42 /**
43 * constructs a new EventProducingIterator, embedding the parent Iterator.
44 * @param parent Iterator<T>; parent.
45 * @param sourceId Serializable; the id by which the EventProducer can be identified by the EventListener
46 */
47 public EventProducingIterator(final Iterator<T> parent, final Serializable sourceId)
48 {
49 this(parent, new IdProvider()
50 {
51 /** */
52 private static final long serialVersionUID = 20200119L;
53
54 @Override
55 public Serializable id()
56 {
57 return sourceId;
58 }
59 });
60 }
61
62 /**
63 * Constructs a new EventProducingIterator, embedding the parent iterator.
64 * @param parent Iterator<T>; the parent set.
65 * @param sourceIdProvider IdProvider; the function that produces the id by which the EventProducer can be identified by the
66 * EventListener
67 */
68 public EventProducingIterator(final Iterator<T> parent, final IdProvider sourceIdProvider)
69 {
70 Throw.whenNull(parent, "parent cannot be null");
71 Throw.whenNull(sourceIdProvider, "sourceIdprovider cannot be null");
72 this.parent = parent;
73 this.sourceIdProvider = sourceIdProvider;
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 public Serializable getSourceId()
79 {
80 return this.sourceIdProvider.id();
81 }
82
83 /** {@inheritDoc} */
84 @Override
85 public boolean hasNext()
86 {
87 return getParent().hasNext();
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 public T next()
93 {
94 return getParent().next();
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public void remove()
100 {
101 getParent().remove();
102 this.fireEvent(OBJECT_REMOVED_EVENT);
103 }
104
105 /**
106 * Return the embedded iterator.
107 * @return parent Iterator<T>; the embedded iterator
108 */
109 protected Iterator<T> getParent()
110 {
111 return this.parent;
112 }
113
114 }