KeyValueOption.java revision 953:221a84ef44c0
1227825Stheraven/*
2227825Stheraven * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3227825Stheraven * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4353358Sdim *
5353358Sdim * This code is free software; you can redistribute it and/or modify it
6353358Sdim * under the terms of the GNU General Public License version 2 only, as
7227825Stheraven * published by the Free Software Foundation.  Oracle designates this
8227825Stheraven * particular file as subject to the "Classpath" exception as provided
9227825Stheraven * by Oracle in the LICENSE file that accompanied this code.
10227825Stheraven *
11227825Stheraven * This code is distributed in the hope that it will be useful, but WITHOUT
12227825Stheraven * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13227825Stheraven * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14227825Stheraven * version 2 for more details (a copy is included in the LICENSE file that
15227825Stheraven * accompanied this code).
16227825Stheraven *
17227825Stheraven * You should have received a copy of the GNU General Public License version
18227825Stheraven * 2 along with this work; if not, write to the Free Software Foundation,
19227825Stheraven * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20227825Stheraven *
21227825Stheraven * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22227825Stheraven * or visit www.oracle.com if you need additional information or have any
23227825Stheraven * questions.
24227825Stheraven */
25227825Stheraven
26227825Stheravenpackage jdk.nashorn.internal.runtime.options;
27227825Stheraven
28227825Stheravenimport java.util.Collections;
29227825Stheravenimport java.util.LinkedHashMap;
30227825Stheravenimport java.util.Map;
31227825Stheravenimport java.util.StringTokenizer;
32227825Stheraven
33227825Stheraven/**
34227825Stheraven * Key Value option such as logger. It comes on the format
35227825Stheraven * such as:
36227825Stheraven *
37227825Stheraven * {@code --log=module1:level1,module2:level2... }
38227825Stheraven */
39227825Stheravenpublic class KeyValueOption extends Option<String> {
40227825Stheraven    /**
41227825Stheraven     * Map of keys given
42227825Stheraven     */
43227825Stheraven    protected Map<String, String> map;
44227825Stheraven
45227825Stheraven    KeyValueOption(final String value) {
46227825Stheraven        super(value);
47227825Stheraven        initialize();
48227825Stheraven    }
49227825Stheraven
50227825Stheraven    Map<String, String> getValues() {
51227825Stheraven        return Collections.unmodifiableMap(map);
52227825Stheraven    }
53227825Stheraven
54227825Stheraven    /**
55227825Stheraven     * Check if the key value option has a value or if it has not
56227825Stheraven     * been initialized
57227825Stheraven     * @param key the key
58227825Stheraven     * @return value, or null if not initialized
59227825Stheraven     */
60227825Stheraven    public boolean hasValue(final String key) {
61227825Stheraven        return map != null && map.get(key) != null;
62227825Stheraven    }
63227825Stheraven
64227825Stheraven    String getValue(final String key) {
65227825Stheraven        if (map == null) {
66227825Stheraven            return null;
67227825Stheraven        }
68227825Stheraven        final String val = map.get(key);
69227825Stheraven        return "".equals(val) ? null : val;
70227825Stheraven    }
71227825Stheraven
72227825Stheraven    private void initialize() {
73227825Stheraven        if (getValue() == null) {
74227825Stheraven            return;
75227825Stheraven        }
76227825Stheraven
77227825Stheraven        map = new LinkedHashMap<>();
78227825Stheraven
79227825Stheraven        final StringTokenizer st = new StringTokenizer(getValue(), ",");
80227825Stheraven        while (st.hasMoreElements()) {
81227825Stheraven            final String   token    = st.nextToken();
82227825Stheraven            final String[] keyValue = token.split(":");
83227825Stheraven
84227825Stheraven            if (keyValue.length == 1) {
85227825Stheraven                map.put(keyValue[0], "");
86227825Stheraven            } else if (keyValue.length == 2) {
87227825Stheraven                map.put(keyValue[0], keyValue[1]);
88227825Stheraven            } else {
89227825Stheraven                throw new IllegalArgumentException(token);
90227825Stheraven            }
91227825Stheraven        }
92227825Stheraven    }
93227825Stheraven}
94227825Stheraven