
org.pepsoft.util.ObjectUtils Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.pepsoft.util;
import org.pepsoft.util.undo.*;
import org.pepsoft.util.undo.Cloneable;
import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferDouble;
import java.awt.image.DataBufferFloat;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.util.*;
import java.util.List;
/**
*
* @author SchmitzP
*/
public final class ObjectUtils {
private ObjectUtils() {
// Prevent instantiation
}
/**
* Make a deep copy of an object. Only a restricted set of types is
* supported.
*
* @param The type of the object.
* @param object The object to copy.
* @return A deep copy of the object.
* @throws OutOfMemoryError If there is not enough memory to copy the
* object.
*/
@SuppressWarnings("unchecked")
public static T copyObject(T object) {
// Point isn't actually immutable, but it is used as such by WorldPainter, at least in all data structures
// managed by an undo manager
if ((object == null) || (object instanceof Number) || (object instanceof Character)
|| (object instanceof Boolean) || (object instanceof String) || (object instanceof Enum)
|| (object instanceof Point) || (object instanceof Immutable)) {
// Object is null or immutable; making a copy not necessary
return object;
} else {
if (object instanceof BitSet) {
return (T) ((BitSet) object).clone();
} else if (object instanceof EnumSet) {
return (T) ((EnumSet) object).clone();
} else if (object instanceof byte[]) {
return (T) ((byte[]) object).clone();
} else if (object instanceof short[]) {
return (T) ((short[]) object).clone();
} else if (object instanceof int[]) {
return (T) ((int[]) object).clone();
} else if (object instanceof long[]) {
return (T) ((long[]) object).clone();
} else if (object instanceof float[]) {
return (T) ((float[]) object).clone();
} else if (object instanceof double[]) {
return (T) ((double[]) object).clone();
} else if (object instanceof String[]) {
return (T) ((String[]) object).clone();
} else if (object instanceof Map) {
final Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy