From 76ede7af8e9cc0706756d0dd2b5df47098571c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sun, 26 Nov 2017 20:28:48 +0100 Subject: [PATCH] [attributes] Add toJson() to attribute API. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../arakhne/afc/attrs/attr/AttributeImpl.java | 8 ++++---- .../arakhne/afc/attrs/attr/AttributeValue.java | 3 ++- .../afc/attrs/attr/AttributeValueImpl.java | 18 +++++++++++++++--- .../collection/AbstractAttributeProvider.java | 16 ++++++++++++++++ .../attrs/collection/AttributeProvider.java | 3 ++- .../collection/HeapAttributeCollection.java | 6 ------ .../collection/MultiAttributeProvider.java | 8 ++++++++ 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeImpl.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeImpl.java index 700ed2aef..6947aa431 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeImpl.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeImpl.java @@ -33,7 +33,7 @@ import org.arakhne.afc.math.geometry.d2.Point2D; import org.arakhne.afc.math.geometry.d3.Point3D; -import org.arakhne.afc.vmutil.ReflectionUtil; +import org.arakhne.afc.vmutil.json.JsonBuffer; /** * This class contains an attribute value. @@ -406,10 +406,10 @@ public int hashCode() { return result ^ (result >> 31); } - @Pure @Override - public String toString() { - return ReflectionUtil.toString(this); + public void toJson(JsonBuffer buffer) { + super.toJson(buffer); + buffer.add("name", this.name); //$NON-NLS-1$ } /** Assert that the attribute value was assigned and not null. diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValue.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValue.java index f763cf823..496648660 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValue.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValue.java @@ -33,6 +33,7 @@ import org.arakhne.afc.math.geometry.d2.Point2D; import org.arakhne.afc.math.geometry.d3.Point3D; +import org.arakhne.afc.vmutil.json.JsonableObject; /** * This class contains a metadata value. @@ -43,7 +44,7 @@ * @mavenartifactid $ArtifactId$ */ @SuppressWarnings({"checkstyle:methodcount"}) -public interface AttributeValue extends Cloneable, Serializable { +public interface AttributeValue extends Cloneable, Serializable, JsonableObject { /** * Replies a comparator suitable for attribute values. diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValueImpl.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValueImpl.java index 606e67ebd..3a70ea8c1 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValueImpl.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/attr/AttributeValueImpl.java @@ -60,7 +60,7 @@ import org.arakhne.afc.math.geometry.d3.Point3D; import org.arakhne.afc.math.geometry.d3.Tuple3D; import org.arakhne.afc.math.geometry.d3.d.Point3d; -import org.arakhne.afc.vmutil.ReflectionUtil; +import org.arakhne.afc.vmutil.json.JsonBuffer; /** * This class contains an attribute value. @@ -569,8 +569,20 @@ public int hashCode() { @Pure @Override - public String toString() { - return ReflectionUtil.toString(this); + public final String toString() { + final JsonBuffer buffer = new JsonBuffer(); + toJson(buffer); + return buffer.toString(); + } + + @Override + public void toJson(JsonBuffer buffer) { + if (isAssigned() && this.value != null) { + buffer.add("value", this.value); //$NON-NLS-1$ + } + if (this.type != null) { + buffer.add("type", this.type.name()); //$NON-NLS-1$ + } } @Pure diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AbstractAttributeProvider.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AbstractAttributeProvider.java index 29e42435a..17b08639e 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AbstractAttributeProvider.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AbstractAttributeProvider.java @@ -28,6 +28,7 @@ import java.util.Iterator; import java.util.UUID; +import com.google.common.collect.Iterables; import org.eclipse.xtext.xbase.lib.Pure; import org.arakhne.afc.attrs.attr.Attribute; @@ -35,6 +36,7 @@ import org.arakhne.afc.attrs.attr.AttributeType; import org.arakhne.afc.attrs.attr.AttributeValue; import org.arakhne.afc.attrs.attr.NullAttribute; +import org.arakhne.afc.vmutil.json.JsonBuffer; /** * This class implements an abstract attribute provider. @@ -449,4 +451,18 @@ public Class getAttribute(String name, Class defaultValue) { return defaultValue; } + @Override + public final String toString() { + final JsonBuffer buffer = new JsonBuffer(); + toJson(buffer); + return buffer.toString(); + } + + @Override + public void toJson(JsonBuffer buffer) { + buffer.add("attributes", Iterables.filter(attributes(), attr -> { //$NON-NLS-1$ + return attr.isAssigned(); + })); + } + } diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AttributeProvider.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AttributeProvider.java index fb39f14fc..4d113959b 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AttributeProvider.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/AttributeProvider.java @@ -37,6 +37,7 @@ import org.arakhne.afc.attrs.attr.AttributeException; import org.arakhne.afc.attrs.attr.AttributeType; import org.arakhne.afc.attrs.attr.AttributeValue; +import org.arakhne.afc.vmutil.json.JsonableObject; /** * This interface representes a provider of attributes @@ -47,7 +48,7 @@ * @mavengroupid $GroupId$ * @mavenartifactid $ArtifactId$ */ -public interface AttributeProvider extends Cloneable, Serializable { +public interface AttributeProvider extends Cloneable, Serializable, JsonableObject { /** Make a deep copy of this object and replies the copy. * diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/HeapAttributeCollection.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/HeapAttributeCollection.java index 04bc793e0..d3015ed5c 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/HeapAttributeCollection.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/HeapAttributeCollection.java @@ -764,11 +764,5 @@ public void flush() { // Do nothing } - @Pure - @Override - public String toString() { - return this.heap.toString(); - } - } diff --git a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/MultiAttributeProvider.java b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/MultiAttributeProvider.java index 4a02ba8aa..601d00ce2 100644 --- a/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/MultiAttributeProvider.java +++ b/advanced/attributes/src/main/java/org/arakhne/afc/attrs/collection/MultiAttributeProvider.java @@ -38,6 +38,7 @@ import org.arakhne.afc.attrs.attr.AttributeValue; import org.arakhne.afc.attrs.attr.AttributeValueImpl; import org.arakhne.afc.references.SoftValueTreeMap; +import org.arakhne.afc.vmutil.json.JsonBuffer; /** * This class contains a collection of attribute containers and @@ -334,6 +335,13 @@ static class ManyValueAttributeValue extends AttributeValueImpl { private AttributeType topType; + @Override + public void toJson(JsonBuffer buffer) { + super.toJson(buffer); + buffer.add("topType", this.topType); //$NON-NLS-1$ + buffer.add("hasMultipleValues", this.hasMultipleValues); //$NON-NLS-1$ + } + /** Replies the type type associated to this attribute value. * * @return the top type associated to this attribute value.