/*
* 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 WeakReference
.
*
*
Like HashMap
, this implementation provides all of the
* optional map operations, and permits the null
key.
Also like HashMap
, this implementation is not synchronized.
* If multiple threads share an instance, and at least one of them executes any
* modifying operations on the WeakMap
, they have to use external
* synchronization.
Unlike other map implementations, WeakMap
is asymmetric in
* that put
expects the given value to be a plain object that is
* then wrapped in a WeakReference
, while the occurrences of values
* in all other methods (containsValue
, entrySet
,
* equals
, get
, hashCode
,
* remove
, values
, and also the return value of
* put
) expect already wrapped instances of
* WeakReference
. That is, after weakMap.put("key",
* o)
, weakMap.get("key").equals(o)
does not work as
* naïvely expected; neither does
* weakMap1.putAll(weakMap2)
.
At an arbitrary time after the WeakReference
value of an
* entry has been cleared by the garbage collector, the entry is automatically
* removed from the map.
Values placed into a WeakMap
may optionally support the
* DisposeNotifier
interface. For those that do, the associated
* WeakReference
wrappers are automatically cleared as soon as the
* values are disposed.
WeakMap
.
*/
public WeakMap() {}
/**
* Constructs a new WeakMap
with the same mappings as the
* specified Map
.
*
* @param m the map whose mappings are to be placed in this map
*/
public WeakMap(MapThis is a non-modifying operation.
* * @return the number of key–value mappings in this map */ public int size() { return map.size(); } /** * Returnstrue
if this map contains no key–value
* mappings.
*
* This is a non-modifying operation.
* * @returntrue
if this map contains no key–value
* mappings
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* Returns true
if this map contains a mapping for the
* specified key.
*
* This is a non-modifying operation.
* * @param key the key whose presence in this map is to be tested * @returntrue
if this map contains a mapping for the
* specified key
*/
public boolean containsKey(/*K*/ Object key) {
return map.containsKey(key);
}
/**
* Returns true
if this map maps one or more keys to the
* specified value.
*
* This is a non-modifying operation.
* * @param value the value whose presence in this map is to be tested * @returntrue
if this map maps one or more keys to the
* specified value
*/
public boolean containsValue(Object /*WeakReferencenull
if the map contains no mapping for this key.
*
* This is a non-modifying operation.
* * @param key the key whose associated value is to be returned * * @return the value to which this map maps the specified key, or *null
if the map contains no mapping for this key
*/
public WeakReferenceThis is a modifying operation.
* * @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 *WeakReference
.
* @return previous value associated with the specified key, or
* null
if there was no mapping for the key
*/
public Object /*WeakReferenceThis is a modifying operation.
* * @param key the key whose mapping is to be removed from the map * @return previous value associated with the specified key, or *null
if there was no mapping for the key
*/
public Object /*WeakReferenceThis is a modifying operation.
* * @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 *WeakReference
.
*/
public void putAll(Map/*This is a modifying operation.
*/ public void clear() { cleanUp(); map.clear(); } /** * Returns a view of the keys contained in this map. * *This is a non-modifying operation.
* * @return a set view of the keys contained in this map */ public SetThis is a non-modifying operation.
* * @return a collection view of the values contained in this map */ public CollectionThis is a non-modifying operation.
* * @return a collection view of the mappings contained in this map */ public Set/*WeakReference
, silently handling a
* null
argument.
*
* This static method is useful to wrap around the return values of
* methods like get
.
WeakReference
or
* null
* @return the referent of the specified WeakReference
, or
* null
if ref
is null
*/
public static