From 96bd38c71caaf97186f8ed6821e726c7512be4ee Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sun, 14 Jul 2019 18:12:26 -0400 Subject: [PATCH] Improve internal representation of binary datatype --- .../fhir/dstu3/model/Base64BinaryType.java | 100 +++++++++++++----- .../hl7/fhir/r4/model/Base64BinaryType.java | 49 ++++++++- .../hl7/fhir/r5/model/Base64BinaryType.java | 49 ++++++++- 3 files changed, 166 insertions(+), 32 deletions(-) diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Base64BinaryType.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Base64BinaryType.java index 3164a334b..bc9c3967a 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Base64BinaryType.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Base64BinaryType.java @@ -37,9 +37,9 @@ package org.hl7.fhir.dstu3.model; * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -49,34 +49,42 @@ package org.hl7.fhir.dstu3.model; */ -import org.apache.commons.codec.binary.Base64; - +import ca.uhn.fhir.model.api.IElement; import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import org.apache.commons.codec.binary.Base64; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.IPrimitiveType; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; /** * Primitive type "base64Binary" in FHIR: a sequence of bytes represented in base64 */ -@DatatypeDef(name="base64Binary") -public class Base64BinaryType extends PrimitiveType { +@DatatypeDef(name = "base64Binary") +public class Base64BinaryType extends Type implements IPrimitiveType, IBaseHasExtensions, IElement, Externalizable { - private static final long serialVersionUID = 3L; + private static final long serialVersionUID = 3L; + private byte[] myValue; - /** - * Constructor - */ - public Base64BinaryType() { - super(); - } + /** + * Constructor + */ + public Base64BinaryType() { + super(); + } - public Base64BinaryType(byte[] theBytes) { - super(); - setValue(theBytes); - } + public Base64BinaryType(byte[] theBytes) { + super(); + setValue(theBytes); + } - public Base64BinaryType(String theValue) { - super(); - setValueAsString(theValue); - } + public Base64BinaryType(String theValue) { + super(); + setValueAsString(theValue); + } protected byte[] parse(String theValue) { return Base64.decodeBase64(theValue.getBytes(ca.uhn.fhir.rest.api.Constants.CHARSET_UTF8)); @@ -89,12 +97,48 @@ public class Base64BinaryType extends PrimitiveType { return new String(Base64.encodeBase64(theValue), ca.uhn.fhir.rest.api.Constants.CHARSET_UTF8); } - @Override - public Base64BinaryType copy() { - return new Base64BinaryType(getValue()); - } + @Override + public Base64BinaryType copy() { + return new Base64BinaryType(getValue()); + } - public String fhirType() { - return "base64Binary"; - } + @Override + protected Type typedCopy() { + return null; + } + + public String fhirType() { + return "base64Binary"; + } + + @Override + public void writeExternal(ObjectOutput out) throws IOException { + out.writeObject(getValue()); + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + setValue((byte[]) in.readObject()); + } + + @Override + public String getValueAsString() { + return encode(myValue); + } + + @Override + public void setValueAsString(String theValue) throws IllegalArgumentException { + setValue(parse(theValue)); + } + + @Override + public byte[] getValue() { + return myValue; + } + + @Override + public IPrimitiveType setValue(byte[] theValue) throws IllegalArgumentException { + myValue = theValue; + return this; + } } diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/Base64BinaryType.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/Base64BinaryType.java index 493103410..386ad336f 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/Base64BinaryType.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/Base64BinaryType.java @@ -49,17 +49,26 @@ package org.hl7.fhir.r4.model; */ +import ca.uhn.fhir.model.api.IElement; import org.apache.commons.codec.binary.Base64; import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.IPrimitiveType; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; /** * Primitive type "base64Binary" in FHIR: a sequence of bytes represented in base64 */ -@DatatypeDef(name="base64Binary") -public class Base64BinaryType extends PrimitiveType { +@DatatypeDef(name = "base64Binary") +public class Base64BinaryType extends Type implements IPrimitiveType, IBaseHasExtensions, IElement, Externalizable { private static final long serialVersionUID = 3L; + private byte[] myValue; /** * Constructor @@ -94,7 +103,43 @@ public class Base64BinaryType extends PrimitiveType { return new Base64BinaryType(getValue()); } + @Override + protected Type typedCopy() { + return null; + } + public String fhirType() { return "base64Binary"; } + + @Override + public void writeExternal(ObjectOutput out) throws IOException { + out.writeObject(getValue()); + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + setValue((byte[]) in.readObject()); + } + + @Override + public String getValueAsString() { + return encode(myValue); + } + + @Override + public void setValueAsString(String theValue) throws IllegalArgumentException { + setValue(parse(theValue)); + } + + @Override + public byte[] getValue() { + return myValue; + } + + @Override + public IPrimitiveType setValue(byte[] theValue) throws IllegalArgumentException { + myValue = theValue; + return this; + } } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Base64BinaryType.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Base64BinaryType.java index b6d65d810..b5206dd36 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Base64BinaryType.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Base64BinaryType.java @@ -49,17 +49,26 @@ package org.hl7.fhir.r5.model; */ +import ca.uhn.fhir.model.api.IElement; import org.apache.commons.codec.binary.Base64; import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.IPrimitiveType; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; /** * Primitive type "base64Binary" in FHIR: a sequence of bytes represented in base64 */ -@DatatypeDef(name="base64Binary") -public class Base64BinaryType extends PrimitiveType { +@DatatypeDef(name = "base64Binary") +public class Base64BinaryType extends Type implements IPrimitiveType, IBaseHasExtensions, IElement, Externalizable { private static final long serialVersionUID = 3L; + private byte[] myValue; /** * Constructor @@ -94,7 +103,43 @@ public class Base64BinaryType extends PrimitiveType { return new Base64BinaryType(getValue()); } + @Override + protected Type typedCopy() { + return null; + } + public String fhirType() { return "base64Binary"; } + + @Override + public void writeExternal(ObjectOutput out) throws IOException { + out.writeObject(getValue()); + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + setValue((byte[]) in.readObject()); + } + + @Override + public String getValueAsString() { + return encode(myValue); + } + + @Override + public void setValueAsString(String theValue) throws IllegalArgumentException { + setValue(parse(theValue)); + } + + @Override + public byte[] getValue() { + return myValue; + } + + @Override + public IPrimitiveType setValue(byte[] theValue) throws IllegalArgumentException { + myValue = theValue; + return this; + } }