Skip to content

Commit

Permalink
Iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontedor committed Apr 19, 2019
1 parent 3cad419 commit e9dd166
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class InstallPluginCommand extends EnvironmentAwareCommand {
OFFICIAL_PLUGINS = Streams.readAllLines(stream)
.stream()
.map(String::trim)
.collect(Sets.toSortedSet());
.collect(Sets.toUnmodifiableSortedSet());
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
final Set<String> liveInConfigIds = new TreeSet<>(currentConfig.getNodeIds());
liveInConfigIds.retainAll(liveNodeIds);

final Set<String> inConfigNotLiveIds = Sets.sortedDifference(currentConfig.getNodeIds(), liveInConfigIds);
final Set<String> inConfigNotLiveIds = Sets.unmodifiableSortedDifference(currentConfig.getNodeIds(), liveInConfigIds);
final Set<String> nonRetiredInConfigNotLiveIds = new TreeSet<>(inConfigNotLiveIds);
nonRetiredInConfigNotLiveIds.removeAll(retiredNodeIds);

Expand Down
63 changes: 59 additions & 4 deletions server/src/main/java/org/elasticsearch/common/util/set/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,50 @@ public static <T> Set<T> difference(Set<T> left, Set<T> right) {
* @param <T> the type of the elements of the sets
* @return the sorted relative complement of the left set with respect to the right set
*/
public static <T> SortedSet<T> sortedDifference(Set<T> left, Set<T> right) {
public static <T> SortedSet<T> sortedDifference(final Set<T> left, final Set<T> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return left.stream().filter(k -> !right.contains(k)).collect(toSortedSet());
return left.stream().filter(k -> right.contains(k) == false).collect(toSortedSet());
}

/**
* The relative complement, or difference, of the specified left and right set, returned as a sorted set. Namely, the resulting set
* contains all the elements that are in the left set but not in the right set, and the set is sorted using the natural ordering of
* element type. Neither input is mutated by this operation, an entirely new set is returned. The resulting set is unmodifiable.
*
* @param left the left set
* @param right the right set
* @param <T> the type of the elements of the sets
* @return the unmodifiable sorted relative complement of the left set with respect to the right set
*/
public static <T> Set<T> unmodifiableSortedDifference(final Set<T> left, final Set<T> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return left.stream().filter(k -> right.contains(k) == false).collect(toUnmodifiableSortedSet());
}

/**
* Returns a {@link Collector} that accumulates the input elements into a sorted set.
*
* @param <T> the type of the input elements
* @return a sorted set
*/
public static <T> Collector<T, SortedSet<T>, SortedSet<T>> toSortedSet() {
return new SortedSetCollector<>();
}

private static class SortedSetCollector<T> implements Collector<T, SortedSet<T>, SortedSet<T>> {
/**
* Returns a {@link Collector} that accumulates the input elements into a sorted set and finishes the resulting set into an
* unmodifiable set. The resulting read-only view through the unmodifiable set is a sorted set.
*
* @param <T> the type of the input elements
* @return an unmodifiable set where the underlying set is sorted
*/
public static <T> Collector<T, SortedSet<T>, Set<T>> toUnmodifiableSortedSet() {
return new UnmodifiableSortedSetCollector<>();
}

abstract static class AbstractSortedSetCollector<T, R extends Set<T>> implements Collector<T, SortedSet<T>, R> {

@Override
public Supplier<SortedSet<T>> supplier() {
Expand All @@ -126,13 +159,21 @@ public BinaryOperator<SortedSet<T>> combiner() {
};
}

public abstract Function<SortedSet<T>, R> finisher();

public abstract Set<Characteristics> characteristics();

}

private static class SortedSetCollector<T> extends AbstractSortedSetCollector<T, SortedSet<T>> {

@Override
public Function<SortedSet<T>, SortedSet<T>> finisher() {
return Function.identity();
}

static final Set<Characteristics> CHARACTERISTICS =
Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));

@Override
public Set<Characteristics> characteristics() {
Expand All @@ -141,6 +182,20 @@ public Set<Characteristics> characteristics() {

}

private static class UnmodifiableSortedSetCollector<T> extends AbstractSortedSetCollector<T, Set<T>> {

@Override
public Function<SortedSet<T>, Set<T>> finisher() {
return Collections::unmodifiableSet;
}

@Override
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}

}

public static <T> Set<T> union(Set<T> left, Set<T> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.rest.action.admin.indices;

import com.carrotsearch.hppc.cursors.ObjectCursor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
Expand Down Expand Up @@ -51,7 +50,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.SortedSet;
import java.util.stream.Collectors;

import static org.elasticsearch.rest.RestRequest.Method.GET;
Expand Down Expand Up @@ -118,7 +116,7 @@ public RestResponse buildResponse(final GetMappingsResponse response, final XCon
}
}

final SortedSet<String> difference = Sets.sortedDifference(Arrays.stream(types).collect(Collectors.toSet()), typeNames);
final Set<String> difference = Sets.sortedDifference(Arrays.stream(types).collect(Collectors.toSet()), typeNames);

// now remove requested aliases that contain wildcards that are simple matches
final List<String> matches = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -42,9 +44,21 @@ public void testDifference() {
}

public void testSortedDifference() {
runSortedDifferenceTest(Sets::sortedDifference, set -> {});
}

public void testUnmodifiableSortedDifference() {
runSortedDifferenceTest(
// assert the resulting difference us unmodifiable
Sets::unmodifiableSortedDifference, set -> expectThrows(UnsupportedOperationException.class, () -> set.add(randomInt())));
}

private void runSortedDifferenceTest(
final BiFunction<Set<Integer>, Set<Integer>, Set<Integer>> sortedDifference,
final Consumer<Set<Integer>> asserter) {
final int endExclusive = randomIntBetween(0, 256);
final Tuple<Set<Integer>, Set<Integer>> sets = randomSets(endExclusive);
final Set<Integer> difference = Sets.sortedDifference(sets.v1(), sets.v2());
final Set<Integer> difference = sortedDifference.apply(sets.v1(), sets.v2());
assertDifference(endExclusive, sets, difference);
final Iterator<Integer> it = difference.iterator();
if (it.hasNext()) {
Expand All @@ -55,6 +69,7 @@ public void testSortedDifference() {
current = next;
}
}
asserter.accept(difference);
}

public void testIntersection() {
Expand Down

0 comments on commit e9dd166

Please sign in to comment.