001package org.maltparser.core.options.option; 002 003import java.util.Collections; 004import java.util.Formatter; 005import java.util.HashMap; 006import java.util.Map; 007import java.util.SortedSet; 008import java.util.TreeSet; 009 010import org.maltparser.core.exception.MaltChainedException; 011import org.maltparser.core.options.OptionException; 012import org.maltparser.core.options.OptionGroup; 013 014/** 015 * An enumerate option is an option that can only contain string value, which is in the legal value set. 016 * 017 * @author Johan Hall 018 * @since 1.0 019**/ 020public class EnumOption extends Option { 021 private String defaultValue; 022 private final SortedSet<String> legalValues; 023 private final Map<String,String> legalValueDesc; 024 025 /** 026 * Creates an enumerate option description 027 * 028 * @param group a reference to the option group. 029 * @param name the name of the option. 030 * @param shortDescription a short description of the option. 031 * @param flag a short string that can be used in the command line. 032 * @param usage a string that explains the usage of the option. 033 * @throws OptionException 034 */ 035 public EnumOption(OptionGroup group, 036 String name, 037 String shortDescription, 038 String flag, 039 String usage) throws MaltChainedException { 040 super(group, name, shortDescription, flag, usage); 041 legalValues = Collections.synchronizedSortedSet(new TreeSet<String>()); 042 legalValueDesc = Collections.synchronizedMap(new HashMap<String,String>()); 043 } 044 045 /* (non-Javadoc) 046 * @see org.maltparser.core.options.option.Option#getValueObject(java.lang.String) 047 */ 048 public Object getValueObject(String value) throws MaltChainedException { 049 if (value == null) { 050 return null; 051 } else if (legalValues.contains(value)) { 052 return new String(value); 053 } else { 054 throw new OptionException("'"+value+"' is not a legal value for the '"+getName()+"' option. "); 055 } 056 } 057 058 /* (non-Javadoc) 059 * @see org.maltparser.core.options.option.Option#getDefaultValueObject() 060 */ 061 public Object getDefaultValueObject() throws MaltChainedException { 062 return new String(defaultValue); 063 } 064 065 /* (non-Javadoc) 066 * @see org.maltparser.core.options.option.Option#getDefaultValueString() 067 */ 068 public String getDefaultValueString() { 069 return defaultValue.toString(); 070 } 071 072 /* (non-Javadoc) 073 * @see org.maltparser.core.options.option.Option#setDefaultValue(java.lang.String) 074 */ 075 public void setDefaultValue(String defaultValue) throws MaltChainedException { 076 if (defaultValue == null) { 077 if (legalValues.isEmpty()) { 078 throw new OptionException("The default value of the '"+getName()+"' option is null and the legal value set is empty."); 079 } else { 080 this.defaultValue = legalValues.first(); 081 } 082 } else if (legalValues.contains(defaultValue.toLowerCase())) { 083 this.defaultValue = defaultValue.toLowerCase(); 084 } else { 085 throw new OptionException("The default value '"+defaultValue+"' for the '"+getName()+"' option is not a legal value. "); 086 } 087 } 088 089 /** 090 * Adds a legal value 091 * 092 * @param value a legal value name 093 * @param desc a short description of the legal value 094 * @throws OptionException 095 */ 096 public void addLegalValue(String value, String desc) throws MaltChainedException { 097 if (value == null || value.equals("")) { 098 throw new OptionException("The legal value is missing for the '"+getName()+"' option. "); 099 } else if (legalValues.contains(value.toLowerCase())) { 100 throw new OptionException("The legal value '"+value+"' already exists for the '"+getName()+"' option. "); 101 } else { 102 legalValues.add(value.toLowerCase()); 103 if (desc == null || desc.equals("")) { 104 legalValueDesc.put(value.toLowerCase(), "Description is missing. "); 105 } else { 106 legalValueDesc.put(value.toLowerCase(), desc); 107 } 108 } 109 } 110 111 /** 112 * Adds a legal value without a description 113 * 114 * @param value a legal value name 115 * @throws OptionException 116 */ 117 public void addLegalValue(String value) throws MaltChainedException { 118 addLegalValue(value, null); 119 } 120 121 /* (non-Javadoc) 122 * @see org.maltparser.core.options.option.Option#getStringRepresentation(java.lang.Object) 123 */ 124 public String getStringRepresentation(Object value) { 125 if (value instanceof String && legalValues.contains(value)) { 126 return value.toString(); 127 } 128 return null; 129 } 130 131 /* (non-Javadoc) 132 * @see org.maltparser.core.options.option.Option#toString() 133 */ 134 public String toString() { 135 final StringBuilder sb = new StringBuilder(); 136 sb.append(super.toString()); 137 Formatter formatter = new Formatter(sb); 138 for (String value : legalValues) { 139 formatter.format("%2s%-10s - %-20s\n", "", value, legalValueDesc.get(value)); 140 } 141 sb.append("-----------------------------------------------------------------------------\n"); 142 return sb.toString(); 143 } 144}