ToSetHandler implements OutputOperationHandler<ToSet<T>, Set<? extends T>> { @Override public Set<T> doOperation(final ToSet<T> operation, final Context context, final Store store) throws OperationException { if (null == operation.getInput()) { return null; } return Streams.toStream(operation.getInput()) .collect(GafferCollectors.toLinkedHashSet()); }  @Override Set<T> doOperation(final ToSet<T> operation, final Context context, final Store store);  }
@Test public void shouldConvertIterableToSet() throws OperationException { final Iterable originalResults = new WrappedCloseableIterable<>(Arrays.asList(1, 2, 2, 2, 3, 4, 1, 5, 6, 7, 8, 5, 9, 1, 6, 8, 2, 10)); final ToSetHandler<Integer> handler = new ToSetHandler<>(); final ToSet<Integer> operation = mock(ToSet.class); given(operation.getInput()).willReturn(originalResults); final Iterable<Integer> results = handler.doOperation(operation, new Context(), null); assertEquals(Sets.newHashSet(originalResults), Sets.newHashSet(results)); }
@Test public void shouldConvertIterableToSetAndMaintainOrder() throws OperationException { final Iterable originalResults = new WrappedCloseableIterable<>(Arrays.asList(10, 9, 8, 10, 7, 8, 7, 6, 6, 5, 6, 9, 4, 5, 3, 4, 2, 2, 2, 1, 1)); final ToSetHandler<Integer> handler = new ToSetHandler<>(); final ToSet<Integer> operation = mock(ToSet.class); given(operation.getInput()).willReturn(originalResults); final Iterable<Integer> results = handler.doOperation(operation, new Context(), null); assertEquals(Sets.newHashSet(10, 9, 8, 7, 6, 5, 4, 3, 2, 1), Sets.newHashSet(results)); }
@Test public void shouldHandleNullInput() throws OperationException { final ToSetHandler<Integer> handler = new ToSetHandler<>(); final ToSet<Integer> operation = mock(ToSet.class); given(operation.getInput()).willReturn(null); final Iterable<Integer> results = handler.doOperation(operation, new Context(), null); assertThat(results, is(nullValue())); }