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