001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.core.options;
021    
022    import java.util.Arrays;
023    import java.util.Set;
024    import java.util.TreeSet;
025    
026    /**
027     * A configuration option, which allows values of type String. Optionally a set
028     * of allowed strings can be set. By default all strings are allowed.
029     * 
030     * @author Jens Lehmann
031     * 
032     */
033    public class StringConfigOption extends ConfigOption<String> {
034    
035            private Set<String> allowedValues = new TreeSet<String>();
036    
037            public StringConfigOption(String name, String description) {
038                    super(name, description);
039            }       
040            
041            public StringConfigOption(String name, String description, String defaultValue) {
042                    super(name, description, defaultValue);
043            }
044    
045            public StringConfigOption(String name, String description, String defaultValue, boolean mandatory, boolean requiresInit) {
046                    super(name, description, defaultValue, mandatory, requiresInit);        
047            }
048    
049            /* (non-Javadoc)
050             * @see org.dllearner.core.config.ConfigOption#getDefaultValue()
051             */
052            @Override
053            public String getDefaultValueInJava() {
054                    return (defaultValue == null)?null:"\""+defaultValue+"\"";
055            }
056            
057            /* (non-Javadoc)
058             * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString()
059             */
060            @Override
061            public String getValueTypeAsJavaString(){
062                    return "String";
063            }
064            
065            /*
066             * (non-Javadoc)
067             * 
068             * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object)
069             */
070            @Override
071            public boolean isValidValue(String value) {
072                    if (allowedValues.size() == 0 || allowedValues.contains(value))
073                            return true;
074                    else
075                            return false;
076            }
077    
078            /**
079             * @return the allowedValues
080             */
081            public Set<String> getAllowedValues() {
082                    return allowedValues;
083            }
084            
085            /* (non-Javadoc)
086             * @see org.dllearner.core.options.ConfigOption#getAllowedValuesDescription()
087             */
088            @Override
089            public String getAllowedValuesDescription() {
090                    return getValueTypeAsJavaString() + " "+allowedValues+" ";
091            }
092    
093            /**
094             * @param allowedValues
095             *            the allowedValues to set
096             */
097            public void setAllowedValues(Set<String> allowedValues) {
098                    this.allowedValues = allowedValues;
099            }
100    
101            public void setAllowedValues(String[] allowedValues) {
102                    this.allowedValues = new TreeSet<String>(Arrays.asList(allowedValues));
103            }
104    
105            /*
106             * (non-Javadoc)
107             * 
108             * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object)
109             */
110            @Override
111            public boolean checkType(Object object) {
112                    return (object instanceof String);
113            }
114    
115            /*
116             * (non-Javadoc)
117             * 
118             * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object)
119             */
120            @Override
121            public String getValueFormatting(String value) {
122                    if (value != null)
123                            return "\"" + value.toString() + "\";";
124                    else
125                            return null;
126            }
127    
128    }