public interface AttributeAware {
// Sets the attribute for the given key:
void setAttribute(Object key, Object value);
// Given a key, returns its attribute:
Object getAttribute(Object key);
}
The AttributeAware interface is used by classes that maintain a collection of key-value pairs.
The definition includes just two methods: a getter and a setter. Note that both the key and the value are Objects.
AttributeAware
interfacepublic interface AttributeAware {
// Sets the attribute for the given key:
void setAttribute(Object key, Object value);
// Given a key, returns its attribute:
Object getAttribute(Object key);
}
The AttributeAware is used by built-in KOS classes, and can be used in your code.
Here’s an example of how you might use the AttributeAware interface.
MyClass1
implements AttributeAware, overriding both of the methods. The setAttribute()
method puts the value into its internal map, while the getAttribute()
method returns the value associated with the key:
MyClass1
classpublic class MyClass1 implements AttributeAware {
private final Map<Object, Object> ourAttributes = new HashMap<>();
@Override
public void setAttribute(Object key, Object value) {
ourAttributes.put(key, value);
}
@Override
public Object getAttribute(Object key) {
return ourAttributes.get(key);
}
}
MyClass2
shows a simple example using MyClass1
. The initValues() method takes an AttributeAware object, where three values are set and one is retrieved.
MyClass2
classpublic class MyClass2 {
private final MyClass1 myClass1 = new MyClass1();
public void init() {
String key0Value = initValues(myClass1);
}
public String initValues(AttributeAware attributeAware) {
attributeAware.setAttribute("key1", "value1");
attributeAware.setAttribute("key2", 2);
attributeAware.setAttribute("key3", List.of(1, 2, 3));
return attributeAware.getAttribute("key0").toString();
}
}