source: java/main/src/main/java/com/framsticks/params/PrimitiveParam.java @ 87

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

HIGHLIGHTS:

  • FramsClass? and contained Param are now immutable classes (like String),

which allows to refer to them concurrently without synchronization
(which for example in turn simplifies GUI management)

  • also make Path immutable (which was earlier only assumed)
  • add global cache for FramsClasses? created solely and automatically

on base of Java classes.

representations basing on given FramsClass?

  • above changes greatly improved GUI responsivness during browsing
  • furtherly improve Param class hierarchy
  • allow to inject actions on state changes into MultiParamLoader?
  • add more tests

CHANGELOG:

Add StatusListener? to MultiParamLoader?.

Minor refactorization in MultiParamLoader?.

First step with auto append.

Add SchemaTest?.

Improve Registry.

Clean up in Registry.

Work out Registry.

Use annotations for Param.

Fix ListChange?.

Improve fluent interface of the FramsClassBuilder?.

Done caching of ReflectionAccess?.Backend

Fix hashCode of Pair.

A step on a way to cache ReflectionAccess?.Backend

Make SimpleAbstractAccess?.framsClass a final field.

Add static cache for FramsClasses? based on java.

Only classes created strictly and automatically
based on java classes are using this cache.

Make all Params immutable.

Many improvement to make Param immutable.

Make PrimitiveParam? generic type.

Several changes to make Param immutable.

Make FramsClass? immutable.

Another improvement to Path immutability.

Several improvements to Path.

Improve PathTest?.

Configurarable MutabilityDetector?.

File size: 2.6 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.util.FramsticksException;
4import com.framsticks.util.lang.Numbers;
5
6import javax.annotation.concurrent.Immutable;
7
8/**
9 * @author Piotr Sniegowski
10 */
11@Immutable
12public abstract class PrimitiveParam<S> extends ValueParam {
13
14        /** The minimum allowed value of parameter. */
15        protected final Object min;
16
17        /** The maximum allowed value of parameter. */
18        protected final Object max;
19
20        /** The default value of parameter. */
21        protected final Object def;
22
23        /**
24         * @param builder
25         */
26        public PrimitiveParam(ParamBuilder builder) {
27                super(builder);
28                min = builder.getMin();
29                max = builder.getMax();
30                def = builder.getDef();
31        }
32
33        public void save(SinkInterface sink, Object value) {
34                sink.print(value);
35        }
36
37        protected <T> T tryCastAndReturn(Object value, Class<T> type) {
38                if (value == null) {
39                        return null;
40                }
41                try {
42                        if (type.equals(Object.class)) {
43                                return type.cast(value);
44                        }
45                        if (value instanceof String) {
46                                if (type.equals(String.class)) {
47                                        return type.cast(value);
48                                }
49                                // if (Number.class.isAssignableFrom(type)) {
50                                return Numbers.parse((String) value, type);
51                                // }
52                                // return null;
53                        } else {
54                                return type.cast(value);
55                        }
56                } catch (ClassCastException e) {
57                        throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type);
58                }
59        }
60
61        /**
62         * Gets the minimum value of parameter.
63         *
64         * @param <T> the generic getType which must be correctly specified by user
65         * @param type the getType of variable, can be checked by
66         * @return the minimum allowed value of parameter
67         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
68         */
69        public <T> T getMin(Class<T> type) {
70                return tryCastAndReturn(min, type);
71        }
72
73        /**
74         * Gets the maximum value of parameter.
75         *
76         * @param <T> the generic getType which must be correctly specified by user
77         * @param type the getType of variable, can be checked by {@link #getStorageType()}
78         * @return the maximum allowed value of parameter
79         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
80         */
81        public <T> T getMax(Class<T> type) {
82                return tryCastAndReturn(max, type);
83        }
84
85        /**
86         * Gets the default value of parameter.
87         *
88         * @param <T> the generic getType which must be correctly specified by user
89         * @param type the getType of variable, can be checked by {@link #getStorageType()}
90         * @return the default value of parameter
91         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
92         */
93        public <T> T getDef(Class<T> type) {
94                return tryCastAndReturn(def, type);
95        }
96}
Note: See TracBrowser for help on using the repository browser.