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