1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import java.util.HashMap; |
---|
4 | import java.util.Map; |
---|
5 | |
---|
6 | |
---|
7 | /** |
---|
8 | * The Class PropertiesAccess. |
---|
9 | * |
---|
10 | * @author Jarek Szymczak <name.surname@gmail.com> (please replace name and |
---|
11 | * surname with my personal data) |
---|
12 | * |
---|
13 | * @author Piotr Śniegowski |
---|
14 | */ |
---|
15 | public class PropertiesAccess extends SimpleAbstractAccess { |
---|
16 | |
---|
17 | public Map<String, Object> properties; |
---|
18 | |
---|
19 | @Override |
---|
20 | public Map<String, Object> createAccessee() { |
---|
21 | return PropertiesAccess.createPropertiesMap(); |
---|
22 | } |
---|
23 | |
---|
24 | public static Map<String, Object> createPropertiesMap() { |
---|
25 | return new HashMap<String, Object>(); |
---|
26 | } |
---|
27 | |
---|
28 | public PropertiesAccess(FramsClass framsClass) { |
---|
29 | super(framsClass); |
---|
30 | } |
---|
31 | |
---|
32 | @Override |
---|
33 | public void clearValues() { |
---|
34 | assert properties != null; |
---|
35 | properties.clear(); |
---|
36 | } |
---|
37 | |
---|
38 | @Override |
---|
39 | public <T> T get(ValueParam param, Class<T> type) { |
---|
40 | assert properties != null; |
---|
41 | assert param != null; |
---|
42 | Object object = properties.get(param.getId()); |
---|
43 | if (object != null) { |
---|
44 | try { |
---|
45 | return type.cast(object); |
---|
46 | } catch (ClassCastException e) { |
---|
47 | throw (ClassCastException) new ClassCastException("property " + param + " type is " + object.getClass().getName() + ", not " + type.getName()).initCause(e); |
---|
48 | } |
---|
49 | } |
---|
50 | try { |
---|
51 | return param.getDef(type); |
---|
52 | } catch (ClassCastException e) { |
---|
53 | throw (ClassCastException) new ClassCastException("default value of property " + param + " is not of type " + type.getName()).initCause(e); |
---|
54 | } |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | protected <T> void internalSet(ValueParam param, T value) { |
---|
60 | properties.put(param.getId(), value); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Sets the new values to operate on. It does not check whether the values |
---|
65 | * which are set through this method are correct. If set values are not |
---|
66 | * correct exceptions might occurred while getting / setting the parameters |
---|
67 | * values |
---|
68 | * |
---|
69 | * @param object |
---|
70 | * the properties with parameters values |
---|
71 | */ |
---|
72 | @SuppressWarnings("unchecked") |
---|
73 | @Override |
---|
74 | public PropertiesAccess select(Object object) { |
---|
75 | this.properties = (Map<String, Object>)object; |
---|
76 | return this; |
---|
77 | } |
---|
78 | |
---|
79 | /** covariant */ |
---|
80 | @Override |
---|
81 | public Map<String, Object> getSelected() { |
---|
82 | return properties; |
---|
83 | } |
---|
84 | |
---|
85 | @Override |
---|
86 | public PropertiesAccess cloneAccess() { |
---|
87 | return new PropertiesAccess(framsClass); |
---|
88 | } |
---|
89 | |
---|
90 | @Override |
---|
91 | public boolean tryAutoAppend(Object object) { |
---|
92 | return false; |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|