com.telekom.m2m.cot.restsdk.util.ExtensibleObject Maven / Gradle / Ivy
package com.telekom.m2m.cot.restsdk.util;
import java.util.HashMap;
import java.util.Map;
/**
* Extensible object is a base class of all domain objects. It handles the (de-)serialization parts.
*
* Created by Patrick Steinert on 31.01.16.
*/
public class ExtensibleObject {
protected final HashMap anyObject = new HashMap<>();
public ExtensibleObject() {
}
/**
* Constructor that uses all the objects from an existing ExtensibleObject.
*
* @param extensibleObject existing base class object whose values in the map will be copied.
*/
protected ExtensibleObject(ExtensibleObject extensibleObject) {
if (extensibleObject != null)
anyObject.putAll(extensibleObject.anyObject);
}
/**
* Set a custom attribute of the object. Setting the same property again will override the old value.
*
* @param attributeId the unique id of the property as String
* @param value the value of the property.
*/
public void set(String attributeId, Object value) {
anyObject.put(attributeId, value);
}
/**
* Getting all attributes.
*
* @return a map with all attributes.
*/
public Map getAttributes() {
return (Map) anyObject.clone();
}
/**
* Get a custom attribute of the object.
*
* @param attributeId the unique identifier of the attribute.
* @return the value of the attribute.
*/
public Object get(String attributeId) {
return anyObject.get(attributeId);
}
/**
* Checks if a custom attribute is set.
*
* @param attributeId the unique identifier of the attribute.
* @return true if attribute is set, even if null.
*/
public boolean has(String attributeId) {
return anyObject.containsKey(attributeId);
}
/**
* Set a custom attribute with its name derived from class package and name.
*
* E.g. a class com.telekom.SpecialObject will get the identifier com_telekom_SpecialObject.
*
* @param attribute the value of the custom attribute.
*/
public void set(Object attribute) {
anyObject.put(attribute.getClass().getCanonicalName().replace('.', '_'), attribute);
}
/**
* Adds all attributes from argument to this object. Attributes with the existing attribute identifiers will be
* overridden.
*
* @param attributes the attributes to add, can't be null.
*/
public void setAttributes(Map attributes) {
if (attributes != null) {
anyObject.putAll(attributes);
}
}
@Override
public String toString() {
return "ExtensibleObject{" + "anyObject=" + anyObject +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ExtensibleObject)) return false;
ExtensibleObject that = (ExtensibleObject) o;
return anyObject != null ? anyObject.equals(that.anyObject) : that.anyObject == null;
}
@Override
public int hashCode() {
return anyObject != null ? anyObject.hashCode() : 0;
}
}