@Test public void getMatchingContainer () { String randomUUID = UUID.randomUUID().toString(); String randomUUID2 = UUID.randomUUID().toString(); containerManager.offer(testContainerClass); doReturn(true).when(testContainerClass).compareUniqueIdentifier(randomUUID); assertSame(testContainerClass, containerManager.getMatchingContainer(TestContainerClass.class, randomUUID)); verify(testContainerClass, times(1)).compareUniqueIdentifier(randomUUID); verify(testContainerClass2, times(0)).compareUniqueIdentifier(randomUUID); reset(testContainerClass, testContainerClass2); doReturn(false).when(testContainerClass).compareUniqueIdentifier(randomUUID2); doReturn(true).when(testContainerClass2).compareUniqueIdentifier(randomUUID2); assertNull(containerManager.getMatchingContainer(TestContainerClass.class, randomUUID2)); verify(testContainerClass, times(1)).compareUniqueIdentifier(randomUUID2); verify(testContainerClass2, times(0)).compareUniqueIdentifier(randomUUID2); }
public <T extends Container> T getMatchingContainer (Class<T> clazz, String uniqueIdentifier) { Optional<T> match; synchronized (containers) { match = containers.stream() .filter(clazz::isInstance) .map(clazz::cast) .filter(container -> container.compareUniqueIdentifier(uniqueIdentifier)) .findFirst(); } return match.orElse(null); }
ContainerManager { public <T extends Container> T getMatchingContainer (Class<T> clazz, String uniqueIdentifier) { Optional<T> match; synchronized (containers) { match = containers.stream() .filter(clazz::isInstance) .map(clazz::cast) .filter(container -> container.compareUniqueIdentifier(uniqueIdentifier)) .findFirst(); } return match.orElse(null); } }
ContainerManager { public <T extends Container> T getMatchingContainer (Class<T> clazz, String uniqueIdentifier) { Optional<T> match; synchronized (containers) { match = containers.stream() .filter(clazz::isInstance) .map(clazz::cast) .filter(container -> container.compareUniqueIdentifier(uniqueIdentifier)) .findFirst(); } return match.orElse(null); }  }
ContainerManager { public <T extends Container> T getMatchingContainer (Class<T> clazz, String uniqueIdentifier) { Optional<T> match; synchronized (containers) { match = containers.stream() .filter(clazz::isInstance) .map(clazz::cast) .filter(container -> container.compareUniqueIdentifier(uniqueIdentifier)) .findFirst(); } return match.orElse(null); }  T getMatchingContainer(Class<T> clazz, String uniqueIdentifier); void offer(Container container); }
ContainerManager { public <T extends Container> T getMatchingContainer (Class<T> clazz, String uniqueIdentifier) { Optional<T> match; synchronized (containers) { match = containers.stream() .filter(clazz::isInstance) .map(clazz::cast) .filter(container -> container.compareUniqueIdentifier(uniqueIdentifier)) .findFirst(); } return match.orElse(null); }  T getMatchingContainer(Class<T> clazz, String uniqueIdentifier); void offer(Container container);  }