Advertisement
Guest User

BaseAttribute

a guest
Jan 3rd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. /**
  2.  * This Source Code Form is subject to the terms of the Mozilla Public License,
  3.  * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  4.  * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
  5.  * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
  6.  *
  7.  * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
  8.  * graphic logo is a trademark of OpenMRS Inc.
  9.  */
  10. package org.openmrs.attribute;
  11.  
  12. import org.openmrs.BaseOpenmrsData;
  13. import org.openmrs.customdatatype.CustomDatatypeUtil;
  14. import org.openmrs.customdatatype.Customizable;
  15. import org.openmrs.customdatatype.InvalidCustomValueException;
  16. import org.openmrs.customdatatype.NotYetPersistedException;
  17. import org.openmrs.util.OpenmrsUtil;
  18.  
  19. /**
  20.  * Abstract base implementation of {@link Attribute}. Actual implementations should be able to extend this
  21.  * class, and have very little of their own code.  
  22.  * @param <AT>
  23.  * @param <OwningType>
  24.  * @since 1.9
  25.  */
  26. @SuppressWarnings("rawtypes")
  27. public abstract class BaseAttribute<AT extends AttributeType, OwningType extends Customizable<?>> extends BaseOpenmrsData implements Attribute<AT, OwningType>, Comparable<Attribute> {
  28.    
  29.     private OwningType owner;
  30.    
  31.     private AT attributeType;
  32.    
  33.     // value pulled from the database
  34.     private String valueReference;
  35.    
  36.     // temporarily holds a typed value, either when getValue() is called the first time (causing valueReference to be converted) or when setValue has been called, but this attribute has not yet been committed to persistent storage
  37.     private transient Object value;
  38.    
  39.     private transient boolean dirty = false;
  40.    
  41.     /**
  42.      * @see org.openmrs.attribute.Attribute#getOwner()
  43.      */
  44.     @Override
  45.     public OwningType getOwner() {
  46.         return owner;
  47.     }
  48.    
  49.     /**
  50.      * @see org.openmrs.attribute.Attribute#setOwner(org.openmrs.customdatatype.Customizable)
  51.      */
  52.     public void setOwner(OwningType owner) {
  53.         this.owner = owner;
  54.     }
  55.    
  56.     /**
  57.      * @param attributeType the attributeType to set
  58.      */
  59.     public void setAttributeType(AT attributeType) {
  60.         this.attributeType = attributeType;
  61.     }
  62.    
  63.     /**
  64.      * @see org.openmrs.attribute.Attribute#getAttributeType()
  65.      */
  66.     @Override
  67.     public AT getAttributeType() {
  68.         return attributeType;
  69.     }
  70.    
  71.     /**
  72.      * @see org.openmrs.customdatatype.SingleCustomValue#getDescriptor()
  73.      */
  74.     @Override
  75.     public AT getDescriptor() {
  76.         return getAttributeType();
  77.     }
  78.    
  79.     /**
  80.      * @see org.openmrs.customdatatype.SingleCustomValue#getValueReference()
  81.      */
  82.     @Override
  83.     public String getValueReference() {
  84.         if (valueReference == null) {
  85.             throw new NotYetPersistedException();
  86.         } else {
  87.             return valueReference;
  88.         }
  89.     }
  90.    
  91.     /**
  92.      * @see org.openmrs.customdatatype.SingleCustomValue#setValueReferenceInternal(java.lang.String)
  93.      */
  94.     @Override
  95.     public void setValueReferenceInternal(String valueReference) throws InvalidCustomValueException {
  96.         this.valueReference = valueReference;
  97.         this.dirty = false;
  98.     }
  99.    
  100.     /**
  101.      * @see org.openmrs.attribute.Attribute#getValue()
  102.      */
  103.     @Override
  104.     public Object getValue() throws InvalidCustomValueException {
  105.         if (value == null) {
  106.             value = CustomDatatypeUtil.getDatatype(getAttributeType()).fromReferenceString(getValueReference());
  107.         }
  108.         return value;
  109.     }
  110.    
  111.     /**
  112.      * @see org.openmrs.attribute.Attribute#setValue(java.lang.Object)
  113.      */
  114.     @Override
  115.     public <T> void setValue(T typedValue) throws InvalidCustomValueException {
  116.         dirty = true;
  117.         value = typedValue;
  118.     }
  119.    
  120.     /**
  121.      * @return the dirty
  122.      */
  123.     public boolean isDirty() {
  124.         return dirty;
  125.     }
  126.    
  127.     /**
  128.      * @see java.lang.Comparable#compareTo(java.lang.Object)
  129.      * Note: this comparator imposes orderings that are inconsistent with equals.
  130.      */
  131.     @SuppressWarnings("squid:S1210")
  132.     @Override
  133.     public int compareTo(Attribute other) {
  134.         if (other == null) {
  135.             return -1;
  136.         }
  137.         int retValue = isVoided().compareTo(other.isVoided());
  138.         if (retValue == 0) {
  139.             retValue = OpenmrsUtil.compareWithNullAsGreatest(getAttributeType().getId(), other.getAttributeType().getId());
  140.         }
  141.         if (retValue == 0) {
  142.             retValue = OpenmrsUtil.compareWithNullAsGreatest(getValueReference(), other.getValueReference());
  143.         }
  144.         if (retValue == 0) {
  145.             retValue = OpenmrsUtil.compareWithNullAsGreatest(getId(), other.getId());
  146.         }
  147.         return retValue;
  148.     }
  149.    
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement