summaryrefslogtreecommitdiff
path: root/elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/EnumParameter.java
blob: 0cad6e113024398b608fec92b666763f5dcab285 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2015
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Collection;

import de.lmu.ifi.dbs.elki.utilities.FormatUtil;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException;

/**
 * Parameter class for a parameter specifying an enum type.
 * 
 * <p>
 * Usage:
 * 
 * <pre>
 * // Enum declaration.
 * enum MyEnum { VALUE1, VALUE2 };
 * // Parameter value holder.
 * MyEnum myEnumParameter;
 * 
 * // ...
 * 
 * // Parameterization.
 * EnumParameter&lt;MyEnum&gt; param = new EnumParameter&lt;MyEnum&gt;(ENUM_PROPERTY_ID, MyEnum.class);
 * // OR
 * EnumParameter&lt;MyEnum&gt; param = new EnumParameter&lt;MyEnum&gt;(ENUM_PROPERTY_ID, MyEnum.class, MyEnum.VALUE1);
 * // OR
 * EnumParameter&lt;MyEnum&gt; param = new EnumParameter&lt;MyEnum&gt;(ENUM_PROPERTY_ID, MyEnum.class, true);
 * 
 * if(config.grab(param)) {
 *   myEnumParameter = param.getValue();
 * }
 * 
 * </p>
 * 
 * @author Florian Nuecke
 * 
 * @param <E> Enum type
 */
public class EnumParameter<E extends Enum<E>> extends AbstractParameter<EnumParameter<E>, E> {

  /**
   * Reference to the actual enum type, for T.valueOf().
   */
  protected Class<E> enumClass;

  /**
   * Constructs an enum parameter with the given optionID, constraints and
   * default value.
   * 
   * @param optionID the unique id of the parameter
   * @param defaultValue the default value of the parameter
   */
  public EnumParameter(OptionID optionID, Class<E> enumClass, E defaultValue) {
    super(optionID, defaultValue);
    this.enumClass = enumClass;
  }

  /**
   * Constructs an enum parameter with the given optionID, constraints and
   * default value.
   * 
   * @param optionID the unique id of the parameter
   * @param optional Flag to signal an optional parameter.
   */
  public EnumParameter(OptionID optionID, Class<E> enumClass, boolean optional) {
    super(optionID, optional);
    this.enumClass = enumClass;
  }

  /**
   * Constructs an enum parameter with the given optionID, constraints and
   * default value.
   * 
   * @param optionID the unique id of the parameter
   */
  public EnumParameter(OptionID optionID, Class<E> enumClass) {
    super(optionID);
    this.enumClass = enumClass;
  }

  @Override
  public String getSyntax() {
    return "<" + joinEnumNames(" | ") + ">";
  }

  @Override
  protected E parseValue(Object obj) throws ParameterException {
    if(obj == null) {
      throw new UnspecifiedParameterException(this);
    }
    if(enumClass.isInstance(obj)) {
      return enumClass.cast(obj);
    }
    if(obj instanceof String) {
      try {
        return Enum.valueOf(enumClass, (String) obj);
      }
      catch(IllegalArgumentException ex) {
        throw new WrongParameterValueException("Enum parameter " + getName() + " is invalid (must be one of [" + joinEnumNames(", ") + "].");
      }
    }
    throw new WrongParameterValueException("Enum parameter " + getName() + " is not given as a string.");
  }

  @Override
  public String getValueAsString() {
    return getValue().name();
  }

  /**
   * This class sometimes provides a list of value descriptions.
   * 
   * @see de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter#hasValuesDescription()
   */
  @Override
  public boolean hasValuesDescription() {
    return true;
  }

  @Override
  public String getValuesDescription() {
    StringBuilder buf = new StringBuilder();
    buf.append("One of:").append(FormatUtil.NEWLINE);
    for(String s : getPossibleValues()) {
      buf.append("->").append(FormatUtil.NONBREAKING_SPACE).append(s).append(FormatUtil.NEWLINE);
    }
    return buf.toString();
  }

  /**
   * Get a list of possible values for this enum parameter.
   * 
   * @return list of strings representing possible enum values.
   */
  public Collection<String> getPossibleValues() {
    // Convert to string array
    final E[] enums = enumClass.getEnumConstants();
    ArrayList<String> values = new ArrayList<>(enums.length);
    for(E t : enums) {
      values.add(t.name());
    }
    return values;
  }

  /**
   * Utility method for merging possible values into a string for informational
   * messages.
   * 
   * @param separator char sequence to use as a separator for enum values.
   * @return <code>{VAL1}{separator}{VAL2}{separator}...</code>
   */
  private String joinEnumNames(String separator) {
    E[] enumTypes = enumClass.getEnumConstants();
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < enumTypes.length; ++i) {
      if(i > 0) {
        sb.append(separator);
      }
      sb.append(enumTypes[i].name());
    }
    return sb.toString();
  }

}