/** * If the instance is provided by the user, {@link Closeable#close()} * will not be invoked. */ protected void releaseInstance(T instance) throws IOException { AtomicInteger currentCount = providedVsCount.get(instance); if(currentCount != null) { if(currentCount.decrementAndGet() < 0) { currentCount.incrementAndGet(); throw new IllegalArgumentException("Given instance of " + instance.getClass().getName() + " is not managed by this provider"); } } else { throw new IllegalArgumentException("Given instance of " + instance.getClass().getName() + " is not managed by this provider"); } if(instantiated.remove(instance) && instance instanceof Closeable) { ((Closeable)instance).close(); } }
@Override public void releaseSerializer(final Serializer<?> serializer) throws IOException { AtomicInteger currentCount = providedVsCount.get(serializer); if(currentCount != null) { if(currentCount.decrementAndGet() < 0) { currentCount.incrementAndGet(); throw new IllegalArgumentException("Given serializer:" + serializer.getClass().getName() + " is not managed by this provider"); } } else { throw new IllegalArgumentException("Given serializer:" + serializer.getClass().getName() + " is not managed by this provider"); } if(instantiated.remove(serializer)) { if (serializer instanceof Closeable) { ((Closeable) serializer).close(); } } }
@Test public void testSameInstanceRetrievedMultipleTimesUpdatesTheProvidedCount() throws Exception { DefaultSerializationProvider provider = new DefaultSerializationProvider(null); @SuppressWarnings("unchecked") TestSerializer<String> serializer = mock(TestSerializer.class); DefaultSerializerConfiguration<String> config = new DefaultSerializerConfiguration<>(serializer, DefaultSerializerConfiguration.Type.KEY); Serializer<?> created = provider.createKeySerializer(TestSerializer.class, getSystemClassLoader(), config); assertSame(serializer, created); assertThat(provider.providedVsCount.get(created).get(), is(1)); created = provider.createKeySerializer(TestSerializer.class, getSystemClassLoader(), config); assertSame(serializer, created); assertThat(provider.providedVsCount.get(created).get(), is(2)); }
@Test public void testSameInstanceRetrievedMultipleTimesUpdatesTheProvidedCount() throws Exception { ClassInstanceProvider<String, ClassInstanceConfiguration<TestService>, TestService> classInstanceProvider = new ClassInstanceProvider<>(null, configClass); TestService service = new TestService(); TestServiceConfiguration config = new TestServiceConfiguration(service); TestService newService = classInstanceProvider.newInstance("test stuff", config); assertThat(newService, sameInstance(service)); assertThat(classInstanceProvider.providedVsCount.get(service).get(), is(1)); newService = classInstanceProvider.newInstance("test stuff", config); assertThat(newService, sameInstance(service)); assertThat(classInstanceProvider.providedVsCount.get(service).get(), is(2)); }
@Test(expected = IllegalArgumentException.class) public void testReleaseSameInstanceMultipleTimesThrows() throws Exception { DefaultSerializationProvider provider = new DefaultSerializationProvider(null); Serializer<?> serializer = mock(Serializer.class); provider.providedVsCount.put(serializer, new AtomicInteger(1)); provider.releaseSerializer(serializer); assertThat(provider.providedVsCount.get("foo"), Matchers.nullValue()); provider.releaseSerializer(serializer); }
@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()); }
@Override public void releaseSerializer(final Serializer<?> serializer) throws IOException { AtomicInteger currentCount = providedVsCount.get(serializer); if(currentCount != null) { if(currentCount.decrementAndGet() < 0) { currentCount.incrementAndGet(); throw new IllegalArgumentException("Given serializer:" + serializer.getClass().getName() + " is not managed by this provider"); } } else { throw new IllegalArgumentException("Given serializer:" + serializer.getClass().getName() + " is not managed by this provider"); } if(instantiated.remove(serializer)) { if (serializer instanceof Closeable) { ((Closeable) serializer).close(); } } }
/** * If the instance is provided by the user, {@link Closeable#close()} * will not be invoked. */ protected void releaseInstance(T instance) throws IOException { AtomicInteger currentCount = providedVsCount.get(instance); if(currentCount != null) { if(currentCount.decrementAndGet() < 0) { currentCount.incrementAndGet(); throw new IllegalArgumentException("Given instance of " + instance.getClass().getName() + " is not managed by this provider"); } } else { throw new IllegalArgumentException("Given instance of " + instance.getClass().getName() + " is not managed by this provider"); } if(instantiated.remove(instance) && instance instanceof Closeable) { ((Closeable)instance).close(); } }