Skip to content

Commit

Permalink
Use an enum set for the FeatureSet. ImmutableSet only uses EnumSets i…
Browse files Browse the repository at this point in the history
…f given an EnumSet to start.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181516248
  • Loading branch information
concavelenz authored and brad4d committed Jan 11, 2018
1 parent 4656c5d commit 0271fb6
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions src/com/google/javascript/jscomp/parsing/parser/FeatureSet.java
Expand Up @@ -16,11 +16,11 @@


package com.google.javascript.jscomp.parsing.parser; package com.google.javascript.jscomp.parsing.parser;


import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.errorprone.annotations.Immutable; import com.google.errorprone.annotations.Immutable;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashSet; import java.util.EnumSet;
import java.util.Set; import java.util.Set;


/** /**
Expand All @@ -44,7 +44,7 @@ public final class FeatureSet implements Serializable {
private final ImmutableSet<Feature> features; private final ImmutableSet<Feature> features;


/** The bare minimum set of features. */ /** The bare minimum set of features. */
public static final FeatureSet BARE_MINIMUM = new FeatureSet(ImmutableSet.<Feature>of()); public static final FeatureSet BARE_MINIMUM = new FeatureSet(emptyEnumSet());


/** Features from ES3. */ /** Features from ES3. */
public static final FeatureSet ES3 = BARE_MINIMUM.with(LangVersion.ES3.features()); public static final FeatureSet ES3 = BARE_MINIMUM.with(LangVersion.ES3.features());
Expand Down Expand Up @@ -91,8 +91,8 @@ private enum LangVersion {
ES_NEXT, ES_NEXT,
TYPESCRIPT; TYPESCRIPT;


private Set<Feature> features() { private EnumSet<Feature> features() {
Set<Feature> set = new HashSet<>(); EnumSet<Feature> set = EnumSet.noneOf(Feature.class);
for (Feature feature : Feature.values()) { for (Feature feature : Feature.values()) {
if (feature.version == this) { if (feature.version == this) {
set.add(feature); set.add(feature);
Expand Down Expand Up @@ -180,7 +180,8 @@ public String toString() {
} }
} }


private FeatureSet(Set<Feature> features) { private FeatureSet(EnumSet<Feature> features) {
// ImmutableSet will only use an EnumSet if the set starts as an EnumSet.
this.features = ImmutableSet.copyOf(features); this.features = ImmutableSet.copyOf(features);
} }


Expand Down Expand Up @@ -214,22 +215,22 @@ public String version() {
} }


public FeatureSet without(Feature feature) { public FeatureSet without(Feature feature) {
return new FeatureSet(Sets.difference(features, ImmutableSet.of(feature))); return new FeatureSet(difference(features, EnumSet.of(feature)));
} }


public FeatureSet without(FeatureSet other) { public FeatureSet without(FeatureSet other) {
return new FeatureSet(Sets.difference(features, other.features)); return new FeatureSet(difference(features, other.features));
} }


public FeatureSet withoutTypes() { public FeatureSet withoutTypes() {
return new FeatureSet(Sets.difference(features, LangVersion.TYPESCRIPT.features())); return new FeatureSet(difference(features, LangVersion.TYPESCRIPT.features()));
} }


/** /**
* Returns a new {@link FeatureSet} including all features of both {@code this} and {@code other}. * Returns a new {@link FeatureSet} including all features of both {@code this} and {@code other}.
*/ */
public FeatureSet union(FeatureSet other) { public FeatureSet union(FeatureSet other) {
return new FeatureSet(Sets.union(features, other.features)); return new FeatureSet(union(features, other.features));
} }


/** /**
Expand All @@ -239,14 +240,50 @@ public boolean contains(FeatureSet other) {
return this.features.containsAll(other.features); return this.features.containsAll(other.features);
} }


private static EnumSet<Feature> emptyEnumSet() {
return EnumSet.noneOf(Feature.class);
}

private static EnumSet<Feature> enumSetOf(Set<Feature> set) {
return set.isEmpty() ? emptyEnumSet() : EnumSet.copyOf(set);
}

private static EnumSet<Feature> add(Set<Feature> features, Feature feature) {
EnumSet<Feature> result = enumSetOf(features);
result.add(feature);
return result;
}

private static EnumSet<Feature> union(Set<Feature> features, Set<Feature> newFeatures) {
EnumSet<Feature> result = enumSetOf(features);
result.addAll(newFeatures);
return result;
}

private static EnumSet<Feature> difference(Set<Feature> features, Set<Feature> removedFeatures) {
EnumSet<Feature> result = enumSetOf(features);
result.removeAll(removedFeatures);
return result;
}

/** Returns a feature set combining all the features from {@code this} and {@code feature}. */
public FeatureSet with(Feature feature) {
if (features.contains(feature)) {
return this;
}
return new FeatureSet(add(features, feature));
}

/** Returns a feature set combining all the features from {@code this} and {@code newFeatures}. */ /** Returns a feature set combining all the features from {@code this} and {@code newFeatures}. */
@VisibleForTesting
public FeatureSet with(Feature... newFeatures) { public FeatureSet with(Feature... newFeatures) {
return new FeatureSet(Sets.union(features, ImmutableSet.copyOf(newFeatures))); return new FeatureSet(union(features, ImmutableSet.copyOf(newFeatures)));
} }


/** Returns a feature set combining all the features from {@code this} and {@code newFeatures}. */ /** Returns a feature set combining all the features from {@code this} and {@code newFeatures}. */
@VisibleForTesting
public FeatureSet with(Set<Feature> newFeatures) { public FeatureSet with(Set<Feature> newFeatures) {
return new FeatureSet(Sets.union(features, newFeatures)); return new FeatureSet(union(features, newFeatures));
} }


/** /**
Expand Down

0 comments on commit 0271fb6

Please sign in to comment.