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