| 14 | public class IdempotencyMap<T> extends LinkedHashMapAndArrayList<T> implements Mutable<IdempotencyMap<T>>, fieldlinker.AsMap, HandlesCompletion { |
| 15 | |
| 16 | private final Class<? extends T> t; |
| 17 | private Function<String, T> autoConstructor; |
| 18 | |
| 19 | public IdempotencyMap(Class t) { |
| 20 | this.t = t; |
| 21 | } |
| 22 | |
| 23 | public IdempotencyMap<T> setAutoconstruct(Function<String, T> auto) { |
| 24 | this.autoConstructor = auto; |
| 25 | return this; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | public IdempotencyMap<T> setAutoconstruct(Class clazz) { |
| 30 | this.autoConstructor = (k) -> { |
| 31 | try { |
| 32 | return (T) clazz.newInstance(); |
| 33 | } catch (InstantiationException e) { |
| 34 | e.printStackTrace(); |
| 35 | } catch (IllegalAccessException e) { |
| 36 | e.printStackTrace(); |
| 37 | } |
| 38 | return null; |
| 39 | }; |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | protected T massage(Object value) { |
| 45 | if (t == null) return (T) value; |
| 46 | if (value == null) return null; |
| 47 | if (t.isAssignableFrom(value.getClass())) return (T) value; |
| 48 | |
| 49 | Object ovalue = value; |
| 50 | |
| 51 | if (value instanceof ScriptObjectMirror) |
| 52 | value = ScriptUtils.unwrap(value); |
| 53 | |
| 54 | value = Conversions.convert(value, t); |
| 55 | |
| 56 | if (value != null && t.isAssignableFrom(value.getClass())) return (T) value; |
| 57 | |
| 58 | if (value == null) { |
| 59 | throw new ClassCastException(" couldn't convert " + ovalue + " of class " + ovalue.getClass() + " to " + t); |
| 60 | } |
| 61 | if (!t.isAssignableFrom(value.getClass())) |
| 62 | throw new ClassCastException(" expected " + t + ", got " + value + " / " + value.getClass()); |
| 63 | |
| 64 | return (T) value; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public IdempotencyMap<T> duplicate() { |
| 69 | IdempotencyMap<T> r = new IdempotencyMap<>(t); |
| 70 | |
| 71 | for (Map.Entry<String, T> e : this.entrySet()) { |
| 72 | r.put(e.getKey(), e.getValue() instanceof Mutable ? ((Mutable) e.getValue()).duplicate() : e.getValue()); |
| 73 | } |
no outgoing calls
no test coverage detected