1 | package com.framsticks.params.types; |
---|
2 | |
---|
3 | import com.framsticks.params.Access; |
---|
4 | import com.framsticks.params.CastFailure; |
---|
5 | import com.framsticks.params.ParamBuilder; |
---|
6 | import com.framsticks.params.ReassignResult; |
---|
7 | import com.framsticks.params.UniqueListAccess; |
---|
8 | import com.framsticks.util.lang.Casting; |
---|
9 | import com.framsticks.util.lang.Numbers; |
---|
10 | |
---|
11 | import java.util.Map; |
---|
12 | |
---|
13 | import javax.annotation.concurrent.Immutable; |
---|
14 | |
---|
15 | /** |
---|
16 | * @author Piotr Sniegowski |
---|
17 | */ |
---|
18 | @Immutable |
---|
19 | public class UniqueListParam extends ListParam { |
---|
20 | |
---|
21 | final String uidName; |
---|
22 | |
---|
23 | public UniqueListParam(ParamBuilder builder) { |
---|
24 | super(builder); |
---|
25 | this.uidName = builder.getUid(); |
---|
26 | } |
---|
27 | |
---|
28 | @Override |
---|
29 | public String computeAccessId() { |
---|
30 | return "l " + containedTypeName + " " + uidName; |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | @Override |
---|
35 | public Class<?> getStorageType() { |
---|
36 | return Map.class; |
---|
37 | } |
---|
38 | |
---|
39 | @Override |
---|
40 | public Access prepareAccess(Access access) { |
---|
41 | return new UniqueListAccess(access, uidName); |
---|
42 | } |
---|
43 | |
---|
44 | @Override |
---|
45 | public ReassignResult<? extends Map<?,?>> reassign(Object newValue, Object oldValue) throws CastFailure { |
---|
46 | if (newValue instanceof Map) { |
---|
47 | return new ReassignResult<Map<?,?>>((Map<?,?>) newValue); |
---|
48 | } |
---|
49 | Integer size = Numbers.cast(newValue, Integer.class); |
---|
50 | if (size != null) { |
---|
51 | //return oldValue; |
---|
52 | /* |
---|
53 | the integer value should be ignored, because this may cause, that object is created before |
---|
54 | information about it's elements is available, which would break resolution flow |
---|
55 | */ |
---|
56 | return new ReassignResult<Map<?,?>>(Casting.tryCast(Map.class, oldValue)); |
---|
57 | // return ReassignResult.create(new TreeMap<Object, Object>()); |
---|
58 | } |
---|
59 | |
---|
60 | throw new CastFailure(); |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public String getFramsTypeName() { |
---|
65 | return "l " + containedTypeName + " " + uidName; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | } |
---|