1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package android.filterfw.core;
19 
20 import java.io.StringWriter;
21 import java.util.HashMap;
22 import java.util.Map;
23 
24 /**
25  * @hide
26  */
27 public class KeyValueMap extends HashMap<String, Object> {
28 
setKeyValues(Object... keyValues)29     public void setKeyValues(Object... keyValues) {
30         if (keyValues.length % 2 != 0) {
31             throw new RuntimeException("Key-Value arguments passed into setKeyValues must be "
32             + "an alternating list of keys and values!");
33         }
34         for (int i = 0; i < keyValues.length; i += 2) {
35             if (!(keyValues[i] instanceof String)) {
36                 throw new RuntimeException("Key-value argument " + i + " must be a key of type "
37                     + "String, but found an object of type " + keyValues[i].getClass() + "!");
38             }
39             String key = (String)keyValues[i];
40             Object value = keyValues[i+1];
41             put(key, value);
42         }
43     }
44 
fromKeyValues(Object... keyValues)45     public static KeyValueMap fromKeyValues(Object... keyValues) {
46         KeyValueMap result = new KeyValueMap();
47         result.setKeyValues(keyValues);
48         return result;
49     }
50 
getString(String key)51     public String getString(String key) {
52         Object result = get(key);
53         return result != null ? (String)result : null;
54     }
55 
getInt(String key)56     public int getInt(String key) {
57         Object result = get(key);
58         return result != null ? (Integer)result : null;
59     }
60 
getFloat(String key)61     public float getFloat(String key) {
62         Object result = get(key);
63         return result != null ? (Float)result : null;
64     }
65 
66     @Override
toString()67     public String toString() {
68         StringWriter writer = new StringWriter();
69         for (Map.Entry<String, Object> entry : entrySet()) {
70             String valueString;
71             Object value = entry.getValue();
72             if (value instanceof String) {
73                 valueString = "\"" + value + "\"";
74             } else {
75                 valueString = value.toString();
76             }
77             writer.write(entry.getKey() + " = " + valueString + ";\n");
78         }
79         return writer.toString();
80     }
81 
82 }
83