1 |
/* AWTKeyStroke.java -- an immutable key stroke |
/* AWTKeyStroke.java -- an immutable key stroke |
2 |
Copyright (C) 2002 Free Software Foundation |
Copyright (C) 2002, 2005 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
63 |
* instances of a subclass, using reflection, provided the subclass has a |
* instances of a subclass, using reflection, provided the subclass has a |
64 |
* no-arg constructor (of any accessibility). |
* no-arg constructor (of any accessibility). |
65 |
* |
* |
66 |
* @author Eric Blake <ebb9@email.byu.edu> |
* @author Eric Blake (ebb9@email.byu.edu) |
67 |
|
* @author Andrew John Hughes (gnu_andrew@member.fsf.org) |
68 |
* @see #getAWTKeyStroke(char) |
* @see #getAWTKeyStroke(char) |
69 |
* @since 1.4 |
* @since 1.4 |
70 |
* @status updated to 1.4 |
* @status updated to 1.4 |
85 |
* under the assumption that garbage collection of a new keystroke is |
* under the assumption that garbage collection of a new keystroke is |
86 |
* easy when we find the old one that it matches in the cache. |
* easy when we find the old one that it matches in the cache. |
87 |
*/ |
*/ |
88 |
private static final LinkedHashMap cache = new LinkedHashMap(11, 0.75f, true) |
private static final LinkedHashMap<AWTKeyStroke,AWTKeyStroke> cache = |
89 |
|
new LinkedHashMap<AWTKeyStroke,AWTKeyStroke>(11, 0.75f, true) |
90 |
{ |
{ |
91 |
/** The largest the keystroke cache can grow. */ |
/** The largest the keystroke cache can grow. */ |
92 |
private static final int MAX_CACHE_SIZE = 2048; |
private static final int MAX_CACHE_SIZE = 2048; |
93 |
|
|
94 |
/** Prune stale entries. */ |
/** Prune stale entries. */ |
95 |
protected boolean removeEldestEntry(Map.Entry eldest) |
protected boolean removeEldestEntry(Map.Entry<AWTKeyStroke,AWTKeyStroke> |
96 |
|
eldest) |
97 |
{ // XXX - FIXME Use Map.Entry, not just Entry as gcj 3.1 workaround. |
{ // XXX - FIXME Use Map.Entry, not just Entry as gcj 3.1 workaround. |
98 |
return size() > MAX_CACHE_SIZE; |
return size() > MAX_CACHE_SIZE; |
99 |
} |
} |
115 |
* |
* |
116 |
* @see #getAWTKeyStroke(String) |
* @see #getAWTKeyStroke(String) |
117 |
*/ |
*/ |
118 |
private static final HashMap vktable = new HashMap(); |
private static final HashMap<String,Object> vktable = new HashMap<String,Object>(); |
119 |
static |
static |
120 |
{ |
{ |
121 |
// Using reflection saves the hassle of keeping this in sync with KeyEvent, |
// Using reflection saves the hassle of keeping this in sync with KeyEvent, |
230 |
* @throws IllegalArgumentException subclass doesn't have no-arg constructor |
* @throws IllegalArgumentException subclass doesn't have no-arg constructor |
231 |
* @throws ClassCastException subclass doesn't extend AWTKeyStroke |
* @throws ClassCastException subclass doesn't extend AWTKeyStroke |
232 |
*/ |
*/ |
233 |
protected static void registerSubclass(final Class subclass) |
protected static void registerSubclass(final Class<?> subclass) |
234 |
{ |
{ |
235 |
if (subclass == null) |
if (subclass == null) |
236 |
throw new IllegalArgumentException(); |
throw new IllegalArgumentException(); |
253 |
throws NoSuchMethodException, InstantiationException, |
throws NoSuchMethodException, InstantiationException, |
254 |
IllegalAccessException, InvocationTargetException |
IllegalAccessException, InvocationTargetException |
255 |
{ |
{ |
256 |
Constructor c = subclass.getDeclaredConstructor(null); |
Constructor<?> c = |
257 |
|
subclass.getDeclaredConstructor((Class<?>[])null); |
258 |
c.setAccessible(true); |
c.setAccessible(true); |
259 |
// Create a new instance, to make sure that we can, and |
// Create a new instance, to make sure that we can, and |
260 |
// to cause any ClassCastException. |
// to cause any ClassCastException. |
580 |
*/ |
*/ |
581 |
protected Object readResolve() throws ObjectStreamException |
protected Object readResolve() throws ObjectStreamException |
582 |
{ |
{ |
583 |
AWTKeyStroke s = (AWTKeyStroke) cache.get(this); |
AWTKeyStroke s = cache.get(this); |
584 |
if (s != null) |
if (s != null) |
585 |
return s; |
return s; |
586 |
cache.put(this, this); |
cache.put(this, this); |