1
1
package uk .ac .ox .oxfish .maximization .generic ;
2
2
3
- import com .google .common .collect .ImmutableList ;
4
3
import org .apache .commons .beanutils .PropertyUtils ;
5
4
import uk .ac .ox .oxfish .model .scenario .Scenario ;
6
5
7
6
import java .lang .reflect .InvocationTargetException ;
8
- import java .util .List ;
9
- import java .util .Map .Entry ;
10
- import java .util .function .Consumer ;
11
- import java .util .function .Supplier ;
12
- import java .util .regex .Pattern ;
13
7
14
8
import static com .google .common .base .Preconditions .checkArgument ;
15
9
import static com .google .common .base .Preconditions .checkNotNull ;
16
- import static uk .ac .ox .oxfish .utility .FishStateUtilities .entry ;
17
10
18
11
public class ParameterAddress {
19
12
private final String address ;
@@ -23,150 +16,26 @@ public ParameterAddress(final String address) {
23
16
this .address = address ;
24
17
}
25
18
26
- private static Entry <String , String > splitBy (final String s , final String sep ) {
27
- final String [] strings = s .split (Pattern .quote (sep ));
28
- checkArgument (
29
- strings .length == 2 ,
30
- "There should be one and only one % in the string %" , sep , s
31
- );
32
- return entry (strings [0 ], strings [1 ]);
33
- }
34
-
35
- public Supplier <Object > getGetter (final Scenario scenario ) {
36
- return getProperty (scenario ).getGetter ();
37
- }
38
-
39
- public Property getProperty (final Scenario scenario ) {
40
- return getProperty (scenario , ImmutableList .copyOf (this .address .split ("\\ ." )));
41
- }
42
-
43
- private Property getProperty (final Object object , final List <String > address ) {
44
- checkArgument (!address .isEmpty ());
45
- final String head = address .get (0 );
46
- final Property property ;
47
- if (head .contains ("$" )) {
48
- final Entry <String , String > entry = splitBy (head , "$" );
49
- property = new IndexedProperty (object , entry .getKey (), Integer .parseInt (entry .getValue ()));
50
- } else if (head .contains ("~" )) {
51
- final Entry <String , String > entry = splitBy (head , "~" );
52
- property = new MappedProperty (object , entry .getKey (), entry .getValue ());
53
- } else {
54
- property = new Property (object , head );
19
+ public Object getValue (final Scenario scenario ) {
20
+ try {
21
+ return PropertyUtils .getProperty (scenario , address );
22
+ } catch (
23
+ final IllegalAccessException | InvocationTargetException | NoSuchMethodException e
24
+ ) {
25
+ throw new RuntimeException (e );
55
26
}
56
- return address .size () == 1
57
- ? property
58
- : getProperty (property .getGetter ().get (), address .subList (1 , address .size ()));
59
- }
60
-
61
- public Consumer <Object > getSetter (final Scenario scenario ) {
62
- return getProperty (scenario ).getSetter ();
63
27
}
64
28
65
- private static class Property {
66
- private final Object bean ;
67
- private final String name ;
68
-
69
- private Property (final Object bean , final String name ) {
70
- this .bean = bean ;
71
- this .name = name ;
72
- }
73
-
74
- public Object getBean () {
75
- return bean ;
76
- }
77
-
78
- public String getName () {
79
- return name ;
80
- }
81
-
82
- public Supplier <Object > getGetter () {
83
- return () -> {
84
- try {
85
- return PropertyUtils .getProperty (bean , name );
86
- } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
87
- throw new RuntimeException (e );
88
- }
89
- };
90
- }
91
-
92
- public Consumer <Object > getSetter () {
93
- return (value ) -> {
94
- try {
95
- PropertyUtils .setProperty (bean , name , value );
96
- } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
97
- throw new RuntimeException (e );
98
- }
99
- };
100
- }
101
- }
102
-
103
- private static class IndexedProperty extends Property {
104
- private final int index ;
105
-
106
- private IndexedProperty (final Object bean , final String name , final int index ) {
107
- super (bean , name );
108
- this .index = index ;
109
- }
110
-
111
- public int getIndex () {
112
- return index ;
113
- }
114
-
115
- @ Override
116
- public Supplier <Object > getGetter () {
117
- return () -> {
118
- try {
119
- return PropertyUtils .getIndexedProperty (getBean (), getName (), index );
120
- } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
121
- throw new RuntimeException (e );
122
- }
123
- };
124
- }
125
-
126
- @ Override
127
- public Consumer <Object > getSetter () {
128
- return (value ) -> {
129
- try {
130
- PropertyUtils .setIndexedProperty (getBean (), getName (), index , value );
131
- } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
132
- throw new RuntimeException (e );
133
- }
134
- };
135
- }
136
- }
137
-
138
- private static class MappedProperty extends Property {
139
- private final String key ;
140
-
141
- private MappedProperty (final Object bean , final String name , final String key ) {
142
- super (bean , name );
143
- this .key = key ;
144
- }
145
-
146
- public String getKey () {
147
- return key ;
148
- }
149
-
150
- @ Override
151
- public Supplier <Object > getGetter () {
152
- return () -> {
153
- try {
154
- return PropertyUtils .getMappedProperty (getBean (), getName (), key );
155
- } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
156
- throw new RuntimeException (e );
157
- }
158
- };
159
- }
160
-
161
- @ Override
162
- public Consumer <Object > getSetter () {
163
- return (value ) -> {
164
- try {
165
- PropertyUtils .setMappedProperty (getBean (), getName (), key , value );
166
- } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
167
- throw new RuntimeException (e );
168
- }
169
- };
29
+ public void setValue (
30
+ final Scenario scenario ,
31
+ final Object value
32
+ ) {
33
+ try {
34
+ PropertyUtils .setProperty (scenario , address , value );
35
+ } catch (
36
+ final IllegalAccessException | InvocationTargetException | NoSuchMethodException e
37
+ ) {
38
+ throw new RuntimeException (e );
170
39
}
171
40
}
172
41
0 commit comments