KeyValueOption.java revision 953:221a84ef44c0
1139790Simp/* 2117267Smarcel * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 385685Sdfr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 485685Sdfr * 585685Sdfr * This code is free software; you can redistribute it and/or modify it 685685Sdfr * under the terms of the GNU General Public License version 2 only, as 785685Sdfr * published by the Free Software Foundation. Oracle designates this 8117267Smarcel * particular file as subject to the "Classpath" exception as provided 985685Sdfr * by Oracle in the LICENSE file that accompanied this code. 1085685Sdfr * 1185685Sdfr * This code is distributed in the hope that it will be useful, but WITHOUT 1285685Sdfr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1385685Sdfr * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1485685Sdfr * version 2 for more details (a copy is included in the LICENSE file that 15117267Smarcel * accompanied this code). 16117267Smarcel * 17117267Smarcel * You should have received a copy of the GNU General Public License version 18117267Smarcel * 2 along with this work; if not, write to the Free Software Foundation, 19117267Smarcel * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20117267Smarcel * 21117267Smarcel * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22117267Smarcel * or visit www.oracle.com if you need additional information or have any 23117267Smarcel * questions. 24117267Smarcel */ 2585685Sdfr 2685685Sdfrpackage jdk.nashorn.internal.runtime.options; 2785685Sdfr 2885685Sdfrimport java.util.Collections; 29115084Smarcelimport java.util.LinkedHashMap; 30115084Smarcelimport java.util.Map; 3185685Sdfrimport java.util.StringTokenizer; 32131945Smarcel 33131945Smarcel/** 34115084Smarcel * Key Value option such as logger. It comes on the format 35115084Smarcel * such as: 36115084Smarcel * 37131945Smarcel * {@code --log=module1:level1,module2:level2... } 38115084Smarcel */ 39115084Smarcelpublic class KeyValueOption extends Option<String> { 40115084Smarcel /** 41115084Smarcel * Map of keys given 42115084Smarcel */ 43131945Smarcel protected Map<String, String> map; 44131945Smarcel 45117267Smarcel KeyValueOption(final String value) { 46115084Smarcel super(value); 47115084Smarcel initialize(); 48115084Smarcel } 49115084Smarcel 50115084Smarcel Map<String, String> getValues() { 51117467Smarcel return Collections.unmodifiableMap(map); 52115084Smarcel } 53115084Smarcel 54115084Smarcel /** 55115084Smarcel * Check if the key value option has a value or if it has not 56115084Smarcel * been initialized 57 * @param key the key 58 * @return value, or null if not initialized 59 */ 60 public boolean hasValue(final String key) { 61 return map != null && map.get(key) != null; 62 } 63 64 String getValue(final String key) { 65 if (map == null) { 66 return null; 67 } 68 final String val = map.get(key); 69 return "".equals(val) ? null : val; 70 } 71 72 private void initialize() { 73 if (getValue() == null) { 74 return; 75 } 76 77 map = new LinkedHashMap<>(); 78 79 final StringTokenizer st = new StringTokenizer(getValue(), ","); 80 while (st.hasMoreElements()) { 81 final String token = st.nextToken(); 82 final String[] keyValue = token.split(":"); 83 84 if (keyValue.length == 1) { 85 map.put(keyValue[0], ""); 86 } else if (keyValue.length == 2) { 87 map.put(keyValue[0], keyValue[1]); 88 } else { 89 throw new IllegalArgumentException(token); 90 } 91 } 92 } 93} 94