Skip to content

Commit 84b964d

Browse files
committed
add AttributesContainer
1 parent e4ba7ae commit 84b964d

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.lecousin.framework.util;
2+
3+
import java.util.HashMap;
4+
5+
/** Abstract class implementing AttributesContainer with a HashMap. */
6+
public abstract class AbstractAttributesContainer implements AttributesContainer {
7+
8+
private HashMap<String,Object> attributes = new HashMap<>(20);
9+
10+
@Override
11+
public void setAttribute(String key, Object value) { attributes.put(key, value); }
12+
13+
@Override
14+
public Object getAttribute(String key) { return attributes.get(key); }
15+
16+
@Override
17+
public Object removeAttribute(String key) { return attributes.remove(key); }
18+
19+
@Override
20+
public boolean hasAttribute(String name) { return attributes.containsKey(name); }
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.lecousin.framework.util;
2+
3+
/**
4+
* Interface for objects which can store attributes.
5+
*/
6+
public interface AttributesContainer {
7+
8+
/** Set an attribute. */
9+
void setAttribute(String name, Object value);
10+
11+
/** Return the requested attribute, or null if no such attribute exists. */
12+
Object getAttribute(String name);
13+
14+
/** Remove and return an attribute. */
15+
Object removeAttribute(String name);
16+
17+
/** Return true if the given attribute is present. */
18+
boolean hasAttribute(String name);
19+
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.lecousin.framework.core.tests.util;
2+
3+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
4+
import net.lecousin.framework.util.AbstractAttributesContainer;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class TestAttributesContainer extends LCCoreAbstractTest {
10+
11+
@Test
12+
public void test() {
13+
AbstractAttributesContainer container = new AbstractAttributesContainer() {
14+
};
15+
Assert.assertNull(container.getAttribute("a"));
16+
Assert.assertFalse(container.hasAttribute("a"));
17+
container.setAttribute("a", "b");
18+
Assert.assertTrue(container.hasAttribute("a"));
19+
Assert.assertEquals("b", container.getAttribute("a"));
20+
Assert.assertTrue(container.hasAttribute("a"));
21+
Assert.assertEquals("b", container.removeAttribute("a"));
22+
Assert.assertFalse(container.hasAttribute("a"));
23+
Assert.assertNull(container.getAttribute("a"));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)