|
30 | 30 |
|
31 | 31 | import java.util.Map; |
32 | 32 |
|
| 33 | +/** |
| 34 | + * A wrapper class around a {@link Map}. |
| 35 | + * <p> |
| 36 | + * We use wrapper classes instead of just passing around plain Maps because |
| 37 | + * </p><ul> |
| 38 | + * <li>it's more descriptive</li> |
| 39 | + * <li>there's no confusion between types (e.g. {@code Client} and {@code Channel})</li> |
| 40 | + * <li>we can create getters for the specific use-cases</li> |
| 41 | + * <li>we don't have to check for {@code null} each time</li> |
| 42 | + * </ul> |
| 43 | + */ |
33 | 44 | public class Wrapper { |
34 | 45 |
|
35 | 46 | private final Map<String, String> map; |
36 | 47 |
|
| 48 | + /** |
| 49 | + * Creates a new wrapper around the given map. |
| 50 | + * |
| 51 | + * @param map |
| 52 | + * the Map to abstract, cannot be {@code null} |
| 53 | + */ |
37 | 54 | public Wrapper(Map<String, String> map) { |
38 | 55 | this.map = map; |
39 | 56 | } |
40 | 57 |
|
| 58 | + /** |
| 59 | + * Gets the map underlying this {@code Wrapper}. |
| 60 | + * |
| 61 | + * @return the underlying map |
| 62 | + */ |
41 | 63 | public Map<String, String> getMap() { |
42 | 64 | return map; |
43 | 65 | } |
44 | 66 |
|
45 | | - public boolean getBoolean(String str) { |
46 | | - return getInt(str) == 1; |
| 67 | + /** |
| 68 | + * Gets a property as a boolean from the underlying map. |
| 69 | + * Returns {@code true} if the property exists in the map and its value is {@code "1"}. |
| 70 | + * |
| 71 | + * @param propertyName |
| 72 | + * the name of the property |
| 73 | + * |
| 74 | + * @return the boolean value of the property or {@code false} if the property doesn't exist |
| 75 | + */ |
| 76 | + public boolean getBoolean(String propertyName) { |
| 77 | + return getInt(propertyName) == 1; |
47 | 78 | } |
48 | 79 |
|
49 | | - public boolean getBoolean(Property p) { |
50 | | - return getBoolean(p.getName()); |
| 80 | + /** |
| 81 | + * Gets a property as a boolean from the underlying map. |
| 82 | + * Returns {@code true} if the property exists in the map and its value is {@code "1"}. |
| 83 | + * |
| 84 | + * @param property |
| 85 | + * the property |
| 86 | + * |
| 87 | + * @return the boolean value of the property or {@code false} if the property doesn't exist |
| 88 | + */ |
| 89 | + public boolean getBoolean(Property property) { |
| 90 | + return getBoolean(property.getName()); |
51 | 91 | } |
52 | 92 |
|
53 | | - public double getDouble(String str) { |
54 | | - final String value = get(str); |
| 93 | + /** |
| 94 | + * Gets a property as a double from the underlying map. |
| 95 | + * If the property doesn't exist in the underlying map, {@code -1.0} is returned. |
| 96 | + * |
| 97 | + * @param propertyName |
| 98 | + * the name of the property |
| 99 | + * |
| 100 | + * @return the double value of the property or {@code -1.0} if the property doesn't exist |
| 101 | + */ |
| 102 | + public double getDouble(String propertyName) { |
| 103 | + final String value = get(propertyName); |
55 | 104 | if (value == null || value.isEmpty()) { |
56 | 105 | return -1D; |
57 | 106 | } |
58 | 107 | return Double.valueOf(value); |
59 | 108 | } |
60 | 109 |
|
61 | | - public double getDouble(Property p) { |
62 | | - return getDouble(p.getName()); |
| 110 | + /** |
| 111 | + * Gets a property as a double from the underlying map. |
| 112 | + * If the property doesn't exist in the underlying map, {@code -1.0} is returned. |
| 113 | + * |
| 114 | + * @param property |
| 115 | + * the property |
| 116 | + * |
| 117 | + * @return the double value of the property or {@code -1.0} if the property doesn't exist |
| 118 | + */ |
| 119 | + public double getDouble(Property property) { |
| 120 | + return getDouble(property.getName()); |
63 | 121 | } |
64 | 122 |
|
65 | | - public long getLong(String str) { |
66 | | - final String value = get(str); |
| 123 | + /** |
| 124 | + * Gets a property as a long from the underlying map. |
| 125 | + * If the property doesn't exist in the underlying map, {@code -1} is returned. |
| 126 | + * |
| 127 | + * @param propertyName |
| 128 | + * the name of the property |
| 129 | + * |
| 130 | + * @return the long value of the property or {@code -1} if the property doesn't exist |
| 131 | + */ |
| 132 | + public long getLong(String propertyName) { |
| 133 | + final String value = get(propertyName); |
67 | 134 | if (value == null || value.isEmpty()) { |
68 | 135 | return -1L; |
69 | 136 | } |
70 | 137 | return Long.valueOf(value); |
71 | 138 | } |
72 | 139 |
|
73 | | - public long getLong(Property p) { |
74 | | - return getLong(p.getName()); |
| 140 | + /** |
| 141 | + * Gets a property as a long from the underlying map. |
| 142 | + * If the property doesn't exist in the underlying map, {@code -1} is returned. |
| 143 | + * |
| 144 | + * @param property |
| 145 | + * the property |
| 146 | + * |
| 147 | + * @return the long value of the property or {@code -1} if the property doesn't exist |
| 148 | + */ |
| 149 | + public long getLong(Property property) { |
| 150 | + return getLong(property.getName()); |
75 | 151 | } |
76 | 152 |
|
77 | | - public int getInt(String str) { |
78 | | - final String value = get(str); |
| 153 | + /** |
| 154 | + * Gets a property as an integer from the underlying map. |
| 155 | + * If the property doesn't exist in the underlying map, {@code -1} is returned. |
| 156 | + * |
| 157 | + * @param propertyName |
| 158 | + * the name of the property |
| 159 | + * |
| 160 | + * @return the integer value of the property or {@code -1} if the property doesn't exist |
| 161 | + */ |
| 162 | + public int getInt(String propertyName) { |
| 163 | + final String value = get(propertyName); |
79 | 164 | if (value == null || value.isEmpty()) { |
80 | 165 | return -1; |
81 | 166 | } |
82 | 167 | return Integer.valueOf(value); |
83 | 168 | } |
84 | 169 |
|
85 | | - public int getInt(Property p) { |
86 | | - return getInt(p.getName()); |
| 170 | + /** |
| 171 | + * Gets a property as an integer from the underlying map. |
| 172 | + * If the property doesn't exist in the underlying map, {@code -1} is returned. |
| 173 | + * |
| 174 | + * @param property |
| 175 | + * the property |
| 176 | + * |
| 177 | + * @return the integer value of the property or {@code -1} if the property doesn't exist |
| 178 | + */ |
| 179 | + public int getInt(Property property) { |
| 180 | + return getInt(property.getName()); |
87 | 181 | } |
88 | 182 |
|
89 | 183 | /** |
90 | | - * Only use this if you know what you are doing. |
| 184 | + * Gets a property as a String from the underlying map. |
| 185 | + * If the property doesn't exist in the underlying map, an empty String is returned. |
| 186 | + * |
| 187 | + * @param propertyName |
| 188 | + * the name of the property |
| 189 | + * |
| 190 | + * @return the String value of the property or an empty String if the property doesn't exist |
91 | 191 | */ |
92 | | - public String get(String str) { |
93 | | - final String result = map.get(str); |
| 192 | + public String get(String propertyName) { |
| 193 | + final String result = map.get(propertyName); |
94 | 194 | return result != null ? result : ""; |
95 | 195 | } |
96 | 196 |
|
97 | 197 | /** |
98 | | - * Only use this if you know what you are doing. |
| 198 | + * Gets a property as a {@code String} from the underlying map. |
| 199 | + * If the property doesn't exist in the underlying map, an empty String is returned. |
| 200 | + * |
| 201 | + * @param property |
| 202 | + * the property |
| 203 | + * |
| 204 | + * @return the String value of the property or an empty String if the property doesn't exist |
99 | 205 | */ |
100 | | - public String get(Property p) { |
101 | | - return get(p.getName()); |
| 206 | + public String get(Property property) { |
| 207 | + return get(property.getName()); |
102 | 208 | } |
103 | 209 |
|
104 | 210 | @Override |
105 | 211 | public String toString() { |
106 | 212 | return map.toString(); |
107 | 213 | } |
108 | | - |
109 | 214 | } |
0 commit comments