/** * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order * each appears first in the source iterator. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> elements) { // We special-case for 0 or 1 elements, but anything further is madness. if (!elements.hasNext()) { return of(); } E first = elements.next(); if (!elements.hasNext()) { return of(first); } else { return new ImmutableSet.Builder<E>().add(first).addAll(elements).build(); } }
public void testReuseBuilderWithNonDuplicateElements() { ImmutableSet.Builder<String> builder = this.<String>builder().add("a").add("b"); assertThat(builder.build()).containsExactly("a", "b").inOrder(); builder.add("c", "d"); assertThat(builder.build()).containsExactly("a", "b", "c", "d").inOrder(); }
public void testReuseBuilderWithDuplicateElements() { ImmutableSet.Builder<String> builder = this.<String>builder().add("a").add("a", "a").add("b"); assertThat(builder.build()).containsExactly("a", "b").inOrder(); builder.add("a", "b", "c", "c"); assertThat(builder.build()).containsExactly("a", "b", "c").inOrder(); }
private static LibraryModels loadLibraryModels() { Iterable<LibraryModels> externalLibraryModels = ServiceLoader.load(LibraryModels.class, LibraryModels.class.getClassLoader()); ImmutableSet.Builder<LibraryModels> libModelsBuilder = new ImmutableSet.Builder<>(); libModelsBuilder.add(new DefaultLibraryModels()).addAll(externalLibraryModels); return new CombinedLibraryModels(libModelsBuilder.build()); }
public static Set<HivePrivilegeInfo> toGrants(List<PrivilegeGrantInfo> userGrants) { if (userGrants == null) { return ImmutableSet.of(); } ImmutableSet.Builder<HivePrivilegeInfo> privileges = ImmutableSet.builder(); for (PrivilegeGrantInfo userGrant : userGrants) { privileges.addAll(parsePrivilege(userGrant)); } return privileges.build(); }
/** * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order * each appears first in the source iterator. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> elements) { // We special-case for 0 or 1 elements, but anything further is madness. if (!elements.hasNext()) { return of(); } E first = elements.next(); if (!elements.hasNext()) { return of(first); } else { return new ImmutableSet.Builder<E>().add(first).addAll(elements).build(); } }
@ConfigDescription("The types of control queries allowed to run [CREATE, READ, MODIFY]") @Config("control.query-types") public VerifierConfig setControlQueryTypes(String types) { if (Strings.isNullOrEmpty(types)) { this.controlQueryTypes = ImmutableSet.of(); return this; } ImmutableSet.Builder<QueryType> builder = ImmutableSet.builder(); for (String value : Splitter.on(',').trimResults().omitEmptyStrings().split(types)) { builder.add(QueryType.valueOf(value.toUpperCase())); } this.controlQueryTypes = builder.build(); return this; }
/** * Converts a map of exposed ports as strings to a set of {@link Port}s (e.g. {@code * {"1000/tcp":{}}} -> {@code Port(1000, Protocol.TCP)}). * * @param portMap the map to convert * @return a set of {@link Port}s */ @VisibleForTesting static ImmutableSet<Port> portMapToSet(@Nullable Map<String, Map<?, ?>> portMap) throws BadContainerConfigurationFormatException { if (portMap == null) { return ImmutableSet.of(); } ImmutableSet.Builder<Port> ports = new ImmutableSet.Builder<>(); for (Map.Entry<String, Map<?, ?>> entry : portMap.entrySet()) { String port = entry.getKey(); Matcher matcher = PORT_PATTERN.matcher(port); if (!matcher.matches()) { throw new BadContainerConfigurationFormatException( "Invalid port configuration: '" + port + "'."); } int portNumber = Integer.parseInt(matcher.group("portNum")); String protocol = matcher.group("protocol"); ports.add(Port.parseProtocol(portNumber, protocol)); } return ports.build(); }