|
Java example source code file (PropertyListenerManager.java)
The PropertyListenerManager.java Java example source code/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.nashorn.internal.runtime; import java.util.Map; import java.util.WeakHashMap; /** * Helper class to manage property listeners and notification. */ public class PropertyListenerManager implements PropertyListener { PropertyListenerManager() {} /** property listeners for this object. */ private Map<PropertyListener,Boolean> listeners; // These counters are updated in debug mode private static int listenersAdded; private static int listenersRemoved; /** * Return aggregate listeners added to all PropertyListenerManagers * @return the listenersAdded */ public static int getListenersAdded() { return listenersAdded; } /** * Return aggregate listeners removed from all PropertyListenerManagers * @return the listenersRemoved */ public static int getListenersRemoved() { return listenersRemoved; } /** * Return listeners added to this PropertyListenerManager. * @return the listener count */ public final int getListenerCount() { return listeners != null? listeners.size() : 0; } // Property listener management methods /** * Add a property listener to this object. * * @param listener The property listener that is added. */ public synchronized final void addPropertyListener(final PropertyListener listener) { if (listeners == null) { listeners = new WeakHashMap<>(); } if (Context.DEBUG) { listenersAdded++; } listeners.put(listener, Boolean.TRUE); } /** * Remove a property listener from this object. * * @param listener The property listener that is removed. */ public synchronized final void removePropertyListener(final PropertyListener listener) { if (listeners != null) { if (Context.DEBUG) { listenersRemoved++; } listeners.remove(listener); } } /** * This method can be called to notify property addition to this object's listeners. * * @param object The ScriptObject to which property was added. * @param prop The property being added. */ protected synchronized final void notifyPropertyAdded(final ScriptObject object, final Property prop) { if (listeners != null) { for (PropertyListener listener : listeners.keySet()) { listener.propertyAdded(object, prop); } } } /** * This method can be called to notify property deletion to this object's listeners. * * @param object The ScriptObject from which property was deleted. * @param prop The property being deleted. */ protected synchronized final void notifyPropertyDeleted(final ScriptObject object, final Property prop) { if (listeners != null) { for (PropertyListener listener : listeners.keySet()) { listener.propertyDeleted(object, prop); } } } /** * This method can be called to notify property modification to this object's listeners. * * @param object The ScriptObject to which property was modified. * @param oldProp The old property being replaced. * @param newProp The new property that replaces the old property. */ protected synchronized final void notifyPropertyModified(final ScriptObject object, final Property oldProp, final Property newProp) { if (listeners != null) { for (PropertyListener listener : listeners.keySet()) { listener.propertyModified(object, oldProp, newProp); } } } /** * This method can be called to notify __proto__ modification to this object's listeners. * * @param object The ScriptObject whose __proto__ was changed. * @param oldProto old __proto__ * @param newProto new __proto__ */ protected synchronized final void notifyProtoChanged(final ScriptObject object, final ScriptObject oldProto, final ScriptObject newProto) { if (listeners != null) { for (PropertyListener listener : listeners.keySet()) { listener.protoChanged(object, oldProto, newProto); } } } // PropertyListener methods @Override public final void propertyAdded(final ScriptObject object, final Property prop) { notifyPropertyAdded(object, prop); } @Override public final void propertyDeleted(final ScriptObject object, final Property prop) { notifyPropertyDeleted(object, prop); } @Override public final void propertyModified(final ScriptObject object, final Property oldProp, final Property newProp) { notifyPropertyModified(object, oldProp, newProp); } @Override public final void protoChanged(final ScriptObject object, final ScriptObject oldProto, final ScriptObject newProto) { notifyProtoChanged(object, oldProto, newProto); } } Other Java examples (source code examples)Here is a short list of links related to this Java PropertyListenerManager.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.