/** * Returns the single element contained in {@code iterable}. * * <p><b>Java 8 users:</b> the {@code Stream} equivalent to this method is {@code * stream.collect(MoreCollectors.onlyElement())}. * * @throws NoSuchElementException if the iterable is empty * @throws IllegalArgumentException if the iterable contains multiple elements */ public static <T> T getOnlyElement(Iterable<T> iterable) { return Iterators.getOnlyElement(iterable.iterator()); }
@Nullable private InputRow parseMap(@Nullable Map<String, Object> theMap) { // If a header is present in the data (and with proper configurations), a null is returned if (theMap == null) { return null; } return Iterators.getOnlyElement(mapParser.parseBatch(theMap).iterator()); } }
/** * Returns the single element contained in {@code iterable}, or {@code defaultValue} if the * iterable is empty. * * <p><b>Java 8 users:</b> the {@code Stream} equivalent to this method is {@code * stream.collect(MoreCollectors.toOptional()).orElse(defaultValue)}. * * @throws IllegalArgumentException if the iterator contains multiple elements */ public static <T> @Nullable T getOnlyElement( Iterable<? extends T> iterable, @Nullable T defaultValue) { return Iterators.getOnlyElement(iterable.iterator(), defaultValue); }
@Nullable @Override public InputRow nextRow() { Map<String, Object> mapToParse = lineIterator.next(); return (InputRow) Iterators.getOnlyElement(parser.parseBatch(mapToParse).iterator()); }
/** * Returns the single element contained in {@code iterator}, or {@code defaultValue} if the * iterator is empty. * * @throws IllegalArgumentException if the iterator contains multiple elements. The state of the * iterator is unspecified. */ @CanIgnoreReturnValue // TODO(kak): Consider removing this? public static <T> @Nullable T getOnlyElement( Iterator<? extends T> iterator, @Nullable T defaultValue) { return iterator.hasNext() ? getOnlyElement(iterator) : defaultValue; }
public void testGetOnlyElement_withDefault_singleton() { Iterator<String> iterator = Collections.singletonList("foo").iterator(); assertEquals("foo", Iterators.getOnlyElement(iterator, "bar")); }
public void testGetOnlyElement_noDefault_valid() { Iterator<String> iterator = Collections.singletonList("foo").iterator(); assertEquals("foo", Iterators.getOnlyElement(iterator)); }
/** * Returns the single element contained in {@code iterator}, or {@code defaultValue} if the * iterator is empty. * * @throws IllegalArgumentException if the iterator contains multiple elements. The state of the * iterator is unspecified. */ @CanIgnoreReturnValue // TODO(kak): Consider removing this? @NullableDecl public static <T> T getOnlyElement(Iterator<? extends T> iterator, @NullableDecl T defaultValue) { return iterator.hasNext() ? getOnlyElement(iterator) : defaultValue; }
@Override public Void visitAggregation(AggregationNode node, Void context) { if (node.getPreGroupedSymbols().isEmpty()) { return null; } StreamProperties properties = derivePropertiesRecursively(node.getSource(), metadata, sesstion, types, sqlParser); List<LocalProperty<Symbol>> desiredProperties = ImmutableList.of(new GroupingProperty<>(node.getPreGroupedSymbols())); Iterator<Optional<LocalProperty<Symbol>>> matchIterator = LocalProperties.match(properties.getLocalProperties(), desiredProperties).iterator(); Optional<LocalProperty<Symbol>> unsatisfiedRequirement = Iterators.getOnlyElement(matchIterator); checkArgument(!unsatisfiedRequirement.isPresent(), "Streaming aggregation with input not grouped on the grouping keys"); return null; } }
private static String getFullyQualifiedName(JCCompilationUnit tree) { Iterator<JCClassDecl> types = Iterables.filter(tree.getTypeDecls(), JCClassDecl.class).iterator(); if (types.hasNext()) { return Iterators.getOnlyElement(types).sym.getQualifiedName().toString(); } // Fallback: if no class is declared, then assume we're looking at a `package-info.java`. return tree.getPackage().packge.package_info.toString(); }
public void testGetOnlyElement_withDefault_two() { Iterator<String> iterator = asList("foo", "bar").iterator(); try { Iterators.getOnlyElement(iterator, "x"); fail(); } catch (IllegalArgumentException expected) { assertThat(expected).hasMessageThat().isEqualTo("expected one element but was: <foo, bar>"); } }
public void testGetOnlyElement_noDefault_moreThanOneLessThanFiveElements() { Iterator<String> iterator = asList("one", "two").iterator(); try { Iterators.getOnlyElement(iterator); fail(); } catch (IllegalArgumentException expected) { assertThat(expected).hasMessageThat().isEqualTo("expected one element but was: <one, two>"); } }
public void testGetOnlyElement_noDefault_fiveElements() { Iterator<String> iterator = asList("one", "two", "three", "four", "five").iterator(); try { Iterators.getOnlyElement(iterator); fail(); } catch (IllegalArgumentException expected) { assertThat(expected) .hasMessageThat() .isEqualTo("expected one element but was: <one, two, three, four, five>"); } }
public void testGetOnlyElement_noDefault_moreThanFiveElements() { Iterator<String> iterator = asList("one", "two", "three", "four", "five", "six").iterator(); try { Iterators.getOnlyElement(iterator); fail(); } catch (IllegalArgumentException expected) { assertThat(expected) .hasMessageThat() .isEqualTo("expected one element but was: <one, two, three, four, five, ...>"); } }
public void testGetOnlyElement_noDefault_empty() { Iterator<String> iterator = Iterators.emptyIterator(); try { Iterators.getOnlyElement(iterator); fail(); } catch (NoSuchElementException expected) { } }
public void testGetOnlyElement_withDefault_empty() { Iterator<String> iterator = Iterators.emptyIterator(); assertEquals("bar", Iterators.getOnlyElement(iterator, "bar")); }
public void testGetOnlyElement_withDefault_empty_null() { Iterator<String> iterator = Iterators.emptyIterator(); assertNull(Iterators.getOnlyElement(iterator, null)); }
@Test public void testProcessWithDictionary() { PageProcessor processor = newPageProcessor(); Page page = createPage(types, true); Page outputPage = getOnlyElement( processor.process( SESSION, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)) .orElseThrow(() -> new AssertionError("page is not present")); assertPageEquals(types, outputPage, page); }
@Test public void testProcess() { PageProcessor processor = newPageProcessor(); Page page = createPage(types, false); Page outputPage = getOnlyElement( processor.process( SESSION, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)) .orElseThrow(() -> new AssertionError("page is not present")); assertPageEquals(types, outputPage, page); }
@Test public void testSanityColumnarDictionary() { PageProcessor processor = compiler.compilePageProcessor(Optional.empty(), ImmutableList.of(field(0, VARCHAR)), MAX_BATCH_SIZE).get(); Page page = new Page(createDictionaryBlock(createExpectedValues(10), 100)); Page outputPage = getOnlyElement( processor.process( null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)) .orElseThrow(() -> new AssertionError("page is not present")); assertEquals(outputPage.getPositionCount(), 100); assertTrue(outputPage.getBlock(0) instanceof DictionaryBlock); DictionaryBlock dictionaryBlock = (DictionaryBlock) outputPage.getBlock(0); assertEquals(dictionaryBlock.getDictionary().getPositionCount(), 10); }