package nl.tudelft.simulation.event.util; import java.util.Collection; import java.util.Map; import java.util.Set; import nl.tudelft.simulation.event.EventProducer; import nl.tudelft.simulation.event.EventType; /** * The Event producing map provides a map to which one can subscribe interest in entry changes. This class does not keep * track of changes which take place indirectly. One is for example not notified on map.iterator.remove(). * A listener must subscribe to the iterator, key set, etc. individually. *

* Copyright (c) 2002-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights * reserved. See for project information * https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can * be found at * https://simulation.tudelft.nl/dsol/3.0/license.html. *

* @author Peter Jacobs * @author Alexander Verbraeck * @since 1.5 * @param the key type * @param the value type */ public class EventProducingMap extends EventProducer implements Map { /** The default serial version UID for serializable classes. */ private static final long serialVersionUID = 1L; /** OBJECT_ADDED_EVENT is fired on new entries. */ public static final EventType OBJECT_ADDED_EVENT = new EventType("OBJECT_ADDED_EVENT"); /** OBJECT_REMOVED_EVENT is fired on removel of entries. */ public static final EventType OBJECT_REMOVED_EVENT = new EventType("OBJECT_REMOVED_EVENT"); /** the parent map. */ private Map parent = null; /** * constructs a new EventProducingMap. * @param parent Map<K,V>; the parent map. */ public EventProducingMap(final Map parent) { super(); this.parent = parent; } /** {@inheritDoc} */ @Override public int size() { return this.parent.size(); } /** {@inheritDoc} */ @Override public boolean isEmpty() { return this.parent.isEmpty(); } /** {@inheritDoc} */ @Override public boolean containsKey(final Object arg0) { return this.parent.containsKey(arg0); } /** {@inheritDoc} */ @Override public boolean containsValue(final Object arg0) { return this.parent.containsValue(arg0); } /** {@inheritDoc} */ @Override public V get(final Object arg0) { return this.parent.get(arg0); } /** {@inheritDoc} */ @Override public V put(final K arg0, final V arg1) { V result = this.parent.put(arg0, arg1); this.fireEvent(OBJECT_ADDED_EVENT, null); return result; } /** {@inheritDoc} */ @Override public V remove(final Object arg0) { V result = this.parent.remove(arg0); this.fireEvent(OBJECT_REMOVED_EVENT, null); return result; } /** {@inheritDoc} */ @Override public void putAll(final Map arg0) { this.parent.putAll(arg0); this.fireEvent(OBJECT_ADDED_EVENT, null); } /** {@inheritDoc} */ @Override public void clear() { this.parent.clear(); this.fireEvent(OBJECT_REMOVED_EVENT, null); } /** {@inheritDoc} */ @Override public Set keySet() { return this.parent.keySet(); } /** {@inheritDoc} */ @Override public Collection values() { return this.parent.values(); } /** {@inheritDoc} */ @Override public Set> entrySet() { return this.parent.entrySet(); } }