/** * Creates a new factory. * * @param loggerClass The class of the wrapped logger. */ protected LoggerFactory(final Class<L> loggerClass) { this.loggerClass = loggerClass; loggers = new WeakValueHashMap<>(String.class); }
/** * Adds the given {@code key}, {@code value} pair in the map of user-defined values, provided that no value * is currently associated to the given key. This method shall be invoked only after the {@link Units} class * has been fully initialized. */ static Object putIfAbsent(final Object key, final Object value) { assert Units.initialized : value; Object previous = HARD_CODED.get(key); if (previous == null) { previous = USER_DEFINED.putIfAbsent(key, value); } return previous; }
/** * Creates a coordinate reference system from the given {@literal Proj.4} definition string. * The {@code Proj4} suffix shall have been removed before to invoke this method. * * @param definition the Proj.4 definition. * @param withHeight whether to include a height axis. * * @see Proj4#createCRS(String, int) * @see #createCoordinateReferenceSystem(String) */ final CoordinateReferenceSystem createCRS(final String definition, final boolean withHeight) throws FactoryException { PJ pj = pool.get(definition); if (pj == null) { pj = unique(new PJ(definition)); pool.putIfAbsent(definition, pj); } return createCRS(pj, withHeight); }
/** * Tests with array keys. */ @Test @DependsOnMethod("testStrongReferences") public void testWithArrayKeys() { final WeakValueHashMap<int[],Integer> weakMap = new WeakValueHashMap<>(int[].class); final int[] k1 = new int[] {2, 5, 3}; final int[] k2 = new int[] {2, 5, 4}; final Integer v1 = 1; final Integer v2 = 2; assertNull ( weakMap.put(k1, v1)); assertSame (v1, weakMap.put(k1, v1)); assertSame (v1, weakMap.put(k1.clone(), v1)); assertNull ( weakMap.put(k2, v2)); assertSame (v2, weakMap.put(k2, v2)); assertSame (v1, weakMap.get(k1)); assertSame (v2, weakMap.get(k2)); }
/** * Returns {@code true} if this map contains a mapping for the specified key. * Null keys are considered never present. * * @param key key whose presence in this map is to be tested. * @return {@code true} if this map contains a mapping for the specified key. */ @Override public boolean containsKey(final Object key) { return get(key) != null; }
/** * Removes the mapping for this key from this map if present. * * @param key key whose mapping is to be removed from the map. * @return previous value associated with specified key, or {@code null} if there was no entry for key. */ @Override public V remove(final Object key) { return intern(key, null, true); }
/** * Creates a coordinate reference system from the given {@literal Proj.4} definition string. * The {@code Proj4} suffix shall have been removed before to invoke this method. * * @param definition the Proj.4 definition. * @param withHeight whether to include a height axis. * * @see Proj4#createCRS(String, int) * @see #createCoordinateReferenceSystem(String) */ final CoordinateReferenceSystem createCRS(final String definition, final boolean withHeight) throws FactoryException { PJ pj = pool.get(definition); if (pj == null) { pj = unique(new PJ(definition)); pool.putIfAbsent(definition, pj); } return createCRS(pj, withHeight); }
/** * Tests using identity comparisons. This test uses two {@link Integer} keys having the same value * but being different instances. * * @since 0.4 */ @Test @DependsOnMethod("testStrongReferences") @SuppressWarnings("UnnecessaryBoxing") public void testIdentityComparisons() { final WeakValueHashMap<Integer,Integer> weakMap = new WeakValueHashMap<>(Integer.class, true); final Integer k1 = 10; final Integer k2 = 20; final Integer k3 = new Integer(10); // Really want a new instance. final Integer v1 = 1; final Integer v2 = 2; final Integer v3 = 3; assertEquals(k1, k3); // Necessary condition for the test to be valid. assertNull(weakMap.put(k1, v1)); assertSame(v1, weakMap.put(k1, v1)); assertNull(weakMap.put(k2, v2)); assertSame(v2, weakMap.put(k2, v2)); assertNull(weakMap.put(k3, v3)); assertSame(v3, weakMap.put(k3, v3)); assertSame(v1, weakMap.get(k1)); assertSame(v2, weakMap.get(k2)); assertSame(v3, weakMap.get(k3)); } }
/** * Returns {@code true} if this map contains a mapping for the specified key. * Null keys are considered never present. * * @param key key whose presence in this map is to be tested. * @return {@code true} if this map contains a mapping for the specified key. */ @Override public boolean containsKey(final Object key) { return get(key) != null; }
/** * Removes the mapping for this key from this map if present. * * @param key key whose mapping is to be removed from the map. * @return previous value associated with specified key, or {@code null} if there was no entry for key. */ @Override public V remove(final Object key) { return intern(key, null, true); }
/** * Creates a new map or return an existing map for the given attribute characteristics. * * <p>This method does not clone the {@code characterizedBy} array. If that array * is a user-provided argument, then cloning that array is caller responsibility.</p> * * @param source the attribute which is characterized by {@code characterizedBy}. * @param characterizedBy characteristics of {@code source}. Should not be empty. * @return a map for this given characteristics. * @throws IllegalArgumentException if two characteristics have the same name. */ static CharacteristicTypeMap create(final DefaultAttributeType<?> source, final DefaultAttributeType<?>[] characterizedBy) { CharacteristicTypeMap map; synchronized (SHARED) { map = SHARED.get(characterizedBy); if (map == null) { map = new CharacteristicTypeMap(source, characterizedBy); SHARED.put(characterizedBy, map); } } return map; }
Object existing = instances.get(key); if (existing != null) { return existing; final Object previous = ((WeakValueHashMap) instances).putIfAbsent(key, feature); if (previous != null) { feature = previous;
/** * Initializes the transient fields. */ private void init() { childs = new WeakValueHashMap<>(String.class); }
/** * Returns the value associated to the given key, or {@code null} if none. * This method can be invoked at anytime (at {@link Units} class initialization time or not). */ static Object get(final Object key) { Object value = HARD_CODED.get(key); // Treated as immutable, no synchronization needed. if (value == null) { value = USER_DEFINED.get(key); // Implies a synchronization lock. } return value; }
/** * Adds the given {@code key}, {@code value} pair in the map of user-defined values, provided that no value * is currently associated to the given key. This method shall be invoked only after the {@link Units} class * has been fully initialized. */ static Object putIfAbsent(final Object key, final Object value) { assert Units.initialized : value; Object previous = HARD_CODED.get(key); if (previous == null) { previous = USER_DEFINED.putIfAbsent(key, value); } return previous; }
/** * Associates the specified value with the specified key in this map. * The value is associated using a {@link WeakReference}. * * @param key key with which the specified value is to be associated. * @param value value to be associated with the specified key. * @return the previous value associated with specified key, or {@code null} if there was no mapping for key. * * @throws NullArgumentException if the key or the value is {@code null}. */ @Override public V put(final K key, final V value) throws NullArgumentException { if (key == null || value == null) { throw new NullArgumentException(Errors.format(key == null ? Errors.Keys.NullMapKey : Errors.Keys.NullMapValue)); } return intern(key, value, true); }
/** * Creates a new map or return an existing map for the given attribute characteristics. * * <p>This method does not clone the {@code characterizedBy} array. If that array * is a user-provided argument, then cloning that array is caller responsibility.</p> * * @param source the attribute which is characterized by {@code characterizedBy}. * @param characterizedBy characteristics of {@code source}. Should not be empty. * @return a map for this given characteristics. * @throws IllegalArgumentException if two characteristics have the same name. */ static CharacteristicTypeMap create(final DefaultAttributeType<?> source, final DefaultAttributeType<?>[] characterizedBy) { CharacteristicTypeMap map; synchronized (SHARED) { map = SHARED.get(characterizedBy); if (map == null) { map = new CharacteristicTypeMap(source, characterizedBy); SHARED.put(characterizedBy, map); } } return map; }