Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2012, 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.event; 027 028import java.lang.ref.WeakReference; 029 030/** 031 * Used in event handler registration in place of its associated event handler. 032 * Its sole purpose is to break the otherwise strong reference between an event 033 * handler container and its associated event handler. While the container still 034 * holds strong reference to the registered {@code WeakEventHandler} proxy, the 035 * proxy itself references the original handler only weakly and so doesn't 036 * prevent it from being garbage collected. Until this weak reference is broken, 037 * any event notification received by the proxy is forwarded to the original 038 * handler. 039 * 040 * @param <T> the event class this handler can handle 041 */ 042public final class WeakEventHandler<T extends Event> 043 implements EventHandler<T> { 044 private final WeakReference<EventHandler<T>> weakRef; 045 046 /** 047 * Creates a new instance of {@code WeakEventHandler}. 048 * 049 * @param eventHandler the original event handler to which to forward event 050 * notifications 051 */ 052 public WeakEventHandler(final EventHandler<T> eventHandler) { 053 weakRef = new WeakReference<EventHandler<T>>(eventHandler); 054 } 055 056 /** 057 * Indicates whether the associated event handler has been garbage 058 * collected. Used by containers to detect when the storage of corresponding 059 * references to this {@code WeakEventHandler} is no longer necessary. 060 * 061 * @return {@code true} if the associated handler has been garbage 062 * collected, {@code false} otherwise 063 */ 064 public boolean wasGarbageCollected() { 065 return weakRef.get() == null; 066 } 067 068 /** 069 * Forwards event notification to the associated event handler. 070 * 071 * @param event the event which occurred 072 */ 073 @Override 074 public void handle(final T event) { 075 final EventHandler<T> eventHandler = weakRef.get(); 076 if (eventHandler != null) { 077 eventHandler.handle(event); 078 } 079 } 080 081 /* Used for testing. */ 082 void clear() { 083 weakRef.clear(); 084 } 085}