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.collections; 027 028import javafx.beans.WeakListener; 029 030import java.lang.ref.WeakReference; 031 032/** 033 * A {@code WeakListChangeListener} can be used, if an {@link ObservableList} 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 WeakListChangeListener} are created by passing in the original 039 * {@link ListChangeListener}. The {@code WeakListChangeListener} 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 ListChangeListener}, that 043 * was passed in as long as it is in use, otherwise it will be garbage collected 044 * to soon. 045 * 046 * @see ListChangeListener 047 * @see ObservableList 048 * @see javafx.beans.WeakListener 049 * 050 * @param <E> 051 * The type of the observed value 052 * 053 */ 054public final class WeakListChangeListener<E> implements ListChangeListener<E>, WeakListener { 055 056 private final WeakReference<ListChangeListener<E>> ref; 057 058 /** 059 * The constructor of {@code WeakListChangeListener}. 060 * 061 * @param listener 062 * The original listener that should be notified 063 */ 064 public WeakListChangeListener(ListChangeListener<E> listener) { 065 if (listener == null) { 066 throw new NullPointerException("Listener must be specified."); 067 } 068 this.ref = new WeakReference<ListChangeListener<E>>(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 onChanged(Change<? extends E> change) { 084 final ListChangeListener<E> listener = ref.get(); 085 if (listener != null) { 086 listener.onChanged(change); 087 } else { 088 // The weakly reference listener has been garbage collected, 089 // so this WeakListener will now unhook itself from the 090 // source bean 091 change.getList().removeListener(this); 092 } 093 } 094}