Skip to content

Commit

Permalink
Add value attributes (#315)
Browse files Browse the repository at this point in the history
* Add value attributes, needs tests

* remove comment

* Add test

* Add tests
  • Loading branch information
wsargent authored Apr 13, 2024
1 parent 198b5b7 commit 58d7803
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 57 deletions.
194 changes: 187 additions & 7 deletions api/src/main/java/com/tersesystems/echopraxia/api/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ protected Value() {}
@NotNull
public abstract Value.Type type();

/**
* @return the attributes associated with the value.
*/
public Attributes attributes() {
return Attributes.empty();
}

/**
* Sets the attributes to newAttributes, replacing the original value.
*
* @param newAttributes the new attributes.
* @return the value with attributes.
*/
public abstract Value<V> withAttributes(Attributes newAttributes);

/**
* @return this value as an object value.
*/
Expand Down Expand Up @@ -499,8 +514,7 @@ private static <T> List<Value<?>> asList(
return list;
}

public static final class BooleanValue extends Value<Boolean> {

public static class BooleanValue extends Value<Boolean> implements Comparable<BooleanValue> {
public static final BooleanValue TRUE = new BooleanValue(true);

public static final BooleanValue FALSE = new BooleanValue(false);
Expand All @@ -520,6 +534,30 @@ private BooleanValue(Boolean bool) {
public @NotNull Value.Type type() {
return Type.BOOLEAN;
}

@Override
public BooleanValue withAttributes(Attributes newAttributes) {
return new BooleanValueWithAttributes(raw, newAttributes);
}

@Override
public int compareTo(@NotNull Value.BooleanValue o) {
return this.raw.compareTo(o.raw);
}
}

private static final class BooleanValueWithAttributes extends BooleanValue {
private final Attributes attributes;

BooleanValueWithAttributes(Boolean bool, Attributes attrs) {
super(bool);
this.attributes = attrs;
}

@Override
public Attributes attributes() {
return this.attributes;
}
}

public static class NumberValue<N extends Number & Comparable<N>> extends Value<N>
Expand All @@ -540,6 +578,11 @@ public N raw() {
return Type.NUMBER;
}

@Override
public NumberValue<N> withAttributes(Attributes newAttributes) {
return new NumberValueWithAttributes<>(raw, newAttributes);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -577,6 +620,22 @@ private Cache() {}
}
}

private static final class NumberValueWithAttributes<N extends Number & Comparable<N>>
extends NumberValue<N> {

private final Attributes attributes;

private NumberValueWithAttributes(N number, Attributes attributes) {
super(number);
this.attributes = attributes;
}

@Override
public Attributes attributes() {
return this.attributes;
}
}

private static final class ShortValue extends NumberValue<Short> {
public static final ShortValue ZERO = new ShortValue((short) 0);

Expand Down Expand Up @@ -663,7 +722,7 @@ private BigDecimalValue(BigDecimal number) {
}
}

public static final class StringValue extends Value<String> implements Comparable<StringValue> {
public static class StringValue extends Value<String> implements Comparable<StringValue> {
private final String raw;

private StringValue(String s) {
Expand All @@ -680,6 +739,11 @@ public String raw() {
return Type.STRING;
}

@Override
public StringValue withAttributes(Attributes newAttributes) {
return new StringValueWithAttributes(raw, newAttributes);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -699,7 +763,22 @@ public int compareTo(@NotNull Value.StringValue o) {
}
}

public static final class ArrayValue extends Value<List<Value<?>>> {
private static final class StringValueWithAttributes extends StringValue {

private final Attributes attributes;

public StringValueWithAttributes(String raw, Attributes newAttributes) {
super(raw);
this.attributes = newAttributes;
}

@Override
public Attributes attributes() {
return attributes;
}
}

public static class ArrayValue extends Value<List<Value<?>>> {
private final List<Value<?>> raw;

public static final ArrayValue EMPTY = new ArrayValue(Collections.emptyList());
Expand All @@ -718,6 +797,11 @@ private ArrayValue(List<Value<?>> raw) {
return Type.ARRAY;
}

@Override
public ArrayValue withAttributes(Attributes newAttributes) {
return new ArrayValueWithAttributes(raw, newAttributes);
}

public ArrayValue add(Value<?> value) {
ArrayList<Value<?>> values = new ArrayList<>(this.raw);
values.add(value);
Expand All @@ -744,7 +828,34 @@ public int hashCode() {
}
}

public static final class ObjectValue extends Value<List<Field>> {
private static final class ArrayValueWithAttributes extends ArrayValue {

private final Attributes attributes;

public ArrayValueWithAttributes(List<Value<?>> raw, Attributes newAttributes) {
super(raw);
this.attributes = newAttributes;
}

@Override
public Attributes attributes() {
return attributes;
}

public ArrayValue add(Value<?> value) {
ArrayList<Value<?>> values = new ArrayList<>(raw());
values.add(value);
return new ArrayValueWithAttributes(values, attributes);
}

public ArrayValue addAll(Collection<Value<?>> values) {
ArrayList<Value<?>> joinedValues = new ArrayList<>(raw());
joinedValues.addAll(values);
return new ArrayValueWithAttributes(joinedValues, attributes);
}
}

public static class ObjectValue extends Value<List<Field>> {
public static final ObjectValue EMPTY = new ObjectValue(Collections.emptyList());

private final List<Field> raw;
Expand All @@ -758,6 +869,11 @@ private ObjectValue(List<Field> raw) {
return Type.OBJECT;
}

@Override
public ObjectValue withAttributes(Attributes newAttributes) {
return new ObjectValueWithAttributes(raw, newAttributes);
}

@Override
public List<Field> raw() {
return raw;
Expand Down Expand Up @@ -789,7 +905,34 @@ public int hashCode() {
}
}

public static final class NullValue extends Value<Void> {
private static final class ObjectValueWithAttributes extends ObjectValue {

private final Attributes attributes;

public ObjectValueWithAttributes(List<Field> raw, Attributes newAttributes) {
super(raw);
this.attributes = newAttributes;
}

@Override
public Attributes attributes() {
return attributes;
}

public ObjectValue add(Field field) {
ArrayList<Field> fields = new ArrayList<>(raw());
fields.add(field);
return new ObjectValueWithAttributes(fields, attributes);
}

public ObjectValue addAll(Collection<Field> fields) {
ArrayList<Field> joinedFields = new ArrayList<>(raw());
joinedFields.addAll(fields);
return new ObjectValueWithAttributes(joinedFields, attributes);
}
}

public static class NullValue extends Value<Void> {
// Should not be able to instantiate this outside of class.
private NullValue() {}

Expand All @@ -803,10 +946,28 @@ public Void raw() {
return Type.NULL;
}

@Override
public NullValue withAttributes(Attributes newAttributes) {
return new NullValueWithAttributes(newAttributes);
}

public static final @NotNull NullValue instance = new NullValue();
}

public static final class ExceptionValue extends Value<Throwable> {
private static final class NullValueWithAttributes extends NullValue {
private final Attributes attributes;

public NullValueWithAttributes(Attributes newAttributes) {
this.attributes = newAttributes;
}

@Override
public Attributes attributes() {
return attributes;
}
}

public static class ExceptionValue extends Value<Throwable> {
private final Throwable raw;

private ExceptionValue(Throwable raw) {
Expand All @@ -818,6 +979,11 @@ private ExceptionValue(Throwable raw) {
return Type.EXCEPTION;
}

@Override
public ExceptionValue withAttributes(Attributes newAttributes) {
return new ExceptionValueWithAttributes(raw, newAttributes);
}

@Override
public Throwable raw() {
return raw;
Expand All @@ -836,4 +1002,18 @@ public int hashCode() {
return raw != null ? raw.hashCode() : 0;
}
}

private static final class ExceptionValueWithAttributes extends ExceptionValue {
private final Attributes attributes;

public ExceptionValueWithAttributes(Throwable raw, Attributes newAttributes) {
super(raw);
this.attributes = newAttributes;
}

@Override
public Attributes attributes() {
return attributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected DefaultField(
}

@Override
public @NotNull PresentationField withToStringFormat(@NotNull FieldVisitor fieldVisitor) {
public @NotNull DefaultField withToStringFormat(@NotNull FieldVisitor fieldVisitor) {
return this.withAttribute(PresentationHintAttributes.withToStringFormat(fieldVisitor));
}

Expand Down
Loading

0 comments on commit 58d7803

Please sign in to comment.