Skip to content

Commit

Permalink
Merge pull request #32 from vlingo/metadata-properties
Browse files Browse the repository at this point in the history
Replace Object metadata with a Map of metadata properties
  • Loading branch information
jakzal authored Aug 10, 2022
2 parents 8f94f0d + b9364e8 commit 14ec2e7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
62 changes: 56 additions & 6 deletions src/main/java/io/vlingo/xoom/symbio/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,35 @@

package io.vlingo.xoom.symbio;

import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;

public class Metadata implements Comparable<Metadata> {
@Deprecated
public final static Object EmptyObject = new Object() { @Override public String toString() { return "(empty)"; } };

// Object is deprecated and will be removed in future versions. Use the Map of properties instead.
@Deprecated
public final Object object;
public final Map<String, String> properties;
public final String operation;
public final String value;

public static Metadata nullMetadata() {
return new Metadata(EmptyObject, "", "");
return new Metadata(Collections.emptyMap(), "", "");
}

@Deprecated
public static Metadata withObject(final Object object) {
return new Metadata(object, "", "");
}

public static Metadata withProperties(final Map<String, String> properties) {
return new Metadata(properties, "", "");
}

public static Metadata withOperation(final String operation) {
return new Metadata(EmptyObject, "", operation);
}
Expand All @@ -34,42 +45,76 @@ public static Metadata withValue(final String value) {
}

public static Metadata with(final String value, final String operation) {
return new Metadata(EmptyObject, value, operation);
return new Metadata(value, operation);
}

public static Metadata with(final Map<String, String> properties, final String value, final String operation) {
return new Metadata(properties, value, operation);
}

public static Metadata with(final Map<String, String> properties, final String value, final Class<?> operationType) {
return with(properties, value, operationType, true);
}

public static Metadata with(final Map<String, String> properties, final String value, final Class<?> operationType, final boolean compact) {
final String operation = compact ? operationType.getSimpleName() : operationType.getName();
return new Metadata(properties, value, operation);
}

@Deprecated
public static Metadata with(final Object object, final String value, final String operation) {
return new Metadata(object, value, operation);
}

@Deprecated
public static Metadata with(final Object object, final String value, final Class<?> operationType) {
return with(object, value, operationType, true);
}

@Deprecated
public static Metadata with(final Object object, final String value, final Class<?> operationType, final boolean compact) {
final String operation = compact ? operationType.getSimpleName() : operationType.getName();
return new Metadata(object, value, operation);
}

@Deprecated
public Metadata(final Object object, final String value, final String operation) {
if (object == null) this.object = EmptyObject; else this.object = object;

if (value == null) this.value = ""; else this.value = value;

if (operation == null) this.operation = ""; else this.operation = operation;

this.properties = Collections.emptyMap();
}

public Metadata(final Map<String, String> properties, final String value, final String operation) {
this.object = EmptyObject;

if (properties == null) this.properties = Collections.emptyMap(); else this.properties = properties;

if (value == null) this.value = ""; else this.value = value;

if (operation == null) this.operation = ""; else this.operation = operation;
}

public Metadata(final String value, final String operation) {
this(EmptyObject, value, operation);
this(Collections.emptyMap(), value, operation);
}

public Metadata() {
this(EmptyObject, "", "");
this(Collections.emptyMap(), "", "");
}

@Deprecated
public boolean hasObject() {
return object != EmptyObject;
}

public boolean hasProperties() {
return !properties.isEmpty();
}

public boolean hasOperation() {
return !operation.isEmpty();
}
Expand All @@ -82,10 +127,12 @@ public boolean isEmpty() {
return !hasOperation() && !hasValue();
}

@Deprecated
public Object object() {
return object;
}

@Deprecated
public Optional<Object> optionalObject() {
return hasObject() ? Optional.of(object) : Optional.empty();
}
Expand All @@ -98,6 +145,7 @@ public String value() {
return value;
}

@Deprecated
@SuppressWarnings("unchecked")
public <T> T typedObject() {
return (T) object;
Expand All @@ -106,6 +154,7 @@ public <T> T typedObject() {
@Override
public int compareTo(final Metadata other) {
if (!this.object.equals(other.object)) return 1;
if (!this.properties.equals(other.properties)) return 1;
return Comparator
.comparing((Metadata m) -> m.value)
.thenComparing(m -> m.operation)
Expand All @@ -114,7 +163,7 @@ public int compareTo(final Metadata other) {

@Override
public int hashCode() {
return 31 * value.hashCode() + operation.hashCode() + object.hashCode();
return 31 * value.hashCode() + operation.hashCode() + properties.hashCode() + object.hashCode();
}

@Override
Expand All @@ -127,12 +176,13 @@ public boolean equals(final Object other) {

return value.equals(otherMetadata.value) &&
operation.equals(otherMetadata.operation) &&
properties.equals(otherMetadata.properties) &&
object.equals(otherMetadata.object);
}

@Override
public String toString() {
return getClass().getSimpleName() +
"[value=" + value + " operation=" + operation + " object=" + object + "]";
"[value=" + value + " operation=" + operation + " properties=" + properties + " object=" + object + "]";
}
}
66 changes: 66 additions & 0 deletions src/test/java/io/vlingo/xoom/symbio/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@

import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MetadataTest {

@Test
public void testMetadataEmpty() {
final Metadata metadata = new Metadata();
assertFalse(metadata.hasValue());
assertFalse(metadata.hasOperation());
assertFalse(metadata.hasProperties());
}

@Test
public void testNullMetadata() {
final Metadata metadata = Metadata.nullMetadata();
assertFalse(metadata.hasValue());
assertFalse(metadata.hasOperation());
assertFalse(metadata.hasProperties());
}

@Test
@Deprecated
public void testMetadataObject() {
final Object object = new Object();
final Metadata metadata = Metadata.withObject(object);
Expand All @@ -32,6 +46,19 @@ public void testMetadataObject() {
assertFalse(metadata.hasOperation());
}

@Test
public void testMetadataProperties() {
final Map<String, String> properties = new HashMap<String, String>() {{
put("prop1", "value1");
put("prop2", "value2");
}};
final Metadata metadata = Metadata.withProperties(properties);
assertTrue(metadata.hasProperties());
assertEquals(properties, metadata.properties);
assertFalse(metadata.hasValue());
assertFalse(metadata.hasOperation());
}

@Test
public void testMetadataValue() {
final Metadata metadata = Metadata.withValue("value");
Expand All @@ -56,4 +83,43 @@ public void testMetadataValueOperation() {
assertTrue(metadata.hasOperation());
assertEquals("op", metadata.operation);
}

@Test
public void testMetadataPropertiesValueOperation() {
final Map<String, String> properties = new HashMap<String, String>() {{
put("prop1", "value1");
put("prop2", "value2");
}};
final Metadata metadata = Metadata.with(properties, "value", "op");
assertTrue(metadata.hasProperties());
assertEquals(properties, metadata.properties);
assertTrue(metadata.hasValue());
assertEquals("value", metadata.value);
assertTrue(metadata.hasOperation());
assertEquals("op", metadata.operation);
}

@Test
public void testMetadataWithClassOperationType() {
final Map<String, String> properties = Collections.singletonMap("prop1", "value1");
final Metadata metadata = Metadata.with(properties, "value", MetadataTest.class);
assertTrue(metadata.hasProperties());
assertEquals(properties, metadata.properties);
assertTrue(metadata.hasValue());
assertEquals("value", metadata.value);
assertTrue(metadata.hasOperation());
assertEquals("MetadataTest", metadata.operation);
}

@Test
public void testMetadataWithNonCompactClassOperationType() {
final Map<String, String> properties = Collections.singletonMap("prop1", "value1");
final Metadata metadata = Metadata.with(properties, "value", MetadataTest.class, false);
assertTrue(metadata.hasProperties());
assertEquals(properties, metadata.properties);
assertTrue(metadata.hasValue());
assertEquals("value", metadata.value);
assertTrue(metadata.hasOperation());
assertEquals("io.vlingo.xoom.symbio.MetadataTest", metadata.operation);
}
}

0 comments on commit 14ec2e7

Please sign in to comment.