/** * Protect a specific value if it is considered mutable * * @param <S> the type of the value, which must be {@link Serializable} * @param value the value to protect if it is mutable (may be <tt>null</tt>) * @param immutableClasses a set of classes that can be considered immutable * over and above the {@link #DEFAULT_IMMUTABLE_CLASSES default set} * @return a cloned instance (via serialization) or the instance itself, if immutable */ @SuppressWarnings("unchecked") public static <S extends Serializable> S protectValue(S value, Set<Class<?>> immutableClasses) { if (!mustProtectValue(value, immutableClasses)) { return value; } // We have to clone it // No worries about the return type; it has to be the same as we put into the serializer return (S) SerializationUtils.deserialize(SerializationUtils.serialize(value)); }
/** * Protect a specific value if it is considered mutable * * @param <S> the type of the value, which must be {@link Serializable} * @param value the value to protect if it is mutable (may be <tt>null</tt>) * @param immutableClasses a set of classes that can be considered immutable * over and above the {@link #DEFAULT_IMMUTABLE_CLASSES default set} * @return a cloned instance (via serialization) or the instance itself, if immutable */ @SuppressWarnings("unchecked") public static <S extends Serializable> S protectValue(S value, Set<Class<?>> immutableClasses) { if (!mustProtectValue(value, immutableClasses)) { return value; } // We have to clone it // No worries about the return type; it has to be the same as we put into the serializer return (S) SerializationUtils.deserialize(SerializationUtils.serialize(value)); }