Java Reference

AttributeAware

Introduction

The AttributeAware interface is used by classes that maintain a collection of key-value pairs.

API Components

The definition includes just two methods: a getter and a setter. Note that both the key and the value are Objects.

API: AttributeAware interface
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);
}

Usages

The AttributeAware is used by built-in KOS classes, and can be used in your code.

Built-in

Two built-in class implement the AttributeAware interface:

  • HttpRequest

  • WebsocketConnection

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 class
public 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 class
public 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();
    }
}
Previous
Next
On this page
Java Development
Seamlessly transition from Legacy+ systems to Freestyle microdosing and advanced distributed dispense systems.
UI Development
Using KOS SDKs, integrating Consumer and Non-consumer facing UIs becomes seamless, giving you less hassle and more time to create.
Video Library
Meet some of our development team, as they lead you through the tools, features, and tips and tricks of various KOS tools.
Resources
Familiarize yourself with KOS terminology, our reference materials, and explore additional resources that complement your KOS journey.
Copyright © 2024 TCCC. All rights reserved.