Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.beans;
027
028import java.lang.ref.WeakReference;
029
030/**
031 * A {@code WeakInvalidationListener} can be used, if an {@link Observable}
032 * should only maintain a weak reference to the listener. This helps to avoid
033 * memory leaks, that can occur if observers are not unregistered from observed
034 * objects after use.
035 * <p>
036 * {@code WeakInvalidationListener} are created by passing in the original
037 * {@link InvalidationListener}. The {@code WeakInvalidationListener} should
038 * then be registered to listen for changes of the observed object.
039 * <p>
040 * Note: You have to keep a reference to the {@code InvalidationListener}, that
041 * was passed in as long as it is in use, otherwise it will be garbage collected
042 * to soon.
043 *
044 * @see InvalidationListener
045 * @see Observable
046 * 
047 * 
048 */
049public final class WeakInvalidationListener implements InvalidationListener, WeakListener {
050
051    private final WeakReference<InvalidationListener> ref;
052
053    /**
054     * The constructor of {@code WeakInvalidationListener}.
055     * 
056     * @param listener
057     *            The original listener that should be notified
058     */
059    public WeakInvalidationListener(InvalidationListener listener) {
060        if (listener == null) {
061            throw new NullPointerException("Listener must be specified.");
062        }
063        this.ref = new WeakReference<InvalidationListener>(listener);
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public boolean wasGarbageCollected() {
071        return (ref.get() == null);
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public void invalidated(Observable observable) {
079        InvalidationListener listener = ref.get();
080        if (listener != null) {
081            listener.invalidated(observable);
082        } else {
083            // The weakly reference listener has been garbage collected,
084            // so this WeakListener will now unhook itself from the
085            // source bean
086            observable.removeListener(this);
087        }
088    }
089}