RuneHive-Game
Loading...
Searching...
No Matches
GenericAttributes.java
Go to the documentation of this file.
1package com.runehive.util.generic;
2
3import java.util.HashMap;
4import java.util.Map;
5
6/**
7 * Holds generic attributes.
8 *
9 * @author Michael | Chex
10 */
11public class GenericAttributes {
12
13 /** The map of generic attributes for an entity. */
14 private final Map<Object, Object> genericAttributes = new HashMap<>();
15
16 public <K, V> void put(K key, V value) {
17 genericAttributes.put(key, value);
18 }
19
20 /**
21 * Sets a generic attribute.
22 *
23 * @param <K>
24 * The key type.
25 * @param <E>
26 * The return value class type.
27 * @param key
28 * The key.
29 * @param attribute
30 * The value associated with this key.
31 */
32 public <K, E> void set(K key, E attribute) {
33 genericAttributes.put(key, attribute);
34 }
35
36 /**
37 * Modifies a generic attribute.
38 * @param <K> The key type.
39 * @param <E> The return value class type.
40 * @param key The key.
41 * @param value The value to set.
42 */
43 public <K, E> void modify(K key, E value) {
44 genericAttributes.replace(key, value);
45 }
46
47 /**
48 * Removes a generic attribute.
49 *
50 * @param <K>
51 * The key type.
52 * @param key
53 * The key.
54 */
55 public <K> void remove(K key) {
56 genericAttributes.remove(key);
57 }
58
59 /**
60 * Checks if a key is in the list of generic attribute.
61 *
62 * @param <K>
63 * The key type.
64 * @param key
65 * The key.
66 * @return {@code True} if the generic attributes contains a value with this
67 * key.
68 */
69 public <K> boolean has(K key) {
70 return genericAttributes.containsKey(key);
71 }
72
73 /**
74 * Gets a generic attribute.
75 *
76 * @param <K>
77 * The key type.
78 * @param <E>
79 * The return value class type.
80 * @param key
81 * The key.
82 * @return The value associated with the key, or null.
83 */
84 @SuppressWarnings("unchecked")
85 public <K, E> E get(K key) {
86 try {
87 return (E) genericAttributes.get(key);
88 } catch (Exception e) {
89 e.printStackTrace();
90 return null;
91 }
92 }
93
94 /**
95 * Gets a generic attribute.
96 *
97 * @param <K>
98 * The key type.
99 * @param key
100 * The key.
101 * @return The value associated with the key, or null.
102 */
103 @SuppressWarnings("unchecked")
104 public <K> Object getObject(K key) {
105 try {
106 return genericAttributes.get(key);
107 } catch (Exception e) {
108 e.printStackTrace();
109 return null;
110 }
111 }
112
113 /**
114 * Gets a generic attribute casted to a specific class type.
115 *
116 * @param <K>
117 * The key type.
118 * @param <E>
119 * The return value class type.
120 * @param key
121 * The key.
122 * @param type
123 * The class for the return type.
124 * @return The value associated with the key, or null.
125 */
126 public <K, E> E get(K key, Class<? extends E> type) {
127 Object attribute = genericAttributes.get(key);
128 try {
129 return type.cast(attribute);
130 } catch (Exception e) {
131 e.printStackTrace();
132 return null;
133 }
134 }
135
136 /**
137 * Gets the state of a key.
138 *
139 * @param <K>
140 * The key type.
141 * @param key
142 * The key to check.
143 * @return {@code True} if the value of the key is equal to
144 * {@code Boolean.TRUE}.
145 */
146 public <K> boolean is(K key) {
147 return has(key) && Boolean.TRUE == get(key, Boolean.class);
148 }
149}
public< K, E > E get(K key)
Gets a generic attribute.
public< K, V > void put(K key, V value)
public< K, E > void modify(K key, E value)
Modifies a generic attribute.
public< K > Object getObject(K key)
Gets a generic attribute.
public< K > boolean is(K key)
Gets the state of a key.
final Map< Object, Object > genericAttributes
The map of generic attributes for an entity.
public< K > boolean has(K key)
Checks if a key is in the list of generic attribute.