Skip to content

Commit

Permalink
Add presentation attributes to value, make API match field attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
wsargent committed Apr 16, 2024
1 parent dabcadc commit 7f31a84
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
62 changes: 51 additions & 11 deletions api/src/main/java/com/tersesystems/echopraxia/api/Value.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tersesystems.echopraxia.api;

import com.tersesystems.echopraxia.spi.EchopraxiaService;
import com.tersesystems.echopraxia.spi.PresentationHintAttributes;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
Expand Down Expand Up @@ -52,13 +53,49 @@ public Attributes attributes() {
return Attributes.empty();
}

public <A> @NotNull Value<V> withAttribute(@NotNull Attribute<A> attr) {
return newAttributes(attributes().plus(attr));
}

public @NotNull Value<V> withAttributes(@NotNull Attributes attrs) {
return newAttributes(attributes().plusAll(attrs));
}

public <A> @NotNull Value<V> withoutAttribute(@NotNull AttributeKey<A> key) {
return newAttributes(attributes().minus(key));
}

public @NotNull Value<V> withoutAttributes(@NotNull Collection<AttributeKey<?>> keys) {
return newAttributes(attributes().minusAll(keys));
}

public @NotNull Value<V> clearAttributes() {
return newAttributes(Attributes.empty());
}

protected abstract @NotNull Value<V> newAttributes(@NotNull Attributes attrs);

/**
* Sets the attributes to newAttributes, replacing the original value.
* Tells the formatter that the value should be represented as a cardinal number in text.
*
* @param newAttributes the new attributes.
* @return the value with attributes.
* @return field with cardinal attribute set
*/
public abstract Value<V> withAttributes(Attributes newAttributes);
@NotNull
public Value<V> asCardinal() {
return newAttributes(attributes().plus(PresentationHintAttributes.asCardinal()));
}

/**
* Tells the formatter that the string value or array value should be abbreviated after the given
* number of elements.
*
* @param after the maximum number of elements to render
* @return the field with the attribute applied
*/
@NotNull
public Value<V> abbreviateAfter(int after) {
return withAttributes(attributes().plus(PresentationHintAttributes.abbreviateAfter(after)));
}

/**
* @return this value as an object value.
Expand Down Expand Up @@ -536,7 +573,7 @@ private BooleanValue(Boolean bool) {
}

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

Expand Down Expand Up @@ -579,7 +616,7 @@ public N raw() {
}

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

Expand Down Expand Up @@ -740,7 +777,7 @@ public String raw() {
}

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

Expand Down Expand Up @@ -797,8 +834,9 @@ private ArrayValue(List<Value<?>> raw) {
return Type.ARRAY;
}

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

Expand Down Expand Up @@ -870,7 +908,7 @@ private ObjectValue(List<Field> raw) {
}

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

Expand Down Expand Up @@ -946,8 +984,9 @@ public Void raw() {
return Type.NULL;
}

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

Expand Down Expand Up @@ -979,8 +1018,9 @@ private ExceptionValue(Throwable raw) {
return Type.EXCEPTION;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ protected DefaultField(
@Override
public @NotNull DefaultField abbreviateAfter(int after) {
// Set these on the value so the behavior is consistent
Value<?> newValue =
value.withAttributes(
value.attributes().plus(PresentationHintAttributes.abbreviateAfter(after)));
return new DefaultField(name, newValue, attributes);
return new DefaultField(name, value.abbreviateAfter(after), attributes);
}

@Deprecated
@Override
public @NotNull DefaultField asCardinal() {
// Set these on the value so the behavior is consistent
Value<?> newValue =
value.withAttributes(value.attributes().plus(PresentationHintAttributes.asCardinal()));
return new DefaultField(name, newValue, attributes);
return new DefaultField(name, value.asCardinal(), attributes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface PresentationHints<F extends Field> {
F asValueOnly();

/**
* Tells the formatter that the array value should be represented as a cardinal number in text.
* Tells the formatter that the value should be represented as a cardinal number in text.
*
* @return field with cardinal attribute set
* @deprecated asCardinal attribute only applies to values, this will not set an attribute on the
Expand All @@ -32,8 +32,7 @@ public interface PresentationHints<F extends Field> {
F asCardinal();

/**
* Tells the formatter that the string value or array value should be abbreviated after the given
* number of elements.
* Tells the formatter that the value should be abbreviated after the given number of elements.
*
* @deprecated abbreviateAfter attribute only applies to values, this will not set an attribute on
* the field.
Expand Down

0 comments on commit 7f31a84

Please sign in to comment.