001package org.hl7.fhir.dstu2.model; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import org.apache.commons.lang3.StringUtils; 035import org.apache.commons.lang3.builder.EqualsBuilder; 036import org.apache.commons.lang3.builder.HashCodeBuilder; 037import org.hl7.fhir.instance.model.api.IBaseHasExtensions; 038import org.hl7.fhir.instance.model.api.IPrimitiveType; 039 040public abstract class PrimitiveType<T> extends Type implements IPrimitiveType<T>, IBaseHasExtensions { 041 042 private static final long serialVersionUID = 3L; 043 044 private T myCoercedValue; 045 private String myStringValue; 046 047 public T getValue() { 048 return myCoercedValue; 049 } 050 051 public String asStringValue() { 052 return myStringValue; 053 } 054 055 @Override 056 public int hashCode() { 057 return new HashCodeBuilder().append(getValue()).toHashCode(); 058 } 059 060 public PrimitiveType<T> setValue(T theValue) { 061 myCoercedValue = theValue; 062 updateStringValue(); 063 return this; 064 } 065 066 protected void updateStringValue() { 067 if (myCoercedValue == null) { 068 myStringValue = null; 069 } else { 070 // NB this might be null 071 myStringValue = encode(myCoercedValue); 072 } 073 } 074 075 @Override 076 public boolean isEmpty() { 077 return super.isEmpty() && StringUtils.isBlank(getValueAsString()); 078 } 079 080 public void fromStringValue(String theValue) { 081 if (theValue == null) { 082 myCoercedValue = null; 083 } else { 084 // NB this might be null 085 myCoercedValue = parse(theValue); 086 } 087 myStringValue = theValue; 088 } 089 090 /** 091 * Subclasses must override to convert an encoded representation of this datatype into a "coerced" one 092 * 093 * @param theValue 094 * Will not be null 095 * @return May return null if the value does not correspond to anything 096 */ 097 protected abstract T parse(String theValue); 098 099 /** 100 * Subclasses must override to convert a "coerced" value into an encoded one. 101 * 102 * @param theValue 103 * Will not be null 104 * @return May return null if the value does not correspond to anything 105 */ 106 protected abstract String encode(T theValue); 107 108 public boolean isPrimitive() { 109 return true; 110 } 111 112 public String primitiveValue() { 113 return asStringValue(); 114 } 115 116 @Override 117 public String toString() { 118 return getClass().getSimpleName() + "[" + asStringValue() + "]"; 119 } 120 121 public boolean hasValue() { 122 return !StringUtils.isBlank(getValueAsString()); 123 } 124 125 public String getValueAsString() { 126 return asStringValue(); 127 } 128 129 public void setValueAsString(String theValue) { 130 fromStringValue(theValue); 131 } 132 133 protected Type typedCopy() { 134 return copy(); 135 } 136 137 public abstract Type copy(); 138 139 @Override 140 public boolean equalsDeep(Base obj) { 141 if (!super.equalsDeep(obj)) 142 return false; 143 if (obj == null) { 144 return false; 145 } 146 if (!(obj.getClass() == getClass())) { 147 return false; 148 } 149 150 PrimitiveType<?> o = (PrimitiveType<?>) obj; 151 152 EqualsBuilder b = new EqualsBuilder(); 153 b.append(getValue(), o.getValue()); 154 return b.isEquals(); 155 } 156 157 @Override 158 public boolean equalsShallow(Base obj) { 159 if (obj == null) { 160 return false; 161 } 162 if (!(obj.getClass() == getClass())) { 163 return false; 164 } 165 166 PrimitiveType<?> o = (PrimitiveType<?>) obj; 167 168 EqualsBuilder b = new EqualsBuilder(); 169 b.append(getValue(), o.getValue()); 170 return b.isEquals(); 171 } 172 173 174}