1. /*
  2. * Copyright 2004 The Apache Software Foundation.
  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. package org.apache.commons.betwixt;
  17. import java.util.HashMap;
  18. import java.util.Set;
  19. /**
  20. * Collective for <code>Betwixt</code> optional behaviour hints.
  21. * An option links a name with a value (both strings).
  22. *
  23. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  24. * @since 0.5
  25. */
  26. public class Options {
  27. /** Empty string array for use with toArray */
  28. private static final String[] EMPTY_STRING_ARRAY = {};
  29. /** Option values indexed by name */
  30. private HashMap valuesByName = new HashMap();
  31. /**
  32. * Gets the value (if any) associated with the given name.
  33. * @param name <code>String</code>, not null
  34. * @return the associated value, or null if no value is assocated
  35. */
  36. public String getValue(String name) {
  37. return (String) valuesByName.get(name);
  38. }
  39. /**
  40. * Gets the names of each option.
  41. * @return
  42. */
  43. public String[] getNames() {
  44. Set names = valuesByName.keySet();
  45. return (String[]) names.toArray(EMPTY_STRING_ARRAY);
  46. }
  47. /**
  48. * Adds the option.
  49. * The rule with options is that the last call to set the
  50. * value with a given name wins.
  51. * @param name <code>String</code> name, not null
  52. * @param value <code>Strong</code> name, not null
  53. */
  54. public void addOption(String name, String value) {
  55. valuesByName.put(name, value);
  56. }
  57. }