Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2011, 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.value; 027 028import javafx.beans.WeakListener; 029 030import java.lang.ref.WeakReference; 031 032/** 033 * A {@code WeakChangeListener} can be used, if an {@link ObservableValue} 034 * should only maintain a weak reference to the listener. This helps to avoid 035 * memory leaks, that can occur if observers are not unregistered from observed 036 * objects after use. 037 * <p> 038 * {@code WeakChangeListener} are created by passing in the original 039 * {@link ChangeListener}. The {@code WeakChangeListener} should then be 040 * registered to listen for changes of the observed object. 041 * <p> 042 * Note: You have to keep a reference to the {@code ChangeListener}, that 043 * was passed in as long as it is in use, otherwise it will be garbage collected 044 * to soon. 045 * 046 * @see ChangeListener 047 * @see ObservableValue 048 * 049 * @param <T> 050 * The type of the observed value 051 * 052 * 053 */ 054public final class WeakChangeListener<T> implements ChangeListener<T>, WeakListener { 055 056 private final WeakReference<ChangeListener<T>> ref; 057 058 /** 059 * The constructor of {@code WeakChangeListener}. 060 * 061 * @param listener 062 * The original listener that should be notified 063 */ 064 public WeakChangeListener(ChangeListener<T> listener) { 065 if (listener == null) { 066 throw new NullPointerException("Listener must be specified."); 067 } 068 this.ref = new WeakReference<ChangeListener<T>>(listener); 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 @Override 075 public boolean wasGarbageCollected() { 076 return (ref.get() == null); 077 } 078 079 /** 080 * {@inheritDoc} 081 */ 082 @Override 083 public void changed(ObservableValue<? extends T> observable, T oldValue, 084 T newValue) { 085 ChangeListener<T> listener = ref.get(); 086 if (listener != null) { 087 listener.changed(observable, oldValue, newValue); 088 } else { 089 // The weakly reference listener has been garbage collected, 090 // so this WeakListener will now unhook itself from the 091 // source bean 092 observable.removeListener(this); 093 } 094 } 095 096}