📄 parameterreader.java
字号:
/* * ParameterReader.java - a class for extracting key-value pairs from * parameters. * Copyright (C) 2003-2004 Rob Spoor * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */package nl.icemanx.util;import java.util.*;/** * ParameterReader contains methods for reading and checking command line * parameters. * * @author Rob Spoor * @version 0.4, 2003-12-01 */public final class ParameterReader{ /** * Indicates a key without value. */ public static final int KEY = 0; /** * Indicates a key with one value, separated by whitespace. */ public static final int KEY_VALUE = 1; /** * Indicates a key with one value, separated by an equals sign. */ public static final int KEY_EQUALS_VALUE = 2; /** * Indicates a key with multiple values. */ public static final int KEY_VALUES = 3; /** * A set of keys without values. */ private Set key; /** * A set of keys with one value, separated by whitespace. */ private Set keyValue; /** * A set of keys without values. */ private Set keyEqualsValue; /** * A set of keys with multiple values. */ private Set keyValues; /** * Creates a parameter reader. Use {@link #addKeys(Set, int)} or * {@link #addKey(String, int)} to set the keys. */ public ParameterReader() { key = new HashSet(); keyValue = new HashSet(); keyEqualsValue = new HashSet(); keyValues = new HashSet(); } /** * Returns a set of keys. * * @param type The type of keys. See the static fields for possible * values. * @return The set of keys of type <tt>type</tt>. * @throws IllegalArgumentException If the type is illegal. */ public Set getKeys(int type) { switch (type) { case KEY: return key; case KEY_VALUE: return keyValue; case KEY_EQUALS_VALUE: return keyEqualsValue; case KEY_VALUES: return keyValues; default: throw new IllegalArgumentException("Wrong type"); } } /** * Adds a set of keys. * * @param keys The set of keys. * @param type The type of keys. See the static fields for possible * values. * @throws IllegalArgumentException If either the type is illegal, or * there is a conflict in the keys. */ public void addKeys(Set keys, int type) { checkKeys(keys); getKeys(type).addAll(keys); } /** * Adds a set of keys. * * @param keys The set of keys, in array form. * @param type The type of keys. See the static fields for possible * values. * @throws IllegalArgumentException If either the type is illegal, or * there is a conflict in the keys. */ public void addKeys(String[] keys, int type) { addKeys(new HashSet(Arrays.asList(keys)), type); } /** * Adds a key. * * @param key The key. * @param type The type of keys. See the static fields for possible * values. * @throws IllegalArgumentException If either the type is illegal, or * there is a conflict in the keys. */ public void addKey(String key, int type) { addKeys(Collections.singleton(key), type); } /** * Removes a set of keys. * * @param keys The set of keys. * @param type The type of keys. See the static fields for possible * values. * @throws IllegalArgumentException If the type is illegal. */ public void removeKeys(Set keys, int type) { getKeys(type).removeAll(keys); } /** * Removes a set of keys. * * @param keys The set of keys. * @param type The type of keys. See the static fields for possible * values. * @throws IllegalArgumentException If the type is illegal. */ public void removeKeys(String[] keys, int type) { removeKeys(new HashSet(Arrays.asList(keys)), type); } /** * Removes a key. * * @param key The key. * @param type The type of keys. See the static fields for possible * values. * @throws IllegalArgumentException If the type is illegal. */ public void removeKey(String key, int type) { removeKeys(Collections.singleton(key), type); } /** * Checks if any of the keys in <tt>keys</tt> is in any of the sets of * keys. * * @param keys The set of keys to check. * @throws IllegalArgumentException If any of the keys in <tt>keys</tt> * is in any of the lists of keys. */ private void checkKeys(Set keys) { if (!emptyIntersection(key, keys) || !emptyIntersection(keyValue, keys) || !emptyIntersection(keyEqualsValue, keys) || !emptyIntersection(keyValues, keys)) { throw new IllegalArgumentException("Duplicate key"); } } /** * Checks whether two sets have an empty intersection. * * @param set1 The first set. * @param set2 The second set. * @return <tt>true</tt> iff the two sets have no common elements. */ private static boolean emptyIntersection(Set set1, Set set2) { Set intersect = new HashSet(set1); intersect.retainAll(set2); return (intersect.size() == 0); } /** * Reads the parameters in <tt>args</tt>, and returns a map.<br> * The value of the key will be <tt>null</tt> if the key should have * no value, a string if the key should have exactly one value, and a * {@link List} of strings if the key can have multiple values. * * @param args An array with the parameters. * @return A map containing the key-value pairs of the parameters. * @throws IllegalArgumentException If an unknown parameter is found, or a * parameter has the wrong number of values. * @see #addKeys(Set, int) * @see #addKey(String, int) */ public Map readParameters(String[] args) { Map parameters = new HashMap(); int i = 0; // for traversing args while (i < args.length) { if (key.contains(args[i])) { parameters.put(args[i++], null); } else if (keyValue.contains(args[i])) { if (i < args.length - 1) { parameters.put(args[i], args[i + 1]); i += 2; } else { // i = args.length - 1, so no value left throw new IllegalArgumentException(args[i]); } } else if (keyValues.contains(args[i])) { List list = new ArrayList(); parameters.put(args[i++], list); /* * Go on until either the arguments are all used up, or * another key is found. In case of keyEqualsValue, split the * argument in two parts at "=". * args[i].split("=", 2)[0].equals(args[i]) if there is no "=" * in args[i]. */ while ((i < args.length) && !key.contains(args[i]) && !keyValue.contains(args[i]) && !keyValues.contains(args[i]) && !keyEqualsValue.contains(args[i].split("=", 2)[0])) { list.add(args[i++]); } } else { // either KEY=VALUE or unknown String[] split = args[i].split("=", 2); if (split.length == 1) { // no "=", so unknown throw new IllegalArgumentException(args[i]); } else { if (keyEqualsValue.contains(split[0])) { parameters.put(split[0], split[1]); i++; } else { // not known throw new IllegalArgumentException(split[0]); } } } } return parameters; } /** * Checks whether only one of the keys in <tt>keys</tt> is set in * <tt>parameters</tt>. * * @param parameters A map with parameters. * @param keys A set containing keys that should be unique in * <tt>parameters</tt>. * @return <tt>true</tt> iff only one of the keys in <tt>keys</tt> * is in <tt>parameters</tt>. */ public static boolean unique(Map parameters, Set keys) { Set intersect = new HashSet(parameters.keySet()); intersect.retainAll(keys); /* * intersect is the intersection of keys in parameters and keys in * keys */ return (intersect.size() <= 1); /* * if the size of intersect is bigger than 1, more than 1 key in keys * were keys of the map. */ } /** * Checks whether only one of the keys in <tt>keys</tt> is set in * <tt>parameters</tt>. * * @param parameters A map with parameters. * @param keys A list containing keys that should be unique in * <tt>parameters</tt>, in array form. * @return <tt>true</tt> iff only one of the keys in <tt>keys</tt> * is in <tt>parameters</tt>. */ public static boolean unique(Map parameters, String[] keys) { return unique(parameters, new HashSet(Arrays.asList(keys))); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -