1 | package geniusweb.domaineditor.model;
|
---|
2 |
|
---|
3 | import geniusweb.issuevalue.DiscreteValueSet;
|
---|
4 | import geniusweb.issuevalue.NumberValueSet;
|
---|
5 | import geniusweb.issuevalue.ValueSet;
|
---|
6 | import tudelft.utilities.logging.Reporter;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Types of {@link ValueSetModel} and Model factory
|
---|
10 | *
|
---|
11 | */
|
---|
12 | public enum ValueSetModelType {
|
---|
13 | DISCRETE(DiscreteValueSetModel.class), NUMBER(NumberValueSetModel.class);
|
---|
14 |
|
---|
15 | private Class<? extends ValueSetModel> modelclass;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | *
|
---|
19 | * @param clas the model class matching with this type.
|
---|
20 | */
|
---|
21 | ValueSetModelType(Class<? extends ValueSetModel> clas) {
|
---|
22 | this.modelclass = clas;
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | *
|
---|
27 | * @param valueset the {@link ValueSet} to get type of
|
---|
28 | * @return the type
|
---|
29 | */
|
---|
30 | public static ValueSetModelType fromModel(ValueSet valueset) {
|
---|
31 | if (valueset instanceof DiscreteValueSet)
|
---|
32 | return DISCRETE;
|
---|
33 | if (valueset instanceof NumberValueSet)
|
---|
34 | return NUMBER;
|
---|
35 | throw new IllegalArgumentException(
|
---|
36 | "valueset of Unknown class " + valueset);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Find the Type for the given model.
|
---|
41 | *
|
---|
42 | * @param model {@link ValueSetModel}
|
---|
43 | * @return {@link ValueSetModelType}
|
---|
44 | */
|
---|
45 | public static ValueSetModelType fromModel(ValueSetModel model) {
|
---|
46 | for (ValueSetModelType type : ValueSetModelType.values()) {
|
---|
47 | if (model.getClass().equals(type.modelclass))
|
---|
48 | return type;
|
---|
49 | }
|
---|
50 | throw new IllegalArgumentException("Model of Unknown class " + model);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static ValueSetModel createModel(ValueSetModelType type,
|
---|
54 | Reporter log) {
|
---|
55 | switch (type) {
|
---|
56 | case DISCRETE:
|
---|
57 | return new DiscreteValueSetModel(log);
|
---|
58 | case NUMBER:
|
---|
59 | return new NumberValueSetModel(log);
|
---|
60 | }
|
---|
61 | return null; // unreachable code
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | *
|
---|
66 | * @return the other type
|
---|
67 | */
|
---|
68 | public ValueSetModelType otherType() {
|
---|
69 | return this == DISCRETE ? NUMBER : DISCRETE;
|
---|
70 | }
|
---|
71 | }
|
---|