private void updateProvidedInstanceCounts(Serializer<?> serializer) { AtomicInteger currentCount = providedVsCount.putIfAbsent(serializer, new AtomicInteger(1)); if(currentCount != null) { currentCount.incrementAndGet(); } }
private T newInstance(K alias, ClassInstanceConfiguration<? extends T> config) { if (config == null) { config = getPreconfigured(alias); if (config == null) { return null; } } T instance; if(config.getInstance() != null) { instance = config.getInstance(); } else { try { instance = invokeConstructor(config.getClazz(), config.getArguments()); instantiated.add(instance); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); } } AtomicInteger currentCount = providedVsCount.putIfAbsent(instance, new AtomicInteger(1)); if(currentCount != null) { currentCount.incrementAndGet(); } return instance; }
@Test public void testBasicOperations() { final ConcurrentWeakIdentityHashMap<Integer, String> map = new ConcurrentWeakIdentityHashMap<>(); final Integer key = 1; final String firstValue = "foo"; final String otherValue = "bar"; assertThat(map.containsKey(key), is(false)); assertThat(map.get(key), nullValue()); assertThat(map.put(key, firstValue), nullValue()); assertThat(map.containsKey(key), is(true)); assertThat(map.putIfAbsent(key, otherValue), is(firstValue)); assertThat(map.replace(key, otherValue, firstValue), is(false)); assertThat(map.get(key), is(firstValue)); assertThat(map.replace(key, firstValue, otherValue), is(true)); assertThat(map.get(key), is(otherValue)); assertThat(map.remove(key, firstValue), is(false)); assertThat(map.get(key), is(otherValue)); assertThat(map.containsKey(key), is(true)); assertThat(map.remove(key, otherValue), is(true)); assertThat(map.containsKey(key), is(false)); assertThat(map.get(key), nullValue()); assertThat(map.putIfAbsent(key, otherValue), nullValue()); assertThat(map.get(key), is(otherValue)); assertThat(map.remove(key), is(otherValue)); assertThat(map.containsKey(key), is(false)); assertThat(map.get(key), nullValue()); }
private void updateProvidedInstanceCounts(Serializer<?> serializer) { AtomicInteger currentCount = providedVsCount.putIfAbsent(serializer, new AtomicInteger(1)); if(currentCount != null) { currentCount.incrementAndGet(); } }
private T newInstance(K alias, ClassInstanceConfiguration<T> config) { if (config == null) { config = getPreconfigured(alias); if (config == null) { return null; } } T instance; if(config.getInstance() != null) { instance = config.getInstance(); } else { try { instance = invokeConstructor(config.getClazz(), config.getArguments()); instantiated.add(instance); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); } } AtomicInteger currentCount = providedVsCount.putIfAbsent(instance, new AtomicInteger(1)); if(currentCount != null) { currentCount.incrementAndGet(); } return instance; }