source: java/main/src/main/java/com/framsticks/params/types/BooleanParam.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

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