source: java/main/src/main/java/com/framsticks/params/types/BooleanParam.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: 1.5 KB
Line 
1package com.framsticks.params.types;
2
3import javax.annotation.concurrent.Immutable;
4
5import com.framsticks.params.CastFailure;
6import com.framsticks.params.ParamBuilder;
7import com.framsticks.params.PrimitiveParam;
8import com.framsticks.params.ReassignResult;
9import com.framsticks.params.SinkInterface;
10import com.framsticks.util.lang.Numbers;
11
12/**
13 * @author Piotr Sniegowski
14 */
15@Immutable
16public class BooleanParam extends PrimitiveParam<Boolean> {
17
18        /**
19         * @param builder
20         */
21        public BooleanParam(ParamBuilder builder) {
22                super(builder.defaultDef(false));
23        }
24
25        @Override
26        public Class<?> getStorageType() {
27                return Boolean.class;
28        }
29
30        @Override
31        public ReassignResult<Boolean> reassign(Object newValue, Object oldValue) throws CastFailure {
32                if (newValue instanceof Boolean) {
33                        return ReassignResult.create((Boolean) newValue);
34                }
35                if (newValue instanceof Integer) {
36                        return ReassignResult.create(((Integer) newValue) != 0);
37                }
38                if (newValue instanceof String) {
39                        if ("true".equals(newValue)) {
40                                return ReassignResult.create(true);
41                        }
42                        if ("false".equals(newValue)) {
43                                return ReassignResult.create(false);
44                        }
45                        Integer i = Numbers.cast(newValue, Integer.class);
46                        if (i != null) {
47                                return ReassignResult.create(i != 0);
48                        }
49                        throw new CastFailure();
50                }
51                throw new CastFailure();
52        }
53
54        @Override
55        public String getFramsTypeName() {
56                return "d 0 1";
57        }
58
59        @Override
60        public void save(SinkInterface sink, Object value) {
61                assert value instanceof Boolean;
62                sink.print(((Boolean)value) ? "1" : "0");
63        }
64}
Note: See TracBrowser for help on using the repository browser.