ToArrayHandler implements OutputOperationHandler<ToArray<T>, T[]> { @SuppressFBWarnings(value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS") @Override public T[] doOperation(final ToArray<T> operation, final Context context, final Store store) throws OperationException { if (null == operation.getInput() || Iterables.isEmpty(operation.getInput())) { return null; } final Set<Class> classes = new HashSet<>(); final Collection<T> collection; if (operation.getInput() instanceof Collection) { collection = (Collection) operation.getInput(); collection.stream() .filter(Objects::nonNull) .forEach(e -> classes.add(e.getClass())); } else { collection = new ArrayList<>(); for (final T t : operation.getInput()) { if (null != t) { classes.add(t.getClass()); } collection.add(t); } } if (classes.isEmpty()) { return null; } final Class clazz; if (1 == classes.size()) { clazz = classes.iterator().next(); } else { if (classes.remove(Edge.class)) { classes.add(Element.class); } if (classes.remove(Entity.class)) { classes.add(Element.class); } if (classes.remove(EntitySeed.class)) { classes.add(EntityId.class); } if (classes.remove(EdgeSeed.class)) { classes.add(EdgeId.class); } if (1 == classes.size()) { clazz = classes.iterator().next(); } else { if (classes.remove(Element.class)) { classes.add(ElementId.class); } if (classes.remove(EdgeId.class)) { classes.add(ElementId.class); } if (classes.remove(EntityId.class)) { classes.add(ElementId.class); } if (1 == classes.size()) { clazz = classes.iterator().next(); } else { clazz = Object.class; } } } return collection.toArray((T[]) Array.newInstance(clazz, collection.size())); }  @SuppressFBWarnings(value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS") @Override T[] doOperation(final ToArray<T> operation, final Context context, final Store store);  }
@Test public void shouldConvertIterableToArray() throws OperationException { final Integer[] originalArray = new Integer[]{1, 2, 3}; final Iterable<Integer> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<Integer> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final Integer[] results = handler.doOperation(operation, new Context(), null); assertArrayEquals(originalArray, results); }
@Test public void shouldConvertIterableOfElementsToArray() throws OperationException { final Element[] originalArray = new Element[]{ new Entity.Builder() .group("entity") .build(), new Edge.Builder() .group("edge") .build() }; final Iterable<Element> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<Element> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final Element[] results = handler.doOperation(operation, new Context(), null); assertArrayEquals(originalArray, results); }
@Test public void shouldConvertIterableOfElementIdsToArray() throws OperationException { final ElementId[] originalArray = new ElementId[]{new EntitySeed("vertex"), new EdgeSeed("src", "dest", true)}; final Iterable<ElementId> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<ElementId> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final ElementId[] results = handler.doOperation(operation, new Context(), null); assertArrayEquals(originalArray, results); }
@Test public void shouldConvertIterableOfElementsAndElementIdsToArray() throws OperationException { final ElementId[] originalArray = new ElementId[]{ new Entity.Builder() .group("entity") .build(), new Edge.Builder() .group("edge") .build(), new EntitySeed("vertex"), new EdgeSeed("src", "dest", true) }; final Iterable<ElementId> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<ElementId> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final ElementId[] results = handler.doOperation(operation, new Context(), null); assertArrayEquals(originalArray, results); }
@Test public void shouldConvertIterableOfObjectsToArray() throws OperationException { final Object[] originalArray = new Object[]{ new Entity("entity"), new Edge.Builder().group("edge"), new EntitySeed("vertex"), new EdgeSeed("src", "dest", true), 1, 2, 1.5 }; final Iterable<Object> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<Object> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final Object[] results = handler.doOperation(operation, new Context(), null); assertArrayEquals(originalArray, results); }
@Test public void shouldConvertEmptyIterableOfObjectsToNullArray() throws OperationException { final Object[] originalArray = new Object[0]; final Iterable<Object> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<Object> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final Object[] results = handler.doOperation(operation, new Context(), null); assertNull(results); }
@Test public void shouldHandleNullInput() throws OperationException { final ToArrayHandler<Integer> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(null); final Integer[] results = handler.doOperation(operation, new Context(), null); assertThat(results, is(nullValue())); }
@Test public void shouldHandleZeroLengthInput() throws OperationException { final Integer[] originalArray = new Integer[]{}; final Iterable<Integer> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray)); final ToArrayHandler<Integer> handler = new ToArrayHandler<>(); final ToArray operation = mock(ToArray.class); given(operation.getInput()).willReturn(originalResults); final Integer[] results = handler.doOperation(operation, new Context(), null); assertThat(results, is(nullValue())); }