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