001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.betwixt; 019 020 import java.util.HashMap; 021 import java.util.Set; 022 023 /** 024 * Collective for <code>Betwixt</code> optional behaviour hints. 025 * An option links a name with a value (both strings). 026 * 027 * @author <a href='http://commons.apache.org/'>Apache Commons Team</a> 028 * @since 0.5 029 */ 030 public class Options { 031 /** Empty string array for use with toArray */ 032 private static final String[] EMPTY_STRING_ARRAY = {}; 033 /** Option values indexed by name */ 034 private HashMap valuesByName = new HashMap(); 035 036 /** 037 * Gets the value (if any) associated with the given name. 038 * @param name <code>String</code>, not null 039 * @return the associated value, or null if no value is assocated 040 */ 041 public String getValue(String name) { 042 return (String) valuesByName.get(name); 043 } 044 045 /** 046 * Gets the names of each option. 047 * @return <code>String</code> array containing the name of each option 048 */ 049 public String[] getNames() { 050 Set names = valuesByName.keySet(); 051 return (String[]) names.toArray(EMPTY_STRING_ARRAY); 052 } 053 054 /** 055 * Adds the option. 056 * The rule with options is that the last call to set the 057 * value with a given name wins. 058 * @param name <code>String</code> name, not null 059 * @param value <code>Strong</code> name, not null 060 */ 061 public void addOption(String name, String value) { 062 valuesByName.put(name, value); 063 } 064 065 /** 066 * Adds multiple options from an existing <code>Options</code> collection. 067 * The rule with options is that the most recently set value for an option 068 * wins, so options are potentially overwritten by this call. 069 * 070 * @param options - 071 * an existing <code>Options</code> collection 072 * @since 0.8 073 */ 074 public void addOptions(Options options) { 075 valuesByName.putAll(options.valuesByName); 076 } 077 }