Skip to content

Instantly share code, notes, and snippets.

@UnquietCode
Created November 14, 2013 19:49
Show Gist options
  • Save UnquietCode/7473178 to your computer and use it in GitHub Desktop.
Save UnquietCode/7473178 to your computer and use it in GitHub Desktop.
Keep track of your API in your code. This also allows you to easily identify places in your application where legacy blocks ('hacks') can be removed.
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
}
}
}
public enum Feature {
BLUE_ICONS,
ACTIVITY_FEED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment