@Test void get() { LazyMap<Integer, Integer> map = new LazyMap<>(30, this::multiplyTwo); Assertions.assertEquals(2, map.get(1)); Assertions.assertEquals(1, this.multiplyTwoSollicitation); Assertions.assertEquals(2, map.get(1)); Assertions.assertEquals(1, this.multiplyTwoSollicitation); Assertions.assertEquals(8, map.get(4)); Assertions.assertEquals(2, this.multiplyTwoSollicitation); Assertions.assertEquals(null, map.get(0)); Assertions.assertEquals(3, this.multiplyTwoSollicitation); Assertions.assertEquals(null, map.get(0)); Assertions.assertEquals(4, this.multiplyTwoSollicitation); }
@Override public B get(final Object key) { B value = super.get(key); if (value == null) { final A castedKey = (A) (key); synchronized (this) { value = super.get(key); if (value == null) { final B created = lazyFactory.apply(castedKey); if (created != null) { put(castedKey, created); return created; } } } } return value; }
LazyMap extends ConcurrentHashMap<A, B> { @Override public B get(final Object key) { B value = super.get(key); if (value == null) { final A castedKey = (A) (key); synchronized (this) { value = super.get(key); if (value == null) { final B created = lazyFactory.apply(castedKey); if (created != null) { put(castedKey, created); return created; } } } } return value; } }
LazyMap extends ConcurrentHashMap<A, B> { @Override public B get(final Object key) { B value = super.get(key); if (value == null) { final A castedKey = (A) (key); synchronized (this) { value = super.get(key); if (value == null) { final B created = lazyFactory.apply(castedKey); if (created != null) { put(castedKey, created); return created; } } } } return value; } LazyMap(final int capacity, final Function<A, B> lazyFactory); }
LazyMap extends ConcurrentHashMap<A, B> { @Override public B get(final Object key) { B value = super.get(key); if (value == null) { final A castedKey = (A) (key); synchronized (this) { value = super.get(key); if (value == null) { final B created = lazyFactory.apply(castedKey); if (created != null) { put(castedKey, created); return created; } } } } return value; } LazyMap(final int capacity, final Function<A, B> lazyFactory); @Override B get(final Object key); }
LazyMap extends ConcurrentHashMap<A, B> { @Override public B get(final Object key) { B value = super.get(key); if (value == null) { final A castedKey = (A) (key); synchronized (this) { value = super.get(key); if (value == null) { final B created = lazyFactory.apply(castedKey); if (created != null) { put(castedKey, created); return created; } } } } return value; } LazyMap(final int capacity, final Function<A, B> lazyFactory); @Override B get(final Object key);  }