source: java/main/src/main/java/com/framsticks/params/Param.java @ 77

Last change on this file since 77 was 77, checked in by psniegowski, 11 years ago

Add new java codebase.

File size: 5.3 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.types.DecimalParam;
4import com.framsticks.params.types.StringParam;
5
6import java.util.HashMap;
7import java.util.Map;
8
9/**
10 * Based on c++ struct ParamEntry located in cpp/gdk/param.h
11 * Here  it is a root of Param hierarchy.
12 *
13 * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus
14 * (please replace name and surname with my personal data)
15 *
16 * @author Piotr Śniegowski
17 */
18public abstract class Param {
19
20        /** The parameter id. */
21        protected String id;
22
23        /**
24         * The parameter internal id. It's set by a user to use user's own getId in
25         * code
26         */
27        protected String internalId;
28
29        /** The parameter name. */
30        protected String name;
31
32        /** The help (description) concerning parameter. */
33        protected String help;
34
35        /** The number of group, that parameter belongs to. */
36        protected Integer group;
37
38        /** The getFlags stored as a bit sum. */
39        protected Integer flags;
40
41        /** The variable determining whether the parameter is an extra parameter. */
42        protected Integer extra;
43
44        /** The minimum allowed value of parameter. */
45        protected Object min;
46
47        /** The maximum allowed value of parameter. */
48        protected Object max;
49
50        /** The default value of parameter. */
51        protected Object def;
52
53
54
55        /**
56         * The defaults used for Integer, Double and String classes, if the default
57         * value of parameter is not explicitly given.
58         */
59        @SuppressWarnings("rawtypes")
60        private Map<Class, Object> defaults = new HashMap<Class, Object>();
61        // static initialization of defaults
62        {
63                defaults.put(String.class, "");
64                defaults.put(Integer.class, 0);
65                defaults.put(Double.class, 0.0);
66        }
67
68        public Param() {
69
70        }
71
72        public String getId() {
73                return id;
74        }
75
76        public String getInternalId() {
77                return internalId;
78        }
79
80        public String getName() {
81                return name;
82        }
83
84        public String getHelp() {
85                return help;
86        }
87
88        public Integer getGroup() {
89                return group;
90        }
91
92        public Integer getFlags() {
93                return flags;
94        }
95
96    public abstract String getType();
97
98
99        public String getEffectiveId() {
100                return (internalId != null) ? internalId : id;
101        }
102
103
104    private <T> T tryCastAndReturn(Object value, Class<T> type) {
105        if (value == null)
106            return null;
107        try {
108            return type.cast(value);
109        } catch (ClassCastException e) {
110            throw new ClassCastException("property \"" + name
111                    + "\" getType is \"" + value.getClass().getName()
112                    + "\", not \"" + type.getName() + "\"");
113        }
114    }
115        /**
116         * Gets the minimum value of parameter.
117         *
118         * @param <T> the generic getType which must be correctly specified by user
119         * @param type the getType of variable, can be checked by
120         * @return the minimum allowed value of parameter
121         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
122         */
123        public <T> T getMin(Class<T> type) {
124                return tryCastAndReturn(min, type);
125        }
126
127        /**
128         * Gets the maximum value of parameter.
129         *
130         * @param <T> the generic getType which must be correctly specified by user
131         * @param type the getType of variable, can be checked by {@link #getStorageType()}
132         * @return the maximum allowed value of parameter
133         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
134         */
135        public <T> T getMax(Class<T> type) {
136                return tryCastAndReturn(max, type);
137        }
138
139        /**
140         * Gets the default value of parameter.
141         *
142         * @param <T> the generic getType which must be correctly specified by user
143         * @param type the getType of variable, can be checked by {@link #getStorageType()}
144         * @return the default value of parameter
145         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
146         */
147        public <T> T getDef(Class<T> type) throws ClassCastException {
148                return tryCastAndReturn(def, type);
149        }
150
151        public Integer getExtra() {
152                return extra;
153        }
154
155        public void setInternalId(String internalId) {
156                this.internalId = internalId;
157        }
158
159        @Override
160        public String toString() {
161                return getId() + ":" + this.getClass().getSimpleName();
162        }
163
164        public boolean isNumeric() {
165                return false;
166        }
167
168        public boolean isEmptyAvailable() {
169                return false;
170        }
171
172        public abstract Class getStorageType();
173
174        public boolean hasFlag(int flag) {
175                return flags != null && (flags & flag) != 0;
176        }
177
178        public Object reassign(Object newValue, Object oldValue) throws CastFailure {
179        if (newValue == null) {
180            if (isEmptyAvailable()) {
181                return null;
182            }
183            throw new CastFailure();
184        }
185        try {
186            return getStorageType().cast(newValue);
187        } catch (ClassCastException ignored) {
188            throw new CastFailure();
189        }
190    }
191
192        public boolean isUserHidden() {
193                return (flags & Flags.USERHIDDEN) != 0;
194        }
195
196    public static FramsClass getFramsClass() {
197        return new FramsClass("prop", "prop", null)
198                .append(new ParamBuilder().setId("name").setName("Name").setType(StringParam.class).build())
199                .append(new ParamBuilder().setId("id").setName("Id").setType(StringParam.class).build())
200                .append(new ParamBuilder().setId("type").setName("Type").setType(StringParam.class).build())
201                .append(new ParamBuilder().setId("help").setName("Help").setType(StringParam.class).build())
202                .append(new ParamBuilder().setId("flags").setName("Flags").setType(DecimalParam.class).build());
203    }
204
205}
Note: See TracBrowser for help on using the repository browser.