Skip to content

Commit

Permalink
Added more methods to 'Enriched'
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Sep 7, 2023
1 parent 10727f7 commit d854861
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;

/** A class for holding an object together with a class to instance map for adding metadata of arbitrary types. */
/**
* A class for annotating an 'item' with metadata of arbitrary types.
*/
public class Enriched<T> {
protected T item;
protected ClassToInstanceMap<Object> classToInstanceMap;
Expand All @@ -33,6 +35,14 @@ public <X> X getInstance(Class<X> type) {
return classToInstanceMap.getInstance(type);
}

public <X> X getInstanceOrDefault(Class<X> type, X defaultValue) {
X result = classToInstanceMap.getInstance(type);
if (result == null) {
result = defaultValue;
}
return result;
}

public <X> X putInstance(X value) {
Class<?> type = value.getClass();
classToInstanceMap.put(type, value);
Expand Down

0 comments on commit d854861

Please sign in to comment.