Coverage Report - org.kuali.rice.core.api.mo.AbstractDataTransferObject
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDataTransferObject
100%
22/22
62%
5/8
1.667
AbstractDataTransferObject$Constants
50%
1/2
N/A
1.667
 
 1  
 package org.kuali.rice.core.api.mo;
 2  
 
 3  
 import org.apache.commons.lang.builder.EqualsBuilder;
 4  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 5  
 import org.apache.commons.lang.builder.ToStringBuilder;
 6  
 import org.kuali.rice.core.api.CoreConstants;
 7  
 import org.kuali.rice.core.api.util.collect.CollectionUtils;
 8  
 
 9  
 import javax.xml.bind.Unmarshaller;
 10  
 import javax.xml.bind.annotation.XmlTransient;
 11  
 
 12  
 /**
 13  
  * All model object's that are Jaxb annotated should extend this class.
 14  
  *
 15  
  * This class does several important things:
 16  
  * <ol>
 17  
  *     <li>Defines jaxb callback method to ensure that Collection and Map types are unmarshalled into immutable empty forms rather than null values</li>
 18  
  *     <li>Defines equals/hashcode/toString</li>
 19  
  *
 20  
  *     Note: the equals/hashCode implementation excludes {@value CoreConstants.CommonElements.FUTURE_ELEMENTS} field.
 21  
  *     This element should be present on all jaxb annotated classes.
 22  
  * </ol>
 23  
  *
 24  
  * <b>Important: all classes extending this class must be immutable</b>
 25  
  */
 26  
 @XmlTransient // marked as @XmlTransient so that an AbstractDataTransferObjectType is not included in all WSDL schemas
 27  
 public abstract class AbstractDataTransferObject implements ModelObjectComplete {
 28  
 
 29  
     private transient volatile Integer _hashCode;
 30  
     private transient volatile String _toString;
 31  
 
 32  
     protected AbstractDataTransferObject() {
 33  485
         super();
 34  485
     }
 35  
 
 36  
     @Override
 37  
     public int hashCode() {
 38  
         //using DCL idiom to cache hashCodes.  Hashcodes on immutable objects never change.  They can be safely cached.
 39  
         //see effective java 2nd ed. pg. 71
 40  333
         Integer h = _hashCode;
 41  333
         if (h == null) {
 42  134
             synchronized (this) {
 43  134
                 h = _hashCode;
 44  134
                 if (h == null) {
 45  134
                     _hashCode = h = Integer.valueOf(HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE));
 46  
                 }
 47  134
             }
 48  
         }
 49  
 
 50  333
         return h.intValue();
 51  
     }
 52  
 
 53  
     @Override
 54  
     public boolean equals(Object obj) {
 55  150
         return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE);
 56  
     }
 57  
 
 58  
     @Override
 59  
     public String toString() {
 60  
         //using DCL idiom to cache toString.  toStrings on immutable objects never change.  They can be safely cached.
 61  
         //see effective java 2nd ed. pg. 71
 62  1
         String t = _toString;
 63  1
         if (t == null) {
 64  1
             synchronized (this) {
 65  1
                 t = _toString;
 66  1
                 if (t == null) {
 67  1
                     _toString = t = ToStringBuilder.reflectionToString(this);
 68  
                 }
 69  1
             }
 70  
         }
 71  
 
 72  1
         return t;
 73  
     }
 74  
 
 75  
     @SuppressWarnings("unused")
 76  
     protected void beforeUnmarshal(Unmarshaller u, Object parent) throws Exception {
 77  173
     }
 78  
 
 79  
     @SuppressWarnings("unused")
 80  
     protected void afterUnmarshal(Unmarshaller u, Object parent) throws Exception {
 81  173
         CollectionUtils.makeUnmodifiableAndNullSafe(this);
 82  173
     }
 83  
 
 84  
     /**
 85  
      * Defines some internal constants used on this class.
 86  
      */
 87  0
     protected static class Constants {
 88  1
         final static String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS, "_hashCode", "_toString"};
 89  
     }
 90  
 }