public static Object copy ( Object thing ) throws CopyNotSupportedException { if ( ! isCopyable ( thing ) ) { throw new CopyNotSupportedException ( thing . getClass ( ) . getName ( ) + " cannot be copied. See Copyable" ) ; } if ( thing instanceof Copyable ) { return ( ( Copyable ) thing ) . copy ( ) ; } // Support for a few primitive types out of the box if ( thing instanceof byte [ ] ) { byte [ ] copy = new byte [ ( ( byte [ ] ) thing ) . length ] ; System . arraycopy ( thing , 0 , copy , 0 , ( ( byte [ ] ) thing ) . length ) ; return copy ; } // Assume that everything other type is immutable, not checking this again return thing ; }
Copy this object if needed .