[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
[99] | 3 | import com.framsticks.params.types.EventParam; |
---|
[90] | 4 | import com.framsticks.params.types.ProcedureParam; |
---|
[84] | 5 | |
---|
[77] | 6 | /** |
---|
| 7 | * The Class PropertiesAccess. |
---|
[84] | 8 | * |
---|
[77] | 9 | * @author Jarek Szymczak <name.surname@gmail.com> (please replace name and |
---|
| 10 | * surname with my personal data) |
---|
| 11 | * |
---|
| 12 | * @author Piotr Śniegowski |
---|
| 13 | */ |
---|
| 14 | public class PropertiesAccess extends SimpleAbstractAccess { |
---|
| 15 | |
---|
[105] | 16 | protected PropertiesObject properties; |
---|
[77] | 17 | |
---|
[84] | 18 | @Override |
---|
[105] | 19 | public PropertiesObject createAccessee() { |
---|
| 20 | return new PropertiesObject(framsClass.getId()); |
---|
[78] | 21 | } |
---|
| 22 | |
---|
[77] | 23 | public PropertiesAccess(FramsClass framsClass) { |
---|
[87] | 24 | super(framsClass); |
---|
[77] | 25 | } |
---|
| 26 | |
---|
| 27 | @Override |
---|
| 28 | public void clearValues() { |
---|
[84] | 29 | assert properties != null; |
---|
| 30 | properties.clear(); |
---|
[77] | 31 | } |
---|
| 32 | |
---|
| 33 | @Override |
---|
[84] | 34 | public <T> T get(ValueParam param, Class<T> type) { |
---|
| 35 | assert properties != null; |
---|
| 36 | assert param != null; |
---|
[105] | 37 | Object object = properties.get(param.getId(), Object.class); |
---|
| 38 | if (object == null) { |
---|
| 39 | return null; |
---|
[85] | 40 | } |
---|
[77] | 41 | try { |
---|
[105] | 42 | return type.cast(object); |
---|
[77] | 43 | } catch (ClassCastException e) { |
---|
[105] | 44 | throw (ClassCastException) new ClassCastException("property " + param + " type is " + object.getClass().getName() + ", not " + type.getName()).initCause(e); |
---|
[77] | 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
[84] | 48 | @Override |
---|
| 49 | protected <T> void internalSet(ValueParam param, T value) { |
---|
[105] | 50 | properties.set(param.getId(), value); |
---|
[77] | 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Sets the new values to operate on. It does not check whether the values |
---|
| 55 | * which are set through this method are correct. If set values are not |
---|
| 56 | * correct exceptions might occurred while getting / setting the parameters |
---|
| 57 | * values |
---|
[84] | 58 | * |
---|
[77] | 59 | * @param object |
---|
| 60 | * the properties with parameters values |
---|
| 61 | */ |
---|
| 62 | @Override |
---|
[84] | 63 | public PropertiesAccess select(Object object) { |
---|
[105] | 64 | properties = ParamsUtil.selectObjectForAccess(this, object, PropertiesObject.class); |
---|
[84] | 65 | return this; |
---|
[77] | 66 | } |
---|
| 67 | |
---|
| 68 | /** covariant */ |
---|
| 69 | @Override |
---|
[105] | 70 | public PropertiesObject getSelected() { |
---|
[77] | 71 | return properties; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | @Override |
---|
| 75 | public PropertiesAccess cloneAccess() { |
---|
| 76 | return new PropertiesAccess(framsClass); |
---|
| 77 | } |
---|
| 78 | |
---|
[87] | 79 | @Override |
---|
[96] | 80 | public void tryAutoAppend(Object object) { |
---|
| 81 | throw new InvalidOperationException(); |
---|
[87] | 82 | } |
---|
[77] | 83 | |
---|
[90] | 84 | @Override |
---|
[105] | 85 | public Object call(String id, Object... arguments) { |
---|
[99] | 86 | throw new InvalidOperationException().msg("properties access does not support calling methods").arg("id", id); |
---|
[90] | 87 | } |
---|
| 88 | |
---|
| 89 | @Override |
---|
[105] | 90 | public Object call(ProcedureParam param, Object... arguments) { |
---|
[99] | 91 | throw new InvalidOperationException().msg("properties access does not support calling methods").arg("param", param); |
---|
[90] | 92 | } |
---|
| 93 | |
---|
[99] | 94 | @Override |
---|
| 95 | public void reg(EventParam param, EventListener<?> listener) { |
---|
| 96 | throw new InvalidOperationException().msg("properties access does not support registering events").arg("param", param).arg("access", this); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | @Override |
---|
| 100 | public void regRemove(EventParam param, EventListener<?> listener) { |
---|
| 101 | throw new InvalidOperationException().msg("properties access does not support registering events").arg("param", param).arg("access", this); |
---|
| 102 | } |
---|
| 103 | |
---|
[100] | 104 | @Override |
---|
| 105 | public String toString() { |
---|
| 106 | StringBuilder b = new StringBuilder(); |
---|
| 107 | b.append(framsClass); |
---|
| 108 | if (getSelected() != null) { |
---|
| 109 | b.append("(").append(getSelected().size()).append(")"); |
---|
| 110 | } |
---|
| 111 | return b.toString(); |
---|
| 112 | } |
---|
[77] | 113 | } |
---|