MATSIM
MyConfigGroup.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2014 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package tutorial.programming.reflectiveConfigGroup;
21 
22 import org.matsim.api.core.v01.Coord;
23 import org.matsim.api.core.v01.Id;
26 
27 
32 public class MyConfigGroup extends ReflectiveConfigGroup {
33 
34  public static final String GROUP_NAME = "testModule";
35 
36  // TODO: test for ALL primitive types
37  private double doubleField = Double.NaN;
38 
39  // Object fields:
40  // Id: string representation is toString
41  private Id<Link> idField = null;
42  // Coord: some conversion needed
43  private Coord coordField = null;
44  // enum: handled especially
45  private MyEnum enumField = null;
46  // field without null conversion
47  private String nonNull = "some arbitrary default value.";
48 
49  public MyConfigGroup() {
50  super( GROUP_NAME );
51  }
52 
53  // /////////////////////////////////////////////////////////////////////
54  // primitive type field: standard getter and setter suffice
55  @StringGetter( "doubleField" )
56  public double getDoubleField() {
57  return this.doubleField;
58  }
59 
60  // there should be no restriction on return type of
61  // setters
62  @StringSetter( "doubleField" )
63  public double setDoubleField(double doubleField) {
64  final double old = this.doubleField;
65  this.doubleField = doubleField;
66  return old;
67  }
68 
69  // /////////////////////////////////////////////////////////////////////
70  // id field: need for a special setter, normal getter suffice
75  @StringGetter( "idField" )
76  public Id<Link> getIdField() {
77  return this.idField;
78  }
79 
80  public void setIdField(Id<Link> idField) {
81  this.idField = idField;
82  }
83 
90  @StringSetter( "idField" )
91  private void setIdField(String s) {
92  // Null handling needs to be done manually if conversion "by hand"
93  this.idField = s == null ? null : Id.create( s, Link.class );
94  }
95 
96  // /////////////////////////////////////////////////////////////////////
97  // coord field: need for special getter and setter
98  public Coord getCoordField() {
99  return this.coordField;
100  }
101 
102  public void setCoordField(Coord coordField) {
103  this.coordField = coordField;
104  }
105 
106  // we have to convert both ways here.
107  // the annotated getter and setter can be private to avoid polluting the
108  // interface: the user just sees the "typed" getter and setter.
109  @StringGetter( "coordField" )
110  private String getCoordFieldString() {
111  // Null handling needs to be done manually if conversion "by hand"
112  // Note that one *needs" to return a null pointer, not the "null"
113  // String, which is reserved word.
114  return this.coordField == null ? null : this.coordField.getX()+","+this.coordField.getY();
115  }
116 
117  @StringSetter( "coordField" )
118  private void setCoordField(String coordField) {
119  if ( coordField == null ) {
120  // Null handling needs to be done manually if conversion "by hand"
121  this.coordField = null;
122  return;
123  }
124 
125  final String[] coords = coordField.split( "," );
126  if ( coords.length != 2 ) throw new IllegalArgumentException( coordField );
127 
128  this.coordField = new Coord(Double.parseDouble(coords[0]), Double.parseDouble(coords[1]));
129  }
130 
131  // /////////////////////////////////////////////////////////////////////////
132  // Non-null string: standard setter and getter
133  @StringGetter( "nonNullField" )
134  @DoNotConvertNull
135  public String getNonNull() {
136  return nonNull;
137  }
138 
139  @StringSetter( "nonNullField" )
140  @DoNotConvertNull
141  public void setNonNull( String nonNull ) {
142  // in case the setter is called from user code, we need to check for nullity ourselves.
143  if ( nonNull == null ) throw new IllegalArgumentException();
144  this.nonNull = nonNull;
145  }
146 
147  // /////////////////////////////////////////////////////////////////////
148  // enum: normal getter and setter suffice
149  @StringGetter( "enumField" )
150  public MyEnum getTestEnumField() {
151  return this.enumField;
152  }
153 
154  @StringSetter( "enumField" )
155  public void setTestEnumField(final MyEnum enumField) {
156  // no need to test for null: the parent class does it for us
157  this.enumField = enumField;
158  }
159 }