001 package org.maltparser.core.options; 002 003 import java.util.Set; 004 import java.util.SortedMap; 005 import java.util.TreeMap; 006 007 import org.maltparser.core.options.option.Option; 008 009 /** 010 * OptionValues contain a number of option containers, which contains the option values (the instance of 011 * options). 012 * 013 * @author Johan Hall 014 * @since 1.0 015 **/ 016 public class OptionValues { 017 private SortedMap<Integer, OptionContainer> optionContainers; 018 019 /** 020 * Creates OptionValues. 021 */ 022 public OptionValues() { 023 super(); 024 optionContainers = new TreeMap<Integer, OptionContainer>(); 025 } 026 027 /** 028 * Returns the option value for an option that is in a specific option container. 029 * 030 * @param containerIndex the index of the option container. 031 * @param option the option object 032 * @return an object that contains the value of the option, <i>null</i> if the option value could not be found. 033 * @throws OptionException 034 */ 035 public Object getOptionValue(int containerIndex, Option option) throws OptionException { 036 OptionContainer oc = optionContainers.get(containerIndex); 037 if (oc == null) { 038 throw new OptionException("The option container '"+containerIndex+"' cannot be found. "); 039 } 040 return oc.getOptionValue(option); 041 } 042 043 /** 044 * Returns a string representation of the option value for an option that is in a specific option container. 045 * 046 * @param containerIndex the index of the option container. 047 * @param option an option object 048 * @return a string representation of the option value for an option that is in a specific option container. 049 * @throws OptionException 050 */ 051 public String getOptionValueString(int containerIndex, Option option) throws OptionException { 052 OptionContainer oc = optionContainers.get(containerIndex); 053 if (oc == null) { 054 throw new OptionException("The option container '"+containerIndex+"' cannot be found. "); 055 } 056 return oc.getOptionValueString(option); 057 } 058 059 /** 060 * Returns the option value for an option. 061 * 062 * @param option an option object 063 * @return the option value for an option, <i>null</i> if the option value could not be found. 064 * @throws OptionException 065 */ 066 public Object getOptionValue(Option option) throws OptionException { 067 if (optionContainers.size() == 0) { 068 return null; 069 } 070 OptionContainer oc = optionContainers.get(optionContainers.firstKey()); 071 return oc.getOptionValue(option); 072 } 073 074 /** 075 * Returns the number of option values for a particular option container. 076 * 077 * @param containerIndex The index of the option container. 078 * @return the number of option values for a particular option container. 079 */ 080 public int getNumberOfOptionValues(int containerIndex) { 081 if (!optionContainers.containsKey(containerIndex)) { 082 return 0; 083 } 084 return optionContainers.get(containerIndex).getNumberOfOptionValues(); 085 } 086 087 /** 088 * Returns a sorted set of container names. 089 * 090 * @return a sorted set of container names. 091 */ 092 public Set<Integer> getOptionContainerIndices() { 093 return optionContainers.keySet(); 094 } 095 096 097 /** 098 * Adds an option value to an option to one of the internal option container specified by the type. 099 * 100 * @param containerType the type of the option container. 101 * @param containerIndex the index of the option container. 102 * @param option an option to add 103 * @param value an option value to add 104 * @return true if the value is added, false if the value already is in use. 105 * @throws OptionException 106 */ 107 public boolean addOptionValue(int containerType, int containerIndex, Option option, Object value) throws OptionException { 108 if (option == null) { 109 throw new OptionException("The option cannot be found. "); 110 } 111 if (value == null) { 112 throw new OptionException("The option value cannot be found. "); 113 } 114 115 if (!optionContainers.containsKey(containerIndex)) { 116 optionContainers.put(containerIndex, new OptionContainer(containerIndex)); 117 } 118 OptionContainer oc = optionContainers.get(containerIndex); 119 if (oc == null) { 120 throw new OptionException("The option container index "+containerIndex+" is unknown"); 121 } 122 if (!oc.contains(containerType, option)) { 123 oc.addOptionValue(containerType, option, value); 124 return true; 125 } 126 return false; 127 } 128 129 130 /* (non-Javadoc) 131 * @see java.lang.Object#toString() 132 */ 133 public String toString() { 134 final StringBuilder sb = new StringBuilder(); 135 if (optionContainers.size() == 0) { 136 sb.append("No option values."); 137 } else if (optionContainers.size() == 1) { 138 sb.append(optionContainers.get(optionContainers.firstKey())); 139 } else { 140 for (Integer index : optionContainers.keySet()) { 141 sb.append("Option container : "+index+"\n"); 142 sb.append(optionContainers.get(index)+"\n"); 143 } 144 } 145 return sb.toString(); 146 } 147 }