/** * Makes a deep copy of a {@link State} map. */ public static State deepCopy(State state) { return deepCopy(state, false); }
/** * Makes a deep copy of a value. */ public static Serializable deepCopy(Object value) { return deepCopy(value, false); }
/** * Makes a deep copy of a {@link List}. */ public static List<Serializable> deepCopy(List<Serializable> list) { return deepCopy(list, false); }
/** * Makes a deep copy of a {@link List}, optionally thread-safe. * * @param threadSafe if {@code true}, then thread-safe datastructures are used */ public static List<Serializable> deepCopy(List<Serializable> list, boolean threadSafe) { List<Serializable> copy = threadSafe ? new CopyOnWriteArrayList<Serializable>() : new ArrayList<Serializable>( list.size()); for (Serializable v : list) { copy.add(deepCopy(v, threadSafe)); } return copy; }
@Override public void createState(State state) { String id = (String) state.get(KEY_ID); if (log.isTraceEnabled()) { log.trace("Mem: CREATE " + id + ": " + state); } if (states.containsKey(id)) { throw new NuxeoException("Already exists: " + id); } state = StateHelper.deepCopy(state, true); // thread-safe StateHelper.resetDeltas(state); states.put(id, state); }
/** * Makes a deep copy of a value, optionally thread-safe. * * @param threadSafe if {@code true}, then thread-safe datastructures are used */ public static Serializable deepCopy(Object value, boolean threadSafe) { if (value == null) { return (Serializable) value; } else if (value instanceof State) { return deepCopy((State) value, threadSafe); } else if (value instanceof List) { @SuppressWarnings("unchecked") List<Serializable> list = (List<Serializable>) value; return (Serializable) deepCopy(list, threadSafe); } else if (value instanceof Object[]) { // array values are supposed to be scalars return ((Object[]) value).clone(); } // else scalar value -- check anyway (debug) else if (!isScalar(value)) { throw new UnsupportedOperationException("Cannot deep copy: " + value.getClass().getName()); } return (Serializable) value; }
/** * Makes a deep copy of a {@link State} map, optionally thread-safe. * * @param threadSafe if {@code true}, then thread-safe datastructures are used */ public static State deepCopy(State state, boolean threadSafe) { State copy = new State(state.size(), threadSafe); for (Entry<String, Serializable> en : state.entrySet()) { copy.put(en.getKey(), deepCopy(en.getValue(), threadSafe)); } return copy; }
state.put(key, StateHelper.deepCopy(value, true)); // thread-safe