|
| 1 | +package com.datapump; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import com.google.common.base.Preconditions; |
| 8 | +import com.google.common.collect.ImmutableMap; |
| 9 | +import com.google.common.collect.Maps; |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by kaideng on 17-6-17. |
| 13 | + */ |
| 14 | +public class Context { |
| 15 | + |
| 16 | + private Map<String, String> parameters; |
| 17 | + |
| 18 | + public Context() { |
| 19 | + parameters = Collections.synchronizedMap(new HashMap<String, String>()); |
| 20 | + } |
| 21 | + |
| 22 | + public Context(Map<String, String> paramters) { |
| 23 | + this(); |
| 24 | + this.putAll(paramters); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets a copy of the backing map structure. |
| 29 | + * @return immutable copy of backing map structure |
| 30 | + */ |
| 31 | + public ImmutableMap<String, String> getParameters() { |
| 32 | + synchronized (parameters) { |
| 33 | + return ImmutableMap.copyOf(parameters); |
| 34 | + } |
| 35 | + } |
| 36 | + /** |
| 37 | + * Removes all of the mappings from this map. |
| 38 | + */ |
| 39 | + public void clear() { |
| 40 | + parameters.clear(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get properties which start with a prefix. When a property is returned, |
| 45 | + * the prefix is removed the from name. For example, if this method is |
| 46 | + * called with a parameter "hdfs." and the context contains: |
| 47 | + * <code> |
| 48 | + * { hdfs.key = value, otherKey = otherValue } |
| 49 | + * </code> |
| 50 | + * this method will return a map containing: |
| 51 | + * <code> |
| 52 | + * { key = value} |
| 53 | + * </code> |
| 54 | + * |
| 55 | + * <b>Note:</b> The <tt>prefix</tt> must end with a period character. If not |
| 56 | + * this method will raise an IllegalArgumentException. |
| 57 | + * |
| 58 | + * @param prefix key prefix to find and remove from keys in resulting map |
| 59 | + * @return map with keys which matched prefix with prefix removed from |
| 60 | + * keys in resulting map. If no keys are matched, the returned map is |
| 61 | + * empty |
| 62 | + * @throws IllegalArguemntException if the given prefix does not end with |
| 63 | + * a period character. |
| 64 | + */ |
| 65 | + public ImmutableMap<String, String> getSubProperties(String prefix) { |
| 66 | + Preconditions.checkArgument(prefix.endsWith("."), |
| 67 | + "The given prefix does not end with a period (" + prefix + ")"); |
| 68 | + Map<String, String> result = Maps.newHashMap(); |
| 69 | + synchronized (parameters) { |
| 70 | + for (String key : parameters.keySet()) { |
| 71 | + if (key.startsWith(prefix)) { |
| 72 | + String name = key.substring(prefix.length()); |
| 73 | + result.put(name, parameters.get(key)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + return ImmutableMap.copyOf(result); |
| 78 | + } |
| 79 | + /** |
| 80 | + * Associates all of the given map's keys and values in the Context. |
| 81 | + */ |
| 82 | + public void putAll(Map<String, String> map) { |
| 83 | + parameters.putAll(map); |
| 84 | + } |
| 85 | + /** |
| 86 | + * Associates the specified value with the specified key in this context. |
| 87 | + * If the context previously contained a mapping for the key, the old value |
| 88 | + * is replaced by the specified value. |
| 89 | + * @param key key with which the specified value is to be associated |
| 90 | + * @param value to be associated with the specified key |
| 91 | + */ |
| 92 | + public void put(String key, String value) { |
| 93 | + parameters.put(key, value); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns true if this Context contains a mapping for key. |
| 98 | + * Otherwise, returns false. |
| 99 | + */ |
| 100 | + public boolean containsKey(String key) { |
| 101 | + return parameters.containsKey(key); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Gets value mapped to key, returning defaultValue if unmapped. |
| 106 | + * @param key to be found |
| 107 | + * @param defaultValue returned if key is unmapped |
| 108 | + * @return value associated with key |
| 109 | + */ |
| 110 | + public Boolean getBoolean(String key, Boolean defaultValue) { |
| 111 | + String value = get(key); |
| 112 | + if (value != null) { |
| 113 | + return Boolean.parseBoolean(value.trim()); |
| 114 | + } |
| 115 | + return defaultValue; |
| 116 | + } |
| 117 | + /** |
| 118 | + * Gets value mapped to key, returning null if unmapped. |
| 119 | + * <p> |
| 120 | + * Note that this method returns an object as opposed to a |
| 121 | + * primitive. The configuration key requested may not be mapped |
| 122 | + * to a value and by returning the primitive object wrapper we can |
| 123 | + * return null. If the key does not exist the return value of |
| 124 | + * this method is assigned directly to a primitive, a |
| 125 | + * {@link NullPointerException} will be thrown. |
| 126 | + * </p> |
| 127 | + * @param key to be found |
| 128 | + * @return value associated with key or null if unmapped |
| 129 | + */ |
| 130 | + public Boolean getBoolean(String key) { |
| 131 | + return getBoolean(key, null); |
| 132 | + } |
| 133 | + /** |
| 134 | + * Gets value mapped to key, returning defaultValue if unmapped. |
| 135 | + * @param key to be found |
| 136 | + * @param defaultValue returned if key is unmapped |
| 137 | + * @return value associated with key |
| 138 | + */ |
| 139 | + public Integer getInteger(String key, Integer defaultValue) { |
| 140 | + String value = get(key); |
| 141 | + if (value != null) { |
| 142 | + return Integer.parseInt(value.trim()); |
| 143 | + } |
| 144 | + return defaultValue; |
| 145 | + } |
| 146 | + /** |
| 147 | + * Gets value mapped to key, returning null if unmapped. |
| 148 | + * <p> |
| 149 | + * Note that this method returns an object as opposed to a |
| 150 | + * primitive. The configuration key requested may not be mapped |
| 151 | + * to a value and by returning the primitive object wrapper we can |
| 152 | + * return null. If the key does not exist the return value of |
| 153 | + * this method is assigned directly to a primitive, a |
| 154 | + * {@link NullPointerException} will be thrown. |
| 155 | + * </p> |
| 156 | + * @param key to be found |
| 157 | + * @return value associated with key or null if unmapped |
| 158 | + */ |
| 159 | + public Integer getInteger(String key) { |
| 160 | + return getInteger(key, null); |
| 161 | + } |
| 162 | + /** |
| 163 | + * Gets value mapped to key, returning defaultValue if unmapped. |
| 164 | + * @param key to be found |
| 165 | + * @param defaultValue returned if key is unmapped |
| 166 | + * @return value associated with key |
| 167 | + */ |
| 168 | + public Long getLong(String key, Long defaultValue) { |
| 169 | + String value = get(key); |
| 170 | + if (value != null) { |
| 171 | + return Long.parseLong(value.trim()); |
| 172 | + } |
| 173 | + return defaultValue; |
| 174 | + } |
| 175 | + /** |
| 176 | + * Gets value mapped to key, returning null if unmapped. |
| 177 | + * <p> |
| 178 | + * Note that this method returns an object as opposed to a |
| 179 | + * primitive. The configuration key requested may not be mapped |
| 180 | + * to a value and by returning the primitive object wrapper we can |
| 181 | + * return null. If the key does not exist the return value of |
| 182 | + * this method is assigned directly to a primitive, a |
| 183 | + * {@link NullPointerException} will be thrown. |
| 184 | + * </p> |
| 185 | + * @param key to be found |
| 186 | + * @return value associated with key or null if unmapped |
| 187 | + */ |
| 188 | + public Long getLong(String key) { |
| 189 | + return getLong(key, null); |
| 190 | + } |
| 191 | + /** |
| 192 | + * Gets value mapped to key, returning defaultValue if unmapped. |
| 193 | + * @param key to be found |
| 194 | + * @param defaultValue returned if key is unmapped |
| 195 | + * @return value associated with key |
| 196 | + */ |
| 197 | + public String getString(String key, String defaultValue) { |
| 198 | + return get(key, defaultValue); |
| 199 | + } |
| 200 | + /** |
| 201 | + * Gets value mapped to key, returning null if unmapped. |
| 202 | + * @param key to be found |
| 203 | + * @return value associated with key or null if unmapped |
| 204 | + */ |
| 205 | + public String getString(String key) { |
| 206 | + return get(key); |
| 207 | + } |
| 208 | + private String get(String key, String defaultValue) { |
| 209 | + String result = parameters.get(key); |
| 210 | + if (result != null) { |
| 211 | + return result; |
| 212 | + } |
| 213 | + return defaultValue; |
| 214 | + } |
| 215 | + private String get(String key) { |
| 216 | + return get(key, null); |
| 217 | + } |
| 218 | + @Override |
| 219 | + public String toString() { |
| 220 | + return "{ parameters:" + parameters + " }"; |
| 221 | + } |
| 222 | +} |
| 223 | + |
0 commit comments