Package craterdog.smart
Class SmartObject<S extends SmartObject<S>>
java.lang.Object
craterdog.smart.SmartObject<S>
- Type Parameters:
S- The concrete type of the smart object.
- All Implemented Interfaces:
Composite<S>,Comparable<S>
This abstract class provides implementations for the standard methods defined in the
Object class. It also adds a generics based copy() method that is
superior to the clone() method in many ways.- Author:
- Derk Norton, Jeff Webb, Mukesh Jyothi, Yan Ma
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddSerializableClass(com.fasterxml.jackson.databind.Module module) This protected method allows a subclass to add to the mappers a Jackson module that can be used to serialize and deserialize instances of the subclass.protected voidaddSerializableClass(Class<?> serializable) This protected method allows a subclass to add to the mappers a class type that can be serialized using its toString().protected voidaddSerializableClass(Class<?> serializable, Class<?> mixin) This protected method allows a subclass to add to the mappers a class type that can be serialized using mixin class.intcopy()This method should work for all objects.static com.fasterxml.jackson.databind.ObjectMappercreateMapper(com.fasterxml.jackson.databind.Module... modules) This function generates a new object mapper with the specified modules.booleanThis method determines whether or not two objects are equal.static <T> TfromString(com.fasterxml.jackson.core.type.TypeReference<T> classType, String json) This function takes string containing Javascript Object Notation (JSON) and uses it to construct the corresponding smart object.static <T> TfromString(Class<T> classType, String json) This function takes string containing Javascript Object Notation (JSON) and uses it to construct the corresponding smart object.inthashCode()This method returns a hash code for the object based on its string form.protected StringThis method behaves similarly to thetoString()method except that it does not perform any censorship of sensitive attributes.toString()This method returns a string containing a structured, human readable version of the object.static StringThis function generates a Javascript Object Notation (JSON) string from an object.static StringThis function generates an indented Javascript Object Notation (JSON) string from an object.
-
Constructor Details
-
SmartObject
public SmartObject()
-
-
Method Details
-
toString
This method returns a string containing a structured, human readable version of the object. The string is formatted in Javascript Object Notation (JSON). -
toString
- Specified by:
toStringin interfaceComposite<S extends SmartObject<S>>
-
toExposedString
This method behaves similarly to thetoString()method except that it does not perform any censorship of sensitive attributes. It should only be used when the resulting output will not be stored or seen by anyone.- Returns:
- The formatted JSON string.
-
equals
This method determines whether or not two objects are equal. Two objects are equal if they have the same class type and all their attributes and sub-components are equal. -
compareTo
- Specified by:
compareToin interfaceComparable<S extends SmartObject<S>>
-
copy
This method should work for all objects. However, it is very inefficient and should only be used sparingly and for unit testing.- Specified by:
copyin interfaceComposite<S extends SmartObject<S>>- Type Parameters:
T- The concrete type of the smart object.- Returns:
- An exact copy of the smart object.
-
hashCode
public int hashCode()This method returns a hash code for the object based on its string form. This is not a very efficient method. -
fromString
This function takes string containing Javascript Object Notation (JSON) and uses it to construct the corresponding smart object. This function can be used for any non-parameterized type for example:Customer customer = SmartObject.fromString(Customer.class, jsonString);- Type Parameters:
T- The type of object being constructed.- Parameters:
classType- The concrete class type being constructed.json- The JSON string.- Returns:
- The corresponding object.
- Throws:
IOException- The JSON string could not be parsed correctly.
-
fromString
public static <T> T fromString(com.fasterxml.jackson.core.type.TypeReference<T> classType, String json) throws IOException This function takes string containing Javascript Object Notation (JSON) and uses it to construct the corresponding smart object. This function can be used for any parameterized type for example:List<String> list = SmartObject.fromString(new TypeReference<List<String>>() { }, jsonString);The anonymous type declaration is required due to Java's erasure of parameterized types at runtime.- Type Parameters:
T- The type of object being constructed.- Parameters:
classType- The parameterized class type being constructed.json- The JSON string.- Returns:
- The corresponding object.
- Throws:
IOException- The JSON string could not be parsed correctly.
-
toString
This function generates a Javascript Object Notation (JSON) string from an object.- Parameters:
object- The object to be turned into a JSON string.- Returns:
- The corresponding JSON string.
-
toString
This function generates an indented Javascript Object Notation (JSON) string from an object.- Parameters:
object- The object to be turned into a JSON string.indentation- The amount of space that should be prepended to each line.- Returns:
- The corresponding JSON string.
-
createMapper
public static com.fasterxml.jackson.databind.ObjectMapper createMapper(com.fasterxml.jackson.databind.Module... modules) This function generates a new object mapper with the specified modules. For example, to create object mapper that masks sensitive attributes do the following:ObjectMapper mapper = SmartObject.createMapper(new CensorshipModule());
- Parameters:
modules- The list of modules that should be added to the object mapper.- Returns:
- A new object mapper containing the specified modules.
-
addSerializableClass
This protected method allows a subclass to add to the mappers a class type that can be serialized using its toString().- Parameters:
serializable- The type of class that can be serialized using its toString() method.
-
addSerializableClass
This protected method allows a subclass to add to the mappers a class type that can be serialized using mixin class.- Parameters:
serializable- The type of class that can be serialized using its toString() method.mixin- The type of class that can be used to serialized the serializable class.
-
addSerializableClass
protected void addSerializableClass(com.fasterxml.jackson.databind.Module module) This protected method allows a subclass to add to the mappers a Jackson module that can be used to serialize and deserialize instances of the subclass.- Parameters:
module- The type of class that can be serialized using its toString() method.
-