6463901: Either generify or deprecate sun.awt.EventListenerAggregate

Reviewed-by: anthony, serb
This commit is contained in:
Petr Pchelko 2014-04-02 17:37:34 +04:00
parent c99112a796
commit 09c759135e
3 changed files with 38 additions and 254 deletions

View File

@ -27,14 +27,13 @@ package java.awt.datatransfer;
import java.awt.EventQueue;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.io.IOException;
import sun.awt.EventListenerAggregate;
/**
* A class that implements a mechanism to transfer data using
* cut/copy/paste operations.
@ -68,7 +67,7 @@ public class Clipboard {
*
* @since 1.5
*/
private EventListenerAggregate flavorListeners;
private final Set<FlavorListener> flavorListeners = new HashSet<>();
/**
* A set of <code>DataFlavor</code>s that is available on
@ -86,6 +85,7 @@ public class Clipboard {
*/
public Clipboard(String name) {
this.name = name;
currentDataFlavors = getAvailableDataFlavorSet();
}
/**
@ -131,11 +131,7 @@ public class Clipboard {
this.contents = contents;
if (oldOwner != null && oldOwner != owner) {
EventQueue.invokeLater(new Runnable() {
public void run() {
oldOwner.lostOwnership(Clipboard.this, oldContents);
}
});
EventQueue.invokeLater(() -> oldOwner.lostOwnership(Clipboard.this, oldContents));
}
fireFlavorsChanged();
}
@ -261,10 +257,6 @@ public class Clipboard {
if (listener == null) {
return;
}
if (flavorListeners == null) {
currentDataFlavors = getAvailableDataFlavorSet();
flavorListeners = new EventListenerAggregate(FlavorListener.class);
}
flavorListeners.add(listener);
}
@ -286,7 +278,7 @@ public class Clipboard {
* @since 1.5
*/
public synchronized void removeFlavorListener(FlavorListener listener) {
if (listener == null || flavorListeners == null) {
if (listener == null) {
return;
}
flavorListeners.remove(listener);
@ -305,8 +297,7 @@ public class Clipboard {
* @since 1.5
*/
public synchronized FlavorListener[] getFlavorListeners() {
return flavorListeners == null ? new FlavorListener[0] :
(FlavorListener[])flavorListeners.getListenersCopy();
return flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
}
/**
@ -317,24 +308,14 @@ public class Clipboard {
* @since 1.5
*/
private void fireFlavorsChanged() {
if (flavorListeners == null) {
return;
}
Set<DataFlavor> prevDataFlavors = currentDataFlavors;
currentDataFlavors = getAvailableDataFlavorSet();
if (prevDataFlavors.equals(currentDataFlavors)) {
if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
return;
}
FlavorListener[] flavorListenerArray =
(FlavorListener[])flavorListeners.getListenersInternal();
for (int i = 0; i < flavorListenerArray.length; i++) {
final FlavorListener listener = flavorListenerArray[i];
EventQueue.invokeLater(new Runnable() {
public void run() {
listener.flavorsChanged(new FlavorEvent(Clipboard.this));
}
});
}
flavorListeners.forEach(listener ->
EventQueue.invokeLater(() ->
listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
}
/**

View File

@ -1,175 +0,0 @@
/*
* Copyright (c) 2003, 2011, 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 sun.awt;
import java.lang.reflect.Array;
import java.util.EventListener;
/**
* A class that assists in managing {@link java.util.EventListener}s of
* the specified type. Its instance holds an array of listeners of the same
* type and allows to perform the typical operations on the listeners.
* This class is thread-safe.
*
* @author Alexander Gerasimov
*
* @since 1.5
*/
public class EventListenerAggregate {
private EventListener[] listenerList;
/**
* Constructs an <code>EventListenerAggregate</code> object.
*
* @param listenerClass the type of the listeners to be managed by this object
*
* @throws NullPointerException if <code>listenerClass</code> is
* <code>null</code>
* @throws ClassCastException if <code>listenerClass</code> is not
* assignable to <code>java.util.EventListener</code>
*/
public EventListenerAggregate(Class<? extends EventListener> listenerClass) {
if (listenerClass == null) {
throw new NullPointerException("listener class is null");
}
listenerList = (EventListener[])Array.newInstance(listenerClass, 0);
}
private Class<?> getListenerClass() {
return listenerList.getClass().getComponentType();
}
/**
* Adds the listener to this aggregate.
*
* @param listener the listener to be added
*
* @throws ClassCastException if <code>listener</code> is not
* an instatce of <code>listenerClass</code> specified
* in the constructor
*/
public synchronized void add(EventListener listener) {
Class<?> listenerClass = getListenerClass();
if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
throw new ClassCastException("listener " + listener + " is not " +
"an instance of listener class " + listenerClass);
}
EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
tmp[listenerList.length] = listener;
listenerList = tmp;
}
/**
* Removes a listener that is equal to the given one from this aggregate.
* <code>equals()</code> method is used to compare listeners.
*
* @param listener the listener to be removed
*
* @return <code>true</code> if this aggregate contained the specified
* <code>listener</code>; <code>false</code> otherwise
*
* @throws ClassCastException if <code>listener</code> is not
* an instatce of <code>listenerClass</code> specified
* in the constructor
*/
public synchronized boolean remove(EventListener listener) {
Class<?> listenerClass = getListenerClass();
if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
throw new ClassCastException("listener " + listener + " is not " +
"an instance of listener class " + listenerClass);
}
for (int i = 0; i < listenerList.length; i++) {
if (listenerList[i].equals(listener)) {
EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass,
listenerList.length - 1);
System.arraycopy(listenerList, 0, tmp, 0, i);
System.arraycopy(listenerList, i + 1, tmp, i, listenerList.length - i - 1);
listenerList = tmp;
return true;
}
}
return false;
}
/**
* Returns an array of all the listeners contained in this aggregate.
* The array is the data structure in which listeners are stored internally.
* The runtime type of the returned array is "array of <code>listenerClass</code>"
* (<code>listenerClass</code> has been specified as a parameter to
* the constructor of this class).
*
* @return all the listeners contained in this aggregate (an empty
* array if there are no listeners)
*/
public synchronized EventListener[] getListenersInternal() {
return listenerList;
}
/**
* Returns an array of all the listeners contained in this aggregate.
* The array is a copy of the data structure in which listeners are stored
* internally.
* The runtime type of the returned array is "array of <code>listenerClass</code>"
* (<code>listenerClass</code> has been specified as a parameter to
* the constructor of this class).
*
* @return a copy of all the listeners contained in this aggregate (an empty
* array if there are no listeners)
*/
public synchronized EventListener[] getListenersCopy() {
return (listenerList.length == 0) ? listenerList : listenerList.clone();
}
/**
* Returns the number of lisetners in this aggregate.
*
* @return the number of lisetners in this aggregate
*/
public synchronized int size() {
return listenerList.length;
}
/**
* Returns <code>true</code> if this aggregate contains no listeners,
* <code>false</code> otherwise.
*
* @return <code>true</code> if this aggregate contains no listeners,
* <code>false</code> otherwise
*/
public synchronized boolean isEmpty() {
return listenerList.length == 0;
}
}

View File

@ -40,7 +40,7 @@ import java.awt.datatransfer.UnsupportedFlavorException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;
@ -49,7 +49,6 @@ import java.io.IOException;
import sun.awt.AppContext;
import sun.awt.PeerEvent;
import sun.awt.SunToolkit;
import sun.awt.EventListenerAggregate;
/**
@ -110,11 +109,7 @@ public abstract class SunClipboard extends Clipboard
setContentsNative(contents);
} finally {
if (oldOwner != null && oldOwner != owner) {
EventQueue.invokeLater(new Runnable() {
public void run() {
oldOwner.lostOwnership(SunClipboard.this, oldContents);
}
});
EventQueue.invokeLater(() -> oldOwner.lostOwnership(SunClipboard.this, oldContents));
}
}
}
@ -358,13 +353,12 @@ public abstract class SunClipboard extends Clipboard
return;
}
AppContext appContext = AppContext.getAppContext();
EventListenerAggregate contextFlavorListeners = (EventListenerAggregate)
appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
if (contextFlavorListeners == null) {
contextFlavorListeners = new EventListenerAggregate(FlavorListener.class);
appContext.put(CLIPBOARD_FLAVOR_LISTENER_KEY, contextFlavorListeners);
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
if (flavorListeners == null) {
flavorListeners = new HashSet<>();
appContext.put(CLIPBOARD_FLAVOR_LISTENER_KEY, flavorListeners);
}
contextFlavorListeners.add(listener);
flavorListeners.add(listener);
if (numberOfFlavorListeners++ == 0) {
long[] currentFormats = null;
@ -385,25 +379,26 @@ public abstract class SunClipboard extends Clipboard
if (listener == null) {
return;
}
AppContext appContext = AppContext.getAppContext();
EventListenerAggregate contextFlavorListeners = (EventListenerAggregate)
appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
if (contextFlavorListeners == null){
Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext());
if (flavorListeners == null){
//else we throw NullPointerException, but it is forbidden
return;
}
if (contextFlavorListeners.remove(listener) &&
--numberOfFlavorListeners == 0) {
if (flavorListeners.remove(listener) && --numberOfFlavorListeners == 0) {
unregisterClipboardViewerChecked();
currentDataFlavors = null;
}
}
@SuppressWarnings("unchecked")
private Set<FlavorListener> getFlavorListeners(AppContext appContext) {
return (Set<FlavorListener>)appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
}
public synchronized FlavorListener[] getFlavorListeners() {
EventListenerAggregate contextFlavorListeners = (EventListenerAggregate)
AppContext.getAppContext().get(CLIPBOARD_FLAVOR_LISTENER_KEY);
return contextFlavorListeners == null ? new FlavorListener[0] :
(FlavorListener[])contextFlavorListeners.getListenersCopy();
Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext());
return flavorListeners == null ? new FlavorListener[0]
: flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
}
public boolean areFlavorListenersRegistered() {
@ -428,45 +423,28 @@ public abstract class SunClipboard extends Clipboard
Set prevDataFlavors = currentDataFlavors;
currentDataFlavors = formatArrayAsDataFlavorSet(formats);
if ((prevDataFlavors != null) && (currentDataFlavors != null) &&
prevDataFlavors.equals(currentDataFlavors)) {
if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
// we've been able to successfully get available on the clipboard
// DataFlavors this and previous time and they are coincident;
// don't notify
return;
}
class SunFlavorChangeNotifier implements Runnable {
private final FlavorListener flavorListener;
SunFlavorChangeNotifier(FlavorListener flavorListener) {
this.flavorListener = flavorListener;
}
public void run() {
if (flavorListener != null) {
flavorListener.flavorsChanged(new FlavorEvent(SunClipboard.this));
}
}
};
for (Iterator it = AppContext.getAppContexts().iterator(); it.hasNext();) {
AppContext appContext = (AppContext)it.next();
for (AppContext appContext : AppContext.getAppContexts()) {
if (appContext == null || appContext.isDisposed()) {
continue;
}
EventListenerAggregate flavorListeners = (EventListenerAggregate)
appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
if (flavorListeners != null) {
FlavorListener[] flavorListenerArray =
(FlavorListener[])flavorListeners.getListenersInternal();
for (int i = 0; i < flavorListenerArray.length; i++) {
SunToolkit.postEvent(appContext, new PeerEvent(this,
new SunFlavorChangeNotifier(flavorListenerArray[i]),
PeerEvent.PRIORITY_EVENT));
for (FlavorListener listener : flavorListeners) {
if (listener != null) {
PeerEvent peerEvent = new PeerEvent(this,
() -> listener.flavorsChanged(new FlavorEvent(SunClipboard.this)),
PeerEvent.PRIORITY_EVENT);
SunToolkit.postEvent(appContext, peerEvent);
}
}
}
}
}
}