diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /ridljar/com/sun/star/lib/util | |
parent | Initial commit. (diff) | |
download | libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.tar.xz libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ridljar/com/sun/star/lib/util')
-rw-r--r-- | ridljar/com/sun/star/lib/util/AsynchronousFinalizer.java | 117 | ||||
-rw-r--r-- | ridljar/com/sun/star/lib/util/DisposeListener.java | 34 | ||||
-rw-r--r-- | ridljar/com/sun/star/lib/util/DisposeNotifier.java | 44 | ||||
-rw-r--r-- | ridljar/com/sun/star/lib/util/NativeLibraryLoader.java | 132 | ||||
-rw-r--r-- | ridljar/com/sun/star/lib/util/StringHelper.java | 46 | ||||
-rw-r--r-- | ridljar/com/sun/star/lib/util/UrlToFileMapper.java | 94 | ||||
-rw-r--r-- | ridljar/com/sun/star/lib/util/WeakMap.java | 319 |
7 files changed, 786 insertions, 0 deletions
diff --git a/ridljar/com/sun/star/lib/util/AsynchronousFinalizer.java b/ridljar/com/sun/star/lib/util/AsynchronousFinalizer.java new file mode 100644 index 000000000..588b8fe38 --- /dev/null +++ b/ridljar/com/sun/star/lib/util/AsynchronousFinalizer.java @@ -0,0 +1,117 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +import java.util.LinkedList; + +/** + * Helper class to asynchronously execute finalize methods. + * + * <p>Current JVMs seem not to be robust against long-running finalize methods, + * in that such long-running finalize methods may lead to OutOfMemoryErrors. + * This class mitigates the problem by asynchronously shifting the bodies of + * potentially long-running finalize methods into an extra thread. Classes that + * make use of this in their finalize methods are the proxies used in the + * intra-process JNI UNO bridge and the inter-process Java URP UNO bridge (where + * in both cases finalizers lead to synchronous UNO release calls).</p> + * + * <p>Irrespective whether JVMs are getting more mature and should no longer + * have problems with long-running finalize methods, at least the JNI UNO bridge + * needs some way to stop finalization of proxies (to C++ objects) well before + * process exit, as provided by drain().</p> + */ +public final class AsynchronousFinalizer { + public AsynchronousFinalizer() { + thread = new Thread("AsynchronousFinalizer") { + @Override + public void run() { + for (;;) { + Job j; + synchronized (queue) { + for (;;) { + if (done) { + return; + } + if (!queue.isEmpty()) { + break; + } + try { + queue.wait(); + } catch (InterruptedException e) { + return; + } + } + j = queue.remove(0); + } + try { + j.run(); + } catch (Throwable e) {} + } + } + }; + thread.start(); + } + + /** + * Add a job to be executed asynchronously. + * + * <p>The run method of the given job is called exactly once. If it terminates + * abnormally by throwing any Throwable, that is ignored.</p> + * + * @param job represents the body of some finalize method; must not be null. + */ + public void add(Job job) { + synchronized (queue) { + boolean first = queue.isEmpty(); + queue.add(job); + if (first) { + queue.notify(); + } + } + } + + public void drain() throws InterruptedException { + synchronized (queue) { + done = true; + queue.notify(); + } + // tdf#123481 Only join if we are not in our own thread, else we have a deadlock + if (Thread.currentThread() != thread) + thread.join(); + } + + /** + * An interface to represent bodies of finalize methods. + * + * Similar to <code>Runnable</code>, except that the run method may throw any + * <code>Throwable</code> (which is effectively ignored by + * <code>AsynchronousFinalizer.add</code>, similar to any <code>Throwables</code> + * raised by finalize being ignored). + */ + public interface Job { + void run() throws Throwable; + } + + private final LinkedList<Job> queue = new LinkedList<Job>(); + private final Thread thread; + private boolean done = false; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/lib/util/DisposeListener.java b/ridljar/com/sun/star/lib/util/DisposeListener.java new file mode 100644 index 000000000..92d7fdc90 --- /dev/null +++ b/ridljar/com/sun/star/lib/util/DisposeListener.java @@ -0,0 +1,34 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +/** + * Counterpart to <code>DisposeNotifier</code>. + * + * @see DisposeNotifier + */ +public interface DisposeListener { + /** + * Callback fired by a <code>DisposeNotifier</code> once it is disposed. + * + * @param source the <code>DisposeNotifer</code> that fires the callback; + * will never be <code>null</code> + */ + void notifyDispose(DisposeNotifier source); +} diff --git a/ridljar/com/sun/star/lib/util/DisposeNotifier.java b/ridljar/com/sun/star/lib/util/DisposeNotifier.java new file mode 100644 index 000000000..ebe0008d0 --- /dev/null +++ b/ridljar/com/sun/star/lib/util/DisposeNotifier.java @@ -0,0 +1,44 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +/** + * Optional interface to be implemented by objects stored in a + * <code>WeakMap</code>. + * + * @see WeakMap + */ +public interface DisposeNotifier { + /** + * Adds a dispose listener, to be notified when this object is disposed. + * + * <p>It is unspecified what happens when the same listener is added + * multiple times.</p> + * + * <p>It is unspecified exactly when the <code>notifyDispose</code> callback + * is fired: immediately before the notifier is disposed, while it is in the + * process of disposing, or some time after it got disposed. But even if + * adding a listener to an already disposed notifier, the listener must + * eventually receive a <code>notifyDispose</code> callback.</p> + * + * @param listener a dispose listener, to be notified when this object is + * disposed; must not be <code>null</code> + */ + void addDisposeListener(DisposeListener listener); +} diff --git a/ridljar/com/sun/star/lib/util/NativeLibraryLoader.java b/ridljar/com/sun/star/lib/util/NativeLibraryLoader.java new file mode 100644 index 000000000..eb5c6af34 --- /dev/null +++ b/ridljar/com/sun/star/lib/util/NativeLibraryLoader.java @@ -0,0 +1,132 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.util; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; + +/** + * Helper functions to locate and load native files. + * + * <p>The methods in this class are designed to find the requested resources in + * as many cases as possible. They search various places, roughly from most + * specific to most general. This works well if a component is known to bring + * with it a certain resource, and that resource has to be found. However, it + * might not work very well in cases where you want to check whether a + * component brings with it a certain resource or not: a similarly named + * resource from another component might be found by the eager search + * algorithm.</p> + */ +public final class NativeLibraryLoader { + /** + * Load a system library, using a given class loader to locate the library. + * + * <p>This is similar to <code>System.loadLibrary</code>.</p> + * + * @param loader a class loader; may be null. + * @param libname the library name; how this name is mapped to a system + * library name is system dependent. + */ + public static void loadLibrary(ClassLoader loader, String libname) { + String sysname = System.mapLibraryName(libname); + // At least Oracle's 1.7.0_51 now maps to .dylib rather than .jnilib: + if (System.getProperty("os.name").startsWith("Mac") + && sysname.endsWith(".dylib")) + { + sysname + = sysname.substring(0, sysname.length() - "dylib".length()) + + "jnilib"; + } + File path = getResource(loader, sysname); + if (path == null) { + // If the library cannot be found as a class loader resource, try + // the global System.loadLibrary as a last resort: + System.loadLibrary(libname); + } else { + System.load(path.getAbsolutePath()); + } + } + + /** + * Locate a system resource, using a given class loader. + * + * <p>This is similar to <code>ClassLoader.getResource</code>, but only works + * for local resources (local files), and adds additional functionality for + * <code>URLClassLoaders</code>.</p> + * + * @param loader a class loader; may be null. + * @param name a resource name (that is, the name of a file). + * @return a File locating the resource, or null if the resource was not + * found. + */ + public static File getResource(ClassLoader loader, String name) { + if (loader != null) { + File path = UrlToFileMapper.mapUrlToFile(loader.getResource(name)); + if (path != null) { + return path; + } + } + // URLClassLoaders work on lists of URLs, which are typically URLs + // locating JAR files (scheme://auth/dir1/dir2/some.jar). The following + // code looks for resource name beside the JAR file + // (scheme://auth/dir1/dir2/name) and one directory up + // (scheme://auth/dir1/name). The second step is important in a typical + // OOo installation, where the JAR files are in the program/classes + // directory while the shared libraries are in the program directory. + if (!(loader instanceof URLClassLoader)) { + return null; + } + URL[] urls = ((URLClassLoader) loader).getURLs(); + for (int i = 0; i < urls.length; ++i) { + File path = UrlToFileMapper.mapUrlToFile(urls[i]); + if (path != null) { + File dir = path.isDirectory() ? path : path.getParentFile(); + if (dir != null) { + path = new File(dir, name); + if (path.exists()) { + return path; + } + dir = dir.getParentFile(); + if (dir != null) { + path = new File(dir, name); + if (path.exists()) { + return path; + } + // On macOS, dir is now the Resources dir, + // we want to look in Frameworks + if (System.getProperty("os.name").startsWith("Mac") + && dir.getName().equals("Resources")) { + dir = dir.getParentFile(); + path = new File(dir, "Frameworks/" + name); + if (path.exists()) { + return path; + } + } + } + } + } + } + return null; + } + + private NativeLibraryLoader() {} // do not instantiate +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/lib/util/StringHelper.java b/ridljar/com/sun/star/lib/util/StringHelper.java new file mode 100644 index 000000000..de8b5253a --- /dev/null +++ b/ridljar/com/sun/star/lib/util/StringHelper.java @@ -0,0 +1,46 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +/** + * jurt.jar internal string helper methods. + */ +public final class StringHelper +{ + private StringHelper() {} // do not instantiate + + public static String replace(String str, char from, String to) { + StringBuffer b = new StringBuffer(); + for (int i = 0;;) { + int j = str.indexOf(from, i); + if (j == -1) { + b.append(str.substring(i)); + break; + } else { + b.append(str.substring(i, j)); + b.append(to); + i = j + 1; + } + } + return b.toString(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/lib/util/UrlToFileMapper.java b/ridljar/com/sun/star/lib/util/UrlToFileMapper.java new file mode 100644 index 000000000..131890f54 --- /dev/null +++ b/ridljar/com/sun/star/lib/util/UrlToFileMapper.java @@ -0,0 +1,94 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.util; + +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLEncoder; + +/** + * Maps Java URL representations to File representations, on any Java version. + * + * This used to be used to do URL to File mapping in pre-Java1.4 days, but since + * we now require Java 1.5, it is largely unnecessary. + * + * @since UDK 3.2.8 + */ +public final class UrlToFileMapper { + + /** + * Maps Java URL representations to File representations. + * + * @param url some URL, possibly null. + * @return a corresponding File, or null on failure. + */ + public static File mapUrlToFile(URL url) { + if (url == null) { + return null; + } else { + try { + // where encodedUrl is url.toString(), but since that may contain + // unsafe characters (e.g., space, " "), it is encoded, as otherwise + // the URI constructor might throw java.net.URISyntaxException (in + // Java 1.5, URL.toURI might be used instead). + String encodedUrl = encode(url.toString()); + URI uri = new URI(encodedUrl); + try { + return new File(uri); + } catch (IllegalArgumentException e) { + return null; + } + } catch (URISyntaxException ex) { + throw new RuntimeException(ex); // should never happen + } + } + } + + private static String encode(String url) { + StringBuffer buf = new StringBuffer(url.length()); + for (int i = 0; i < url.length(); ++i) { + char c = url.charAt(i); + // The RFC 2732 <uric> characters: !$&'()*+,-./:;=?@[]_~ plus digits + // and letters; additionally, do not encode % again. + if (c >= 'a' && c <= 'z' || c >= '?' && c <= '[' + || c >= '$' && c <= ';' || c == '!' || c == '=' || c == ']' + || c == '_' || c == '~') + { + buf.append(c); + } else if (c == ' ') { + buf.append("%20"); + } else { + try { + String enc = URLEncoder.encode(Character.toString(c), "UTF-8"); + buf.append(enc); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); // should never happen + } + } + } + return buf.toString(); + } + + private UrlToFileMapper() {} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/lib/util/WeakMap.java b/ridljar/com/sun/star/lib/util/WeakMap.java new file mode 100644 index 000000000..f3b3b55a3 --- /dev/null +++ b/ridljar/com/sun/star/lib/util/WeakMap.java @@ -0,0 +1,319 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * A hash map that holds values of type <code>WeakReference</code>. + * + * <p>Like <code>HashMap</code>, this implementation provides all of the + * optional map operations, and permits the <code>null</code> key.</p> + * + * <p>Also like <code>HashMap</code>, this implementation is not synchronized. + * If multiple threads share an instance, and at least one of them executes any + * modifying operations on the <code>WeakMap</code>, they have to use external + * synchronization.</p> + * + * <p>Unlike other map implementations, <code>WeakMap</code> is asymmetric in + * that <code>put</code> expects the given value to be a plain object that is + * then wrapped in a <code>WeakReference</code>, while the occurrences of values + * in all other methods (<code>containsValue</code>, <code>entrySet</code>, + * <code>equals</code>, <code>get</code>, <code>hashCode</code>, + * <code>remove</code>, <code>values</code>, and also the return value of + * <code>put</code>) expect already wrapped instances of + * <code>WeakReference</code>. That is, after <code>weakMap.put("key", + * o)</code>, <code>weakMap.get("key").equals(o)</code> does not work as + * naïvely expected; neither does + * <code>weakMap1.putAll(weakMap2)</code>.</p> + * + * <p>At an arbitrary time after the <code>WeakReference</code> value of an + * entry has been cleared by the garbage collector, the entry is automatically + * removed from the map.</p> + * + * <p>Values placed into a <code>WeakMap</code> may optionally support the + * <code>DisposeNotifier</code> interface. For those that do, the associated + * <code>WeakReference</code> wrappers are automatically cleared as soon as the + * values are disposed.</p> + * + * Note that this class does not actually implement the Map interface properly, + * the type of the return value of the entrySet and values methods is wrong, + * but the "implements Map" is retained for backward compatibility. + */ +public final class WeakMap<K,V> implements Map { + + /** + * Declare the map as WeakReference instead of Entry because it makes the return + * type signatures of values() and keySet() cleaner. + */ + private final HashMap<K, WeakReference<V>> map = new HashMap<K, WeakReference<V>>(); + private final ReferenceQueue<V> queue = new ReferenceQueue<V>(); + + /** + * Constructs an empty <code>WeakMap</code>. + */ + public WeakMap() {} + + /** + * Constructs a new <code>WeakMap</code> with the same mappings as the + * specified <code>Map</code>. + * + * @param m the map whose mappings are to be placed in this map + */ + public WeakMap(Map<K,V> m) { + putAll(m); + } + + /** + * Returns the number of key–value mappings in this map. + * + * <p>This is a non-modifying operation.</p> + * + * @return the number of key–value mappings in this map + */ + public int size() { + return map.size(); + } + + /** + * Returns <code>true</code> if this map contains no key–value + * mappings. + * + * <p>This is a non-modifying operation.</p> + * + * @return <code>true</code> if this map contains no key–value + * mappings + */ + public boolean isEmpty() { + return map.isEmpty(); + } + + /** + * Returns <code>true</code> if this map contains a mapping for the + * specified key. + * + * <p>This is a non-modifying operation.</p> + * + * @param key the key whose presence in this map is to be tested + * @return <code>true</code> if this map contains a mapping for the + * specified key + */ + public boolean containsKey(/*K*/ Object key) { + return map.containsKey(key); + } + + /** + * Returns <code>true</code> if this map maps one or more keys to the + * specified value. + * + * <p>This is a non-modifying operation.</p> + * + * @param value the value whose presence in this map is to be tested + * @return <code>true</code> if this map maps one or more keys to the + * specified value + */ + public boolean containsValue(Object /*WeakReference<V>*/ value) { + return map.containsValue(value); + } + + /** + * Returns the value to which the specified key is mapped in this map, or + * <code>null</code> if the map contains no mapping for this key. + * + * <p>This is a non-modifying operation.</p> + * + * @param key the key whose associated value is to be returned + * + * @return the value to which this map maps the specified key, or + * <code>null</code> if the map contains no mapping for this key + */ + public WeakReference<V> get(/*K*/ Object key) { + return map.get(key); + } + + /** + * Associates the specified value with the specified key in this map. + * + * <p>This is a modifying operation.</p> + * + * @param key the key with which the specified value is to be associated + * @param value the value to be associated with the specified key. This + * must be a plain object, which is then wrapped in a + * <code>WeakReference</code>. + * @return previous value associated with the specified key, or + * <code>null</code> if there was no mapping for the key + */ + public Object /*WeakReference<V>*/ put(/*K*/ Object key, /*V*/ Object value) { + cleanUp(); + return map.put((K) key, new Entry<K,V>((K) key, (V) value, queue)); + } + + /** + * Removes the mapping for this key from this map if present. + * + * <p>This is a modifying operation.</p> + * + * @param key the key whose mapping is to be removed from the map + * @return previous value associated with the specified key, or + * <code>null</code> if there was no mapping for the key + */ + public Object /*WeakReference<V>*/ remove(/*K*/ Object key) { + cleanUp(); + return map.remove(key); + } + + /** + * Copies all of the mappings from the specified map to this map. + * + * <p>This is a modifying operation.</p> + * + * @param m mappings to be stored in this map. The values of those mappings + * must be plain objects, which are then wrapped in instances of + * <code>WeakReference</code>. + */ + public void putAll(Map/*<K,V>*/ m) { + cleanUp(); + for (Iterator<Map.Entry<K,V>> i = m.entrySet().iterator(); i.hasNext();) { + Map.Entry<K,V> e = i.next(); + K k = e.getKey(); + map.put(k, new Entry<K,V>(k, e.getValue(), queue)); + } + } + + /** + * Removes all mappings from this map. + * + * <p>This is a modifying operation.</p> + */ + public void clear() { + cleanUp(); + map.clear(); + } + + /** + * Returns a view of the keys contained in this map. + * + * <p>This is a non-modifying operation.</p> + * + * @return a set view of the keys contained in this map + */ + public Set<K> keySet() { + return map.keySet(); + } + + /** + * Returns a collection view of the values contained in this map. + * + * <p>This is a non-modifying operation.</p> + * + * @return a collection view of the values contained in this map + */ + public Collection<WeakReference<V>> values() { + return map.values(); + } + + /** + * Returns a collection view of the mappings contained in this map. + * + * <p>This is a non-modifying operation.</p> + * + * @return a collection view of the mappings contained in this map + */ + public Set/*<Map.Entry<K,WeakReference<V>>>*/ entrySet() { + return map.entrySet(); + } + + @Override + public boolean equals(Object o) { + return map.equals(o); + } + + @Override + public int hashCode() { + return map.hashCode(); + } + + /** + * Returns the referent of a <code>WeakReference</code>, silently handling a + * <code>null</code> argument. + * + * <p>This static method is useful to wrap around the return values of + * methods like <code>get</code>.</p> + * + * @param ref must be either an instance of <code>WeakReference</code> or + * <code>null</code> + * @return the referent of the specified <code>WeakReference</code>, or + * <code>null</code> if <code>ref</code> is <code>null</code> + */ + public static <T> T getValue(Object /*WeakReference<T>*/ ref) { + return ref == null ? null : ((WeakReference<T>) ref).get(); + } + + /** + * cleanUp() must only be called from within modifying methods. Otherwise, + * the implementations of entrySet, keySet and values would break + * (Specifically, iterating over the collections returned by those + * methods), as non-modifying methods might modify the underlying map. + **/ + private void cleanUp() { + for (;;) { + Entry<K,V> e = (Entry<K,V>) queue.poll(); + if (e == null) { + break; + } + // It is possible that an Entry e1 becomes weakly reachable, then + // another Entry e2 is added to the map for the same key, and only + // then e1 is enqueued. To not erroneously remove the new e2 in + // that case, check whether the map still contains e1: + Object k = e.key; + if (e == map.get(k)) { + map.remove(k); + } + } + } + + private static final class Entry<K,V> extends WeakReference<V> + implements DisposeListener + { + private final K key; + + private Entry(K key, V value, ReferenceQueue<V> queue) { + super(value, queue); + this.key = key; + if (value instanceof DisposeNotifier) { + ((DisposeNotifier) value).addDisposeListener(this); + } + } + + /** + * @see DisposeListener#notifyDispose(DisposeNotifier) + */ + public void notifyDispose(DisposeNotifier source) { + clear(); + enqueue(); + } + } + +} |