Skip to content

Instantly share code, notes, and snippets.

@UnquietCode
Created November 14, 2013 19:49
Show Gist options
  • Select an option

  • Save UnquietCode/7473178 to your computer and use it in GitHub Desktop.

Select an option

Save UnquietCode/7473178 to your computer and use it in GitHub Desktop.

Revisions

  1. UnquietCode created this gist Nov 14, 2013.
    107 changes: 107 additions & 0 deletions API.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    public enum API {

    @Deprecated Version1,

    Version2(
    add(Feature.BLUE_ICONS)
    ),

    Version3(
    parent(Version2),
    add(Feature.ACTIVITY_FEED)
    ),

    Version4(
    parent(Version3),
    remove(Feature.BLUE_ICONS)
    )

    ;

    private final Set<Feature> featureSet = new HashSet<Feature>();

    API(FeatureWrapper...features) {

    // parents first
    for (FeatureWrapper wrapped : features) {
    if (wrapped.operation == FeatureWrapper.Operation.PARENT) {
    featureSet.addAll(wrapped.parent.featureSet);
    }
    }

    // then modifications
    for (FeatureWrapper wrapped : features) {
    if (wrapped.operation == FeatureWrapper.Operation.ADD) {
    featureSet.add(wrapped.feature);
    } else if (wrapped.operation == FeatureWrapper.Operation.REMOVE) {
    featureSet.remove(wrapped.feature);
    }
    }
    }

    boolean isGreaterThan(API other) {
    return this.ordinal() > other.ordinal();
    }

    boolean isGreaterOrEqualTo(API other) {
    return this.ordinal() >= other.ordinal();
    }

    boolean isLesserThan(API other) {
    return this.ordinal() < other.ordinal();
    }

    boolean isLesserOrEqualTo(API other) {
    return this.ordinal() <= other.ordinal();
    }

    boolean hasFeature(Feature feature) {
    return featureSet.contains(feature);
    }

    boolean hasFeatures(Feature...features) {
    for (Feature feature : features) {
    if (!hasFeature(feature)) {
    return false;
    }
    }

    return true;
    }

    // ---------------------------------------------------------------- //

    private static FeatureWrapper add(Feature feature) {
    return new FeatureWrapper(feature, FeatureWrapper.Operation.ADD);
    }

    private static FeatureWrapper remove(Feature feature) {
    return new FeatureWrapper(feature, FeatureWrapper.Operation.REMOVE);
    }

    private static FeatureWrapper parent(API parent) {
    return new FeatureWrapper(parent);
    }

    private static class FeatureWrapper {
    final Feature feature;
    final Operation operation;
    final API parent;

    private FeatureWrapper(Feature feature, Operation operation) {
    this.feature = feature;
    this.operation = operation;
    this.parent = null;
    }

    private FeatureWrapper(API parent) {
    this.feature = null;
    this.operation = Operation.PARENT;
    this.parent = parent;
    }

    enum Operation {
    ADD, REMOVE, PARENT
    }
    }
    }
    4 changes: 4 additions & 0 deletions Feature.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    public enum Feature {
    BLUE_ICONS,
    ACTIVITY_FEED
    }