More work on #50, almosty have all primitives done

This commit is contained in:
James Agnew 2014-11-14 14:58:54 -05:00
parent d09e735a3d
commit cfcad3aabf
10 changed files with 255 additions and 477 deletions

View File

@ -29,8 +29,6 @@ import ca.uhn.fhir.model.api.annotation.SimpleSetter;
@DatatypeDef(name = "base64Binary")
public class Base64BinaryDt extends BasePrimitive<byte[]> {
private byte[] myValue;
/**
* Constructor
*/
@ -42,36 +40,18 @@ public class Base64BinaryDt extends BasePrimitive<byte[]> {
* Constructor
*/
@SimpleSetter
public Base64BinaryDt(@SimpleSetter.Parameter(name="theBytes") byte[] theBytes) {
public Base64BinaryDt(@SimpleSetter.Parameter(name = "theBytes") byte[] theBytes) {
setValue(theBytes);
}
@Override
public void setValueAsString(String theValue) {
if (theValue == null) {
myValue = null;
} else {
myValue = Base64.decodeBase64(theValue);
}
protected byte[] parse(String theValue) {
return Base64.decodeBase64(theValue);
}
@Override
public String getValueAsString() {
if (myValue == null) {
return null;
} else {
return Base64.encodeBase64String(myValue);
}
}
@Override
public void setValue(byte[] theValue) {
myValue = theValue;
}
@Override
public byte[] getValue() {
return myValue;
protected String encode(byte[] theValue) {
return Base64.encodeBase64String(theValue);
}
}

View File

@ -59,11 +59,55 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
private TemporalPrecisionEnum myPrecision = TemporalPrecisionEnum.SECOND;
private TimeZone myTimeZone;
private boolean myTimeZoneZulu = false;
private Date myValue;
private void clearTimeZone() {
myTimeZone = null;
myTimeZoneZulu = false;
}
@Override
protected String encode(Date theValue) {
if (theValue == null) {
return null;
} else {
switch (myPrecision) {
case DAY:
return ourYearMonthDayFormat.format(theValue);
case MONTH:
return ourYearMonthFormat.format(theValue);
case YEAR:
return ourYearFormat.format(theValue);
case SECOND:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.setTime(theValue);
return ourYearMonthDayTimeFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
GregorianCalendar cal = new GregorianCalendar(myTimeZone);
cal.setTime(theValue);
return ourYearMonthDayTimeZoneFormat.format(cal);
} else {
return ourYearMonthDayTimeFormat.format(theValue);
}
case MILLI:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.setTime(theValue);
return ourYearMonthDayTimeMilliFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
GregorianCalendar cal = new GregorianCalendar(myTimeZone);
cal.setTime(theValue);
return ourYearMonthDayTimeMilliZoneFormat.format(cal);
} else {
return ourYearMonthDayTimeMilliFormat.format(theValue);
}
}
throw new IllegalStateException("Invalid precision (this is a HAPI bug, shouldn't happen): " + myPrecision);
}
}
/**
* Gets the precision for this datatype using field values from {@link Calendar}, such as {@link Calendar#MONTH}.
* Default is {@link Calendar#DAY_OF_MONTH}
* Gets the precision for this datatype using field values from {@link Calendar}, such as {@link Calendar#MONTH}. Default is {@link Calendar#DAY_OF_MONTH}
*
* @see #setPrecision(int)
*/
@ -75,51 +119,10 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
return myTimeZone;
}
@Override
public Date getValue() {
return myValue;
}
@Override
public String getValueAsString() {
if (myValue == null) {
return null;
} else {
switch (myPrecision) {
case DAY:
return ourYearMonthDayFormat.format(myValue);
case MONTH:
return ourYearMonthFormat.format(myValue);
case YEAR:
return ourYearFormat.format(myValue);
case SECOND:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.setTime(myValue);
return ourYearMonthDayTimeFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
GregorianCalendar cal = new GregorianCalendar(myTimeZone);
cal.setTime(myValue);
return ourYearMonthDayTimeZoneFormat.format(cal);
} else {
return ourYearMonthDayTimeFormat.format(myValue);
}
case MILLI:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.setTime(myValue);
return ourYearMonthDayTimeMilliFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
GregorianCalendar cal = new GregorianCalendar(myTimeZone);
cal.setTime(myValue);
return ourYearMonthDayTimeMilliZoneFormat.format(cal);
} else {
return ourYearMonthDayTimeMilliFormat.format(myValue);
}
}
throw new IllegalStateException("Invalid precision (this is a HAPI bug, shouldn't happen): " + myPrecision);
}
}
/**
* To be implemented by subclasses to indicate whether the given precision is allowed by this type
*/
abstract boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision);
public boolean isTimeZoneZulu() {
return myTimeZoneZulu;
@ -132,8 +135,91 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
* if {@link #getValue()} returns <code>null</code>
*/
public boolean isToday() {
Validate.notNull(myValue, getClass().getSimpleName() + " contains null value");
return DateUtils.isSameDay(new Date(), myValue);
Validate.notNull(getValue(), getClass().getSimpleName() + " contains null value");
return DateUtils.isSameDay(new Date(), getValue());
}
@Override
protected Date parse(String theValue) throws DataFormatException {
try {
if (theValue.length() == 4 && ourYearPattern.matcher(theValue).matches()) {
if (isPrecisionAllowed(YEAR)) {
setPrecision(YEAR);
clearTimeZone();
return ((ourYearFormat).parse(theValue));
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support YEAR precision): " + theValue);
}
} else if (theValue.length() == 6 && ourYearMonthPattern.matcher(theValue).matches()) {
// Eg. 198401 (allow this just to be lenient)
if (isPrecisionAllowed(MONTH)) {
setPrecision(MONTH);
clearTimeZone();
return ((ourYearMonthNoDashesFormat).parse(theValue));
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() == 7 && ourYearDashMonthPattern.matcher(theValue).matches()) {
// E.g. 1984-01 (this is valid according to the spec)
if (isPrecisionAllowed(MONTH)) {
setPrecision(MONTH);
clearTimeZone();
return ((ourYearMonthFormat).parse(theValue));
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support MONTH precision): " + theValue);
}
} else if (theValue.length() == 8 && ourYearMonthDayPattern.matcher(theValue).matches()) {
// Eg. 19840101 (allow this just to be lenient)
if (isPrecisionAllowed(DAY)) {
setPrecision(DAY);
clearTimeZone();
return ((ourYearMonthDayNoDashesFormat).parse(theValue));
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() == 10 && ourYearDashMonthDashDayPattern.matcher(theValue).matches()) {
// E.g. 1984-01-01 (this is valid according to the spec)
if (isPrecisionAllowed(DAY)) {
setPrecision(DAY);
clearTimeZone();
return ((ourYearMonthDayFormat).parse(theValue));
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() >= 18) {
int dotIndex = theValue.indexOf('.', 18);
if (dotIndex == -1 && !isPrecisionAllowed(SECOND)) {
throw new DataFormatException("Invalid date/time string (data type does not support SECONDS precision): " + theValue);
} else if (dotIndex > -1 && !isPrecisionAllowed(MILLI)) {
throw new DataFormatException("Invalid date/time string (data type " + getClass().getSimpleName() + " does not support MILLIS precision):" + theValue);
}
Calendar cal;
try {
cal = DatatypeConverter.parseDateTime(theValue);
} catch (IllegalArgumentException e) {
throw new DataFormatException("Invalid data/time string (" + e.getMessage() + "): " + theValue);
}
if (dotIndex == -1) {
setPrecision(TemporalPrecisionEnum.SECOND);
} else {
setPrecision(TemporalPrecisionEnum.MILLI);
}
clearTimeZone();
if (theValue.endsWith("Z")) {
myTimeZoneZulu = true;
} else if (theValue.indexOf('+', 19) != -1 || theValue.indexOf('-', 19) != -1) {
myTimeZone = cal.getTimeZone();
}
return cal.getTime();
} else {
throw new DataFormatException("Invalid date/time string (invalid length): " + theValue);
}
} catch (ParseException e) {
throw new DataFormatException("Invalid date string (" + e.getMessage() + "): " + theValue);
}
}
/**
@ -164,103 +250,14 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
@Override
public void setValue(Date theValue) throws DataFormatException {
myValue = theValue;
clearTimeZone();
super.setValue(theValue);
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
try {
if (theValue == null) {
myValue = null;
clearTimeZone();
} else if (theValue.length() == 4 && ourYearPattern.matcher(theValue).matches()) {
if (isPrecisionAllowed(YEAR)) {
setValue((ourYearFormat).parse(theValue));
setPrecision(YEAR);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support YEAR precision): " + theValue);
}
} else if (theValue.length() == 6 && ourYearMonthPattern.matcher(theValue).matches()) {
// Eg. 198401 (allow this just to be lenient)
if (isPrecisionAllowed(MONTH)) {
setValue((ourYearMonthNoDashesFormat).parse(theValue));
setPrecision(MONTH);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() == 7 && ourYearDashMonthPattern.matcher(theValue).matches()) {
// E.g. 1984-01 (this is valid according to the spec)
if (isPrecisionAllowed(MONTH)) {
setValue((ourYearMonthFormat).parse(theValue));
setPrecision(MONTH);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support MONTH precision): " + theValue);
}
} else if (theValue.length() == 8 && ourYearMonthDayPattern.matcher(theValue).matches()) {
// Eg. 19840101 (allow this just to be lenient)
if (isPrecisionAllowed(DAY)) {
setValue((ourYearMonthDayNoDashesFormat).parse(theValue));
setPrecision(DAY);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() == 10 && ourYearDashMonthDashDayPattern.matcher(theValue).matches()) {
// E.g. 1984-01-01 (this is valid according to the spec)
if (isPrecisionAllowed(DAY)) {
setValue((ourYearMonthDayFormat).parse(theValue));
setPrecision(DAY);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() >= 18) {
int dotIndex = theValue.indexOf('.', 18);
if (dotIndex == -1 && !isPrecisionAllowed(SECOND)) {
throw new DataFormatException("Invalid date/time string (data type does not support SECONDS precision): " + theValue);
} else if (dotIndex > -1 && !isPrecisionAllowed(MILLI)) {
throw new DataFormatException("Invalid date/time string (data type " + getClass().getSimpleName() + " does not support MILLIS precision):" + theValue);
}
Calendar cal;
try {
cal = DatatypeConverter.parseDateTime(theValue);
} catch (IllegalArgumentException e) {
throw new DataFormatException("Invalid data/time string (" + e.getMessage() + "): " + theValue);
}
myValue = cal.getTime();
if (dotIndex == -1) {
setPrecision(TemporalPrecisionEnum.SECOND);
} else {
setPrecision(TemporalPrecisionEnum.MILLI);
}
clearTimeZone();
if (theValue.endsWith("Z")) {
myTimeZoneZulu = true;
} else if (theValue.indexOf('+', 19) != -1 || theValue.indexOf('-', 19) != -1) {
myTimeZone = cal.getTimeZone();
}
} else {
throw new DataFormatException("Invalid date/time string (invalid length): " + theValue);
}
} catch (ParseException e) {
throw new DataFormatException("Invalid date string (" + e.getMessage() + "): " + theValue);
}
clearTimeZone();
super.setValueAsString(theValue);
}
private void clearTimeZone() {
myTimeZone = null;
myTimeZoneZulu = false;
}
/**
* To be implemented by subclasses to indicate whether the given precision is allowed by this type
*/
abstract boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision);
}

View File

@ -28,8 +28,6 @@ import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "boolean")
public class BooleanDt extends BasePrimitive<Boolean> {
private Boolean myValue;
/**
* Constructor
*/
@ -41,45 +39,28 @@ public class BooleanDt extends BasePrimitive<Boolean> {
* Constructor
*/
@SimpleSetter
public BooleanDt(@SimpleSetter.Parameter(name="theBoolean") boolean theBoolean) {
public BooleanDt(@SimpleSetter.Parameter(name = "theBoolean") boolean theBoolean) {
setValue(theBoolean);
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
protected Boolean parse(String theValue) {
if ("true".equals(theValue)) {
myValue = Boolean.TRUE;
return Boolean.TRUE;
} else if ("false".equals(theValue)) {
myValue = Boolean.FALSE;
return Boolean.FALSE;
} else {
throw new DataFormatException("Invalid boolean string: '" + theValue + "'");
}
}
@Override
public String getValueAsString() {
if (myValue == null) {
return null;
} else if (Boolean.TRUE.equals(myValue)) {
protected String encode(Boolean theValue) {
if (Boolean.TRUE.equals(theValue)) {
return "true";
} else {
return "false";
}
}
@Override
public void setValue(Boolean theValue) {
myValue = theValue;
}
@Override
public Boolean getValue() {
return myValue;
}
}

View File

@ -29,8 +29,6 @@ import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "code")
public class CodeDt extends BasePrimitive<String> implements ICodedDatatype, Comparable<CodeDt> {
private String myValue;
/**
* Constructor
*/
@ -46,31 +44,6 @@ public class CodeDt extends BasePrimitive<String> implements ICodedDatatype, Com
setValue(theCode);
}
@Override
public String getValue() {
return myValue;
}
@Override
public void setValue(String theValue) throws DataFormatException {
if (theValue == null) {
myValue = null;
} else {
String newValue = theValue.trim();
myValue = newValue;
}
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
setValue(theValue);
}
@Override
public String getValueAsString() {
return getValue();
}
@Override
public int compareTo(CodeDt theCode) {
if (theCode == null) {
@ -79,4 +52,14 @@ public class CodeDt extends BasePrimitive<String> implements ICodedDatatype, Com
return defaultString(getValue()).compareTo(defaultString(theCode.getValue()));
}
@Override
protected String parse(String theValue) {
return theValue.trim();
}
@Override
protected String encode(String theValue) {
return theValue;
}
}

View File

@ -27,13 +27,10 @@ import java.math.RoundingMode;
import ca.uhn.fhir.model.api.BasePrimitive;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "decimal")
public class DecimalDt extends BasePrimitive<BigDecimal> implements Comparable<DecimalDt> {
private BigDecimal myValue;
/**
* Constructor
*/
@ -41,10 +38,6 @@ public class DecimalDt extends BasePrimitive<BigDecimal> implements Comparable<D
super();
}
public Number getValueAsNumber() {
return myValue;
}
/**
* Constructor
*/
@ -63,31 +56,6 @@ public class DecimalDt extends BasePrimitive<BigDecimal> implements Comparable<D
setValue(BigDecimal.valueOf(theValue));
}
/**
* Rounds the value to the given prevision
*
* @see MathContext#getPrecision()
*/
public void round(int thePrecision) {
if (getValue()!=null) {
BigDecimal newValue = getValue().round(new MathContext(thePrecision));
setValue(newValue);
}
}
/**
* Rounds the value to the given prevision
*
* @see MathContext#getPrecision()
* @see MathContext#getRoundingMode()
*/
public void round(int thePrecision, RoundingMode theRoundingMode) {
if (getValue()!=null) {
BigDecimal newValue = getValue().round(new MathContext(thePrecision, theRoundingMode));
setValue(newValue);
}
}
/**
* Constructor
*/
@ -104,58 +72,70 @@ public class DecimalDt extends BasePrimitive<BigDecimal> implements Comparable<D
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
if (theValue == null) {
myValue = null;
} else {
myValue = new BigDecimal(theValue);
public int compareTo(DecimalDt theObj) {
if (getValue() == null && theObj.getValue() == null) {
return 0;
}
}
@Override
public String getValueAsString() {
if (myValue == null) {
return null;
if (getValue() != null && theObj.getValue() == null) {
return 1;
}
return myValue.toPlainString();
if (getValue() == null && theObj.getValue() != null) {
return -1;
}
return getValue().compareTo(theObj.getValue());
}
@Override
public BigDecimal getValue() {
return myValue;
}
@Override
public void setValue(BigDecimal theValue) throws DataFormatException {
myValue = theValue;
}
/**
* Sets a new value using an integer
*/
public void setValueAsInteger(int theValue) {
myValue = new BigDecimal(theValue);
protected String encode(BigDecimal theValue) {
return getValue().toPlainString();
}
/**
* Gets the value as an integer, using {@link BigDecimal#intValue()}
*/
public int getValueAsInteger() {
return myValue.intValue();
return getValue().intValue();
}
public Number getValueAsNumber() {
return getValue();
}
@Override
public int compareTo(DecimalDt theObj) {
if (myValue == null && theObj.getValue() == null) {
return 0;
protected BigDecimal parse(String theValue) {
return new BigDecimal(theValue);
}
/**
* Rounds the value to the given prevision
*
* @see MathContext#getPrecision()
*/
public void round(int thePrecision) {
if (getValue() != null) {
BigDecimal newValue = getValue().round(new MathContext(thePrecision));
setValue(newValue);
}
if (myValue != null && theObj.getValue() == null) {
return 1;
}
/**
* Rounds the value to the given prevision
*
* @see MathContext#getPrecision()
* @see MathContext#getRoundingMode()
*/
public void round(int thePrecision, RoundingMode theRoundingMode) {
if (getValue() != null) {
BigDecimal newValue = getValue().round(new MathContext(thePrecision, theRoundingMode));
setValue(newValue);
}
if (myValue == null && theObj.getValue() != null) {
return -1;
}
return myValue.compareTo(theObj.getValue());
}
/**
* Sets a new value using an integer
*/
public void setValueAsInteger(int theValue) {
setValue(new BigDecimal(theValue));
}
}

View File

@ -20,7 +20,8 @@ package ca.uhn.fhir.model.primitive;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.*;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.math.BigDecimal;
@ -28,7 +29,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import ca.uhn.fhir.model.api.BasePrimitive;
import ca.uhn.fhir.model.api.IPrimitiveDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
@ -47,7 +48,7 @@ import ca.uhn.fhir.util.UrlUtil;
* </p>
*/
@DatatypeDef(name = "id")
public class IdDt extends BasePrimitive<String> {
public class IdDt implements IPrimitiveDatatype<String> {
private String myBaseUrl;
private boolean myHaveComponentParts;
@ -312,7 +313,6 @@ public class IdDt extends BasePrimitive<String> {
/**
* Copies the value from the given IdDt to <code>this</code> IdDt. It is generally not neccesary to use this method but it is provided for consistency with the rest of the API.
*/
@Override
public void setId(IdDt theId) {
setValue(theId.getValue());
}
@ -481,4 +481,9 @@ public class IdDt extends BasePrimitive<String> {
return theIdPart.toPlainString();
}
@Override
public boolean isEmpty() {
return isNotBlank(getValue());
}
}

View File

@ -20,43 +20,19 @@ package ca.uhn.fhir.model.primitive;
* #L%
*/
import ca.uhn.fhir.model.api.BasePrimitive;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "idref")
public class IdrefDt extends BasePrimitive<String> {
public class IdrefDt extends StringDt {
private IElement myTarget;
private String myValue;
public IElement getTarget() {
return myTarget;
}
@Override
public String getValue() {
return myValue;
}
@Override
public String getValueAsString() throws DataFormatException {
return myValue;
}
public void setTarget(IElement theTarget) {
myTarget = theTarget;
}
@Override
public void setValue(String theValue) throws DataFormatException {
myValue = theValue;
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
myValue = theValue;
}
}

View File

@ -28,8 +28,6 @@ import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "integer")
public class IntegerDt extends BasePrimitive<Integer> {
private Integer myValue;
/**
* Constructor
*/
@ -48,46 +46,27 @@ public class IntegerDt extends BasePrimitive<Integer> {
/**
* Constructor
*
* @param theIntegerAsString A string representation of an integer
* @throws DataFormatException If the string is not a valid integer representation
* @param theIntegerAsString
* A string representation of an integer
* @throws DataFormatException
* If the string is not a valid integer representation
*/
public IntegerDt(String theIntegerAsString) {
setValueAsString(theIntegerAsString);
}
public IntegerDt(Long theCount) {
// TODO Auto-generated constructor stub
}
@Override
public Integer getValue() {
return myValue;
}
@Override
public void setValue(Integer theValue) {
myValue = theValue;
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
if (theValue == null) {
myValue = null;
} else {
try {
myValue = Integer.parseInt(theValue);
} catch (NumberFormatException e) {
throw new DataFormatException(e);
}
protected Integer parse(String theValue) {
try {
return Integer.parseInt(theValue);
} catch (NumberFormatException e) {
throw new DataFormatException(e);
}
}
@Override
public String getValueAsString() {
if (myValue == null) {
return null;
}
return Integer.toString(myValue);
protected String encode(Integer theValue) {
return Integer.toString(theValue);
}
}

View File

@ -26,13 +26,10 @@ import ca.uhn.fhir.model.api.BasePrimitive;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "string")
public class StringDt extends BasePrimitive<String> implements IQueryParameterType {
private String myValue;
/**
* Create a new String
*/
@ -45,31 +42,11 @@ public class StringDt extends BasePrimitive<String> implements IQueryParameterTy
*/
@SimpleSetter
public StringDt(@SimpleSetter.Parameter(name = "theString") String theValue) {
myValue = theValue;
}
@Override
public String getValue() {
return myValue;
setValue(theValue);
}
public String getValueNotNull() {
return StringUtils.defaultString(myValue);
}
@Override
public String getValueAsString() {
return myValue;
}
@Override
public void setValue(String theValue) throws DataFormatException {
myValue = theValue;
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
myValue = theValue;
return StringUtils.defaultString(getValue());
}
/**
@ -77,14 +54,14 @@ public class StringDt extends BasePrimitive<String> implements IQueryParameterTy
*/
@Override
public String toString() {
return myValue;
return getValue();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((myValue == null) ? 0 : myValue.hashCode());
result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode());
return result;
}
@ -97,10 +74,10 @@ public class StringDt extends BasePrimitive<String> implements IQueryParameterTy
if (getClass() != obj.getClass())
return false;
StringDt other = (StringDt) obj;
if (myValue == null) {
if (other.myValue != null)
if (getValue() == null) {
if (other.getValue() != null)
return false;
} else if (!myValue.equals(other.myValue))
} else if (!getValue().equals(other.getValue()))
return false;
return true;
}
@ -135,4 +112,14 @@ public class StringDt extends BasePrimitive<String> implements IQueryParameterTy
return null;
}
@Override
protected String parse(String theValue) {
return theValue;
}
@Override
protected String encode(String theValue) {
return theValue;
}
}

View File

@ -20,28 +20,25 @@ package ca.uhn.fhir.model.primitive;
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import ca.uhn.fhir.model.api.BasePrimitive;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
/**
* Represents a Time datatype, per the FHIR specification. A time is a specification of hours and minutes (and optionally
* milliseconds), with NO date and NO timezone information attached. It is expressed as a string in the form
* <code>HH:mm:ss[.SSSS]</code>
* Represents a Time datatype, per the FHIR specification. A time is a specification of hours and minutes (and optionally milliseconds), with NO date and NO timezone information attached. It is
* expressed as a string in the form <code>HH:mm:ss[.SSSS]</code>
*
* <p>
* This datatype is not valid in FHIR DSTU1
* </p>
*
* @since FHIR DSTU 2 / HAPI 0.8
*
* TODO: have a way of preventing this from being used in DSTU1 resources
* TODO: validate time?
*/
@DatatypeDef(name = "time")
public class TimeDt extends BasePrimitive<String> implements IQueryParameterType {
private String myValue;
public class TimeDt extends StringDt implements IQueryParameterType {
/**
* Create a new String
@ -55,94 +52,7 @@ public class TimeDt extends BasePrimitive<String> implements IQueryParameterType
*/
@SimpleSetter
public TimeDt(@SimpleSetter.Parameter(name = "theString") String theValue) {
myValue = theValue;
}
@Override
public String getValue() {
return myValue;
}
public String getValueNotNull() {
return StringUtils.defaultString(myValue);
}
@Override
public String getValueAsString() {
return myValue;
}
@Override
public void setValue(String theValue) throws DataFormatException {
myValue = theValue;
}
@Override
public void setValueAsString(String theValue) throws DataFormatException {
myValue = theValue;
}
/**
* Returns the value of this string, or <code>null</code>
*/
@Override
public String toString() {
return myValue;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((myValue == null) ? 0 : myValue.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TimeDt other = (TimeDt) obj;
if (myValue == null) {
if (other.myValue != null)
return false;
} else if (!myValue.equals(other.myValue))
return false;
return true;
}
/**
* {@inheritDoc}
*/
@Override
public void setValueAsQueryToken(String theQualifier, String theValue) {
setValue(theValue);
}
/**
* {@inheritDoc}
*/
@Override
public String getValueAsQueryToken() {
return getValue();
}
/**
* Returns <code>true</code> if this datatype has no extensions, and has either a <code>null</code> value or an empty ("") value.
*/
@Override
public boolean isEmpty() {
boolean retVal = super.isBaseEmpty() && StringUtils.isBlank(getValue());
return retVal;
}
@Override
public String getQueryParameterQualifier() {
return null;
}
}