Skip to content

Commit

Permalink
[attributes] Add toJson() to attribute API.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 26, 2017
1 parent 7b09efa commit 76ede7a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <code>null</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
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;
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.attrs.attr.NullAttribute;
import org.arakhne.afc.vmutil.json.JsonBuffer;

/**
* This class implements an abstract attribute provider.
Expand Down Expand Up @@ -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();
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,5 @@ public void flush() {
// Do nothing
}

@Pure
@Override
public String toString() {
return this.heap.toString();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 76ede7a

Please sign in to comment.