< prev index next >

src/java.base/share/classes/jdk/internal/util/ReferencedKeyMap.java

Print this page

  1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any

 31 import java.lang.ref.WeakReference;
 32 import java.util.AbstractMap;
 33 import java.util.Collection;
 34 import java.util.HashMap;
 35 import java.util.Objects;
 36 import java.util.Map;
 37 import java.util.Set;
 38 import java.util.concurrent.ConcurrentHashMap;
 39 import java.util.function.Supplier;
 40 import java.util.function.UnaryOperator;
 41 import java.util.stream.Collectors;
 42 import java.util.stream.Stream;
 43 
 44 import jdk.internal.access.SharedSecrets;
 45 
 46 /**
 47  * This class provides management of {@link Map maps} where it is desirable to
 48  * remove entries automatically when the key is garbage collected. This is
 49  * accomplished by using a backing map where the keys are either a
 50  * {@link WeakReference} or a {@link SoftReference}.

 51  * <p>
 52  * To create a {@link ReferencedKeyMap} the user must provide a {@link Supplier}
 53  * of the backing map and whether {@link WeakReference} or
 54  * {@link SoftReference} is to be used.
 55  *
 56  * {@snippet :
 57  * // Use HashMap and WeakReference
 58  * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
 59  * map.put(10_000_000L, "a");
 60  * map.put(10_000_001L, "b");
 61  * map.put(10_000_002L, "c");
 62  * map.put(10_000_003L, "d");
 63  * map.put(10_000_004L, "e");
 64  *
 65  * // Use ConcurrentHashMap and SoftReference
 66  * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
 67  * map.put(20_000_000L, "v");
 68  * map.put(20_000_001L, "w");
 69  * map.put(20_000_002L, "x");
 70  * map.put(20_000_003L, "y");

  1 /*
  2  * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any

 31 import java.lang.ref.WeakReference;
 32 import java.util.AbstractMap;
 33 import java.util.Collection;
 34 import java.util.HashMap;
 35 import java.util.Objects;
 36 import java.util.Map;
 37 import java.util.Set;
 38 import java.util.concurrent.ConcurrentHashMap;
 39 import java.util.function.Supplier;
 40 import java.util.function.UnaryOperator;
 41 import java.util.stream.Collectors;
 42 import java.util.stream.Stream;
 43 
 44 import jdk.internal.access.SharedSecrets;
 45 
 46 /**
 47  * This class provides management of {@link Map maps} where it is desirable to
 48  * remove entries automatically when the key is garbage collected. This is
 49  * accomplished by using a backing map where the keys are either a
 50  * {@link WeakReference} or a {@link SoftReference}.
 51  * Keys must be {@linkplain Class#isIdentity() identity objects.}
 52  * <p>
 53  * To create a {@link ReferencedKeyMap} the user must provide a {@link Supplier}
 54  * of the backing map and whether {@link WeakReference} or
 55  * {@link SoftReference} is to be used.
 56  *
 57  * {@snippet :
 58  * // Use HashMap and WeakReference
 59  * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
 60  * map.put(10_000_000L, "a");
 61  * map.put(10_000_001L, "b");
 62  * map.put(10_000_002L, "c");
 63  * map.put(10_000_003L, "d");
 64  * map.put(10_000_004L, "e");
 65  *
 66  * // Use ConcurrentHashMap and SoftReference
 67  * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
 68  * map.put(20_000_000L, "v");
 69  * map.put(20_000_001L, "w");
 70  * map.put(20_000_002L, "x");
 71  * map.put(20_000_003L, "y");
< prev index next >