Class BaseBo

  • All Implemented Interfaces:
    com.github.ddth.commons.serialization.ISerializationSupport, java.lang.Cloneable
    Direct Known Subclasses:
    BaseDataJsonFieldBo, BaseJsonBo

    public class BaseBo
    extends java.lang.Object
    implements java.lang.Cloneable, com.github.ddth.commons.serialization.ISerializationSupport
    Base class for application Business Objects (BO).
    Since:
    0.1.0
    Author:
    Thanh Ba Nguyen
    • Constructor Summary

      Constructors 
      Constructor Description
      BaseBo()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean attributeExists​(java.lang.String attrName)
      Check an attribute exists.
      protected java.util.Map<java.lang.String,​java.lang.Object> attributeMap()
      Return the underlying attribute map.
      long calcChecksum()
      Calculate the checksum of BO's attributes (ignore "dirty" flag).
      protected long checksum()
      Sub-class may override this method to implement its own business logic.
      BaseBo clone()
      protected static <T> java.util.Map<java.lang.String,​T> cloneData​(java.util.Map<java.lang.String,​T> data)
      Deep-clone data.
      boolean equals​(java.lang.Object obj)
      BaseBo fromByteArray​(byte[] data)
      De-serialize the BO from a byte array (previously generated by toByteArray().
      com.github.ddth.commons.serialization.ISerializationSupport fromBytes​(byte[] data)
      BaseBo fromJson​(java.lang.String jsonString)
      De-serialize the BO from a JSON string (previously generated by toJson().
      BaseBo fromMap​(java.util.Map<java.lang.String,​java.lang.Object> data)
      De-serialize the BO from a Java map (previously generated by toMap().
      java.lang.Object getAttribute​(java.lang.String attrName)
      Get a BO's attribute.
      <T> T getAttribute​(java.lang.String attrName, java.lang.Class<T> clazz)
      Get a BO's attribute.
      java.util.Date getAttributeAsDate​(java.lang.String attrName, java.lang.String dateTimeFormat)
      Get a BO's attribute as a date.
      <T> java.util.Optional<T> getAttributeOptional​(java.lang.String attrName, java.lang.Class<T> clazz)
      Get a BO's attribute.
      java.util.Map<java.lang.String,​java.lang.Object> getAttributes()
      Get all BO's attributes as a map.
      com.fasterxml.jackson.databind.JsonNode getAttributesAsJson()
      Get all BO's attributes as a JsonNode.
      java.lang.String getAttributesAsJsonString()
      Get all BO's attributes as a JSON-string.
      int hashCode()
      protected <T> java.util.Map<java.lang.String,​T> initAttributes​(java.util.Map<java.lang.String,​T> initData)  
      boolean isDirty()
      Has the BO been changed?
      protected java.util.concurrent.locks.Lock lockForRead()
      Lock the BO for read.
      protected java.util.concurrent.locks.Lock lockForWrite()
      Lock the BO for write.
      BaseBo markClean()
      Mark that the BO is no longer dirty.
      protected BaseBo markDirty()
      Mark that the BO is dirty.
      protected java.util.concurrent.locks.Lock readLock()
      Obtain the BO's "read" lock.
      protected BaseBo removeAttribute​(java.lang.String attrName)
      Remove a BO's attribute.
      protected BaseBo removeAttribute​(java.lang.String attrName, boolean triggerChange)
      Remove a BO's attribute.
      BaseBo setAttribute​(java.lang.String attrName, java.lang.Object value)
      Set a BO's attribute.
      BaseBo setAttribute​(java.lang.String attrName, java.lang.Object value, boolean triggerChange)
      Set a BO's attribute.
      BaseBo setAttributes​(com.fasterxml.jackson.databind.JsonNode attrs)
      Set all BO's attributes.
      BaseBo setAttributes​(java.lang.String jsonString)
      Set all BO's attributes.
      BaseBo setAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attrs)
      Set all BO's attributes.
      byte[] toByteArray()
      Serialize the BO to byte array.
      byte[] toBytes()
      java.lang.String toJson()
      Serialize the BO to JSON string.
      java.util.Map<java.lang.String,​java.lang.Object> toMap()
      Serialize the BO to a Java map.
      java.lang.String toString()
      protected void triggerChange​(java.lang.String attrName)
      Called when one of BO's attributes is changed (added, updated or removed).
      protected void triggerPopulate()
      Called when the BO's entire attribute set are (re)populated.
      protected java.util.concurrent.locks.Lock tryLockForRead()
      Try to lock the BO for read.
      protected java.util.concurrent.locks.Lock tryLockForRead​(long time, java.util.concurrent.TimeUnit unit)
      Try to lock the BO for read.
      protected java.util.concurrent.locks.Lock tryLockForWrite()
      Try to lock the BO for write.
      protected java.util.concurrent.locks.Lock tryLockForWrite​(long time, java.util.concurrent.TimeUnit unit)
      Try to lock the BO for write.
      protected void unlockForRead()
      Release the "read"-lock.
      protected void unlockForWrite()
      Release the "write"-lock.
      protected java.util.concurrent.locks.Lock writeLock()
      Obtain the BO's "write" lock.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • BaseBo

        public BaseBo()
    • Method Detail

      • cloneData

        protected static <T> java.util.Map<java.lang.String,​T> cloneData​(java.util.Map<java.lang.String,​T> data)
        Deep-clone data.
        Parameters:
        data -
        Returns:
        Since:
        0.10.0
      • initAttributes

        protected <T> java.util.Map<java.lang.String,​T> initAttributes​(java.util.Map<java.lang.String,​T> initData)
      • clone

        public BaseBo clone()
        Overrides:
        clone in class java.lang.Object
        Since:
        0.5.0.5
      • isDirty

        public boolean isDirty()
        Has the BO been changed?
        Returns:
      • markDirty

        protected BaseBo markDirty()
        Mark that the BO is dirty.
        Returns:
      • markClean

        public BaseBo markClean()
        Mark that the BO is no longer dirty.
        Returns:
      • attributeExists

        protected boolean attributeExists​(java.lang.String attrName)
        Check an attribute exists.
        Parameters:
        attrName -
        Returns:
        Since:
        0.7.1
      • attributeMap

        protected java.util.Map<java.lang.String,​java.lang.Object> attributeMap()
        Return the underlying attribute map.
        Returns:
        Since:
        0.7.1
      • getAttributes

        public java.util.Map<java.lang.String,​java.lang.Object> getAttributes()
        Get all BO's attributes as a map.
        Returns:
        Since:
        0.8.2
      • setAttributes

        public BaseBo setAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attrs)
        Set all BO's attributes.
        Parameters:
        attrs -
        Returns:
      • getAttributesAsJson

        public com.fasterxml.jackson.databind.JsonNode getAttributesAsJson()
        Get all BO's attributes as a JsonNode.
        Returns:
        BO's attributes as a JsonNode, NullNode is returned if BO's attribute-map is null
        Since:
        0.10.0
      • setAttributes

        public BaseBo setAttributes​(com.fasterxml.jackson.databind.JsonNode attrs)
        Set all BO's attributes.
        Parameters:
        attrs -
        Returns:
        Since:
        0.10.0
      • getAttributesAsJsonString

        public java.lang.String getAttributesAsJsonString()
        Get all BO's attributes as a JSON-string.
        Returns:
        Since:
        0.10.0
      • setAttributes

        public BaseBo setAttributes​(java.lang.String jsonString)
        Set all BO's attributes.
        Parameters:
        jsonString -
        Returns:
        Since:
        0.10.0
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String attrName)
        Get a BO's attribute.
        Parameters:
        attrName -
        Returns:
      • getAttribute

        public <T> T getAttribute​(java.lang.String attrName,
                                  java.lang.Class<T> clazz)
        Get a BO's attribute.
        Parameters:
        attrName -
        clazz -
        Returns:
      • getAttributeOptional

        public <T> java.util.Optional<T> getAttributeOptional​(java.lang.String attrName,
                                                              java.lang.Class<T> clazz)
        Get a BO's attribute.
        Parameters:
        attrName -
        clazz -
        Returns:
        Since:
        0.8.0
      • getAttributeAsDate

        public java.util.Date getAttributeAsDate​(java.lang.String attrName,
                                                 java.lang.String dateTimeFormat)
        Get a BO's attribute as a date. If the attribute value is a string, parse it as a Date using the specified date-time format.
        Parameters:
        attrName -
        dateTimeFormat -
        Returns:
        Since:
        0.8.0
      • setAttribute

        public BaseBo setAttribute​(java.lang.String attrName,
                                   java.lang.Object value)
        Set a BO's attribute.
        Parameters:
        attrName -
        value -
        Returns:
      • setAttribute

        public BaseBo setAttribute​(java.lang.String attrName,
                                   java.lang.Object value,
                                   boolean triggerChange)
        Set a BO's attribute.
        Parameters:
        attrName -
        value -
        triggerChange - if set to true triggerChange(String) will be called
        Returns:
        Since:
        0.7.1
      • removeAttribute

        protected BaseBo removeAttribute​(java.lang.String attrName)
        Remove a BO's attribute.
        Parameters:
        attrName -
        Returns:
        Since:
        0.7.1
      • removeAttribute

        protected BaseBo removeAttribute​(java.lang.String attrName,
                                         boolean triggerChange)
        Remove a BO's attribute.
        Parameters:
        attrName -
        triggerChange - if set to true triggerChange(String) will be called
        Returns:
        Since:
        0.7.1
      • triggerPopulate

        protected void triggerPopulate()
        Called when the BO's entire attribute set are (re)populated.
        Since:
        0.5.0.2
      • triggerChange

        protected void triggerChange​(java.lang.String attrName)
        Called when one of BO's attributes is changed (added, updated or removed).
        Parameters:
        attrName -
        Since:
        0.7.1
      • calcChecksum

        public long calcChecksum()
        Calculate the checksum of BO's attributes (ignore "dirty" flag).
        Returns:
        Since:
        0.10.0
      • checksum

        protected long checksum()
        Sub-class may override this method to implement its own business logic.

        This method is called by calcChecksum(), no need to implement lock/synchronization

        Returns:
        Since:
        0.10.0
      • readLock

        protected java.util.concurrent.locks.Lock readLock()
        Obtain the BO's "read" lock.
        Returns:
        Since:
        0.10.0
      • writeLock

        protected java.util.concurrent.locks.Lock writeLock()
        Obtain the BO's "write" lock.
        Returns:
        Since:
        0.10.0
      • lockForRead

        protected java.util.concurrent.locks.Lock lockForRead()
        Lock the BO for read.
        Returns:
        the "read"-lock in "lock" state
        Since:
        0.10.0
      • tryLockForRead

        protected java.util.concurrent.locks.Lock tryLockForRead()
        Try to lock the BO for read.
        Returns:
        the "read"-lock in "lock" state if successful, null otherwise
        Since:
        0.10.0
      • tryLockForRead

        protected java.util.concurrent.locks.Lock tryLockForRead​(long time,
                                                                 java.util.concurrent.TimeUnit unit)
                                                          throws java.lang.InterruptedException
        Try to lock the BO for read.
        Parameters:
        time -
        unit -
        Returns:
        the "read"-lock in "lock" state if successful, null otherwise
        Throws:
        java.lang.InterruptedException
        Since:
        0.10.0
      • unlockForRead

        protected void unlockForRead()
        Release the "read"-lock.
        Since:
        0.10.0
      • lockForWrite

        protected java.util.concurrent.locks.Lock lockForWrite()
        Lock the BO for write.
        Returns:
        the "write"-lock in "lock" state
        Since:
        0.10.0
      • tryLockForWrite

        protected java.util.concurrent.locks.Lock tryLockForWrite()
        Try to lock the BO for write.
        Returns:
        the "write"-lock in "lock" state if successful, null otherwise
        Since:
        0.10.0
      • tryLockForWrite

        protected java.util.concurrent.locks.Lock tryLockForWrite​(long time,
                                                                  java.util.concurrent.TimeUnit unit)
                                                           throws java.lang.InterruptedException
        Try to lock the BO for write.
        Parameters:
        time -
        unit -
        Returns:
        the "write"-lock in "lock" state if successful, null otherwise
        Throws:
        java.lang.InterruptedException
        Since:
        0.10.0
      • unlockForWrite

        protected void unlockForWrite()
        Release the "write"-lock.
        Since:
        0.10.0
      • fromMap

        public BaseBo fromMap​(java.util.Map<java.lang.String,​java.lang.Object> data)
        De-serialize the BO from a Java map (previously generated by toMap().

        This method is meant to be used in conjunction with method #toMap(). Use setAttributes(Map) to reset just the underlying attribute map.

        Parameters:
        data - BO data as a Java map (generated via toMap()
        Returns:
      • toMap

        public java.util.Map<java.lang.String,​java.lang.Object> toMap()
        Serialize the BO to a Java map.
        Returns:
        BO's data as a Java map, can be used to de-serialize the BO via fromMap(Map)
      • fromJson

        public BaseBo fromJson​(java.lang.String jsonString)
        De-serialize the BO from a JSON string (previously generated by toJson().
        Parameters:
        jsonString - BO data as a JSON string (generated via toJson()
        Returns:
      • toJson

        public java.lang.String toJson()
        Serialize the BO to JSON string.
        Returns:
        BO's data as a JSON string, can be used to de-serialize the BO via fromJson(String)
      • fromByteArray

        public BaseBo fromByteArray​(byte[] data)
        De-serialize the BO from a byte array (previously generated by toByteArray().
        Parameters:
        data - BO data as a byte array (generated via toByteArray()
        Returns:
        Since:
        0.5.0
      • toByteArray

        public byte[] toByteArray()
        Serialize the BO to byte array.
        Returns:
        BO's data as a byte array, can be used to de-serialize the BO via fromByteArray(byte[])
        Since:
        0.5.0
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toBytes

        public byte[] toBytes()
                       throws com.github.ddth.commons.serialization.SerializationException
        Specified by:
        toBytes in interface com.github.ddth.commons.serialization.ISerializationSupport
        Throws:
        com.github.ddth.commons.serialization.SerializationException
        Since:
        0.6.0
      • fromBytes

        public com.github.ddth.commons.serialization.ISerializationSupport fromBytes​(byte[] data)
                                                                              throws com.github.ddth.commons.serialization.DeserializationException
        Specified by:
        fromBytes in interface com.github.ddth.commons.serialization.ISerializationSupport
        Throws:
        com.github.ddth.commons.serialization.DeserializationException
        Since:
        0.6.0