summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/ObjectListParameter.java
blob: cfd74d9ae55504e157571fbc5f95aa172034f2ce (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
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) 2012
 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.List;

import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil;
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;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;

/**
 * Parameter that represents a list of objects (in contrast to a class list parameter, they will be instanced at most once.)
 * 
 * @author Erich Schubert
 *
 * @param <C> Class type
 */
// TODO: add missing constructors.
public class ObjectListParameter<C> extends ClassListParameter<C> {
  /**
   * Cache for the generated instances.
   */
  private ArrayList<C> instances = null;
  
  /**
   * Constructor with optional flag.
   * 
   * @param optionID Option ID
   * @param restrictionClass Restriction class
   * @param optional optional flag
   */
  public ObjectListParameter(OptionID optionID, Class<?> restrictionClass, boolean optional) {
    super(optionID, restrictionClass, optional);
  }

  /**
   * Constructor for non-optional.
   * 
   * @param optionID Option ID
   * @param restrictionClass Restriction class
   */
  public ObjectListParameter(OptionID optionID, Class<?> restrictionClass) {
    super(optionID, restrictionClass);
  }
  
  /** {@inheritDoc} */
  @Override
  public String getSyntax() {
    return "<object_1|class_1,...,object_n|class_n>";
  }

  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  protected List<Class<? extends C>> parseValue(Object obj) throws ParameterException {
    if(obj == null) {
      throw new UnspecifiedParameterException("Parameter Error.\n" + "No value for parameter \"" + getName() + "\" " + "given.");
    }
    if (List.class.isInstance(obj)) {
      List<?> l = (List<?>) obj;
      ArrayList<C> inst = new ArrayList<C>(l.size());
      ArrayList<Class<? extends C>> classes = new ArrayList<Class<? extends C>>(l.size());
      for (Object o : l) {
        // does the given objects class fit?
        if (restrictionClass.isInstance(o)) {
          inst.add((C) o);
          classes.add((Class<? extends C>) o.getClass());
        } else if (o instanceof Class) {
          if (restrictionClass.isAssignableFrom((Class<?>)o)) {
          inst.add(null);
          classes.add((Class<? extends C>) o);
          } else {
            throw new WrongParameterValueException(this, ((Class<?>)o).getName(), "Given class not a subclass / implementation of " + restrictionClass.getName());
          }
        } else {
          throw new WrongParameterValueException(this, o.getClass().getName(), "Given instance not an implementation of " + restrictionClass.getName());
        }
      }
      this.instances = inst;
      return super.parseValue(classes);
    }
    // Did we get a single instance?
    try {
      C inst = restrictionClass.cast(obj);
      this.instances = new ArrayList<C>(1);
      this.instances.add(inst);
      return super.parseValue(inst.getClass());
    } catch (ClassCastException e) {
      // Continue
    }
    return super.parseValue(obj);
  }

  /** {@inheritDoc} */
  @Override
  public List<C> instantiateClasses(Parameterization config) {
    if (instances == null) {
      // instantiateClasses will descend itself.
      instances = new ArrayList<C>(super.instantiateClasses(config));
    } else {
      Parameterization cfg = null;
      for (int i = 0; i < instances.size(); i++) {
        if (instances.get(i) == null) {
          Class<? extends C> cls = getValue().get(i);
          try {
            // Descend at most once, and only when needed
            if (cfg == null) {
              cfg = config.descend(this);
            }
            C instance = ClassGenericsUtil.tryInstantiate(restrictionClass, cls, cfg);
            instances.set(i, instance);
          }
          catch(Exception e) {
            config.reportError(new WrongParameterValueException(this, cls.getName(), e));
          }
        }
      }
    }
    return new ArrayList<C>(instances);
  } 
}