Skip to content

Commit

Permalink
optimize ID marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Nov 1, 2023
1 parent 6629a5b commit 28a69e1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public Instant getRevision() {
return this.revision;
}

@Override
public boolean hasRevision() {

return (this.revision != null);
}

@Override
public Class<Instant> getRevisionType() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public Long getRevision() {
return this.revision;
}

@Override
public boolean hasRevision() {

return (this.revision != null) && (this.revision.longValue() != 0);
}

@Override
public Class<Long> getRevisionType() {

Expand Down
18 changes: 10 additions & 8 deletions core/src/main/java/io/github/mmm/entity/id/GenericId.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public interface GenericId<E, I, R extends Comparable<?>>
@Override
R getRevision();

/**
* @return {@code true} if this {@link Id} has relevant revision information, {@code false} otherwise (revision can be
* omitted because it is {@code null} or zero).
*/
boolean hasRevision();

/**
* @return the {@link Class} reflecting the {@link #getRevision() revision}.
*/
Expand Down Expand Up @@ -181,22 +187,18 @@ default GenericId<E, I, R> updateRevision() {
@Override
default void write(StructuredWriter writer) {

R revision = getRevision();
I id = get();
if (revision == null) {
if (id == null) {
writer.writeValueAsNull();
} else {
writer.writeValue(id);
}
} else {
if (hasRevision()) {
R revision = getRevision();
writer.writeStartObject();
writer.writeName(getMarshalPropertyId());
assert (id != null);
writer.writeValue(id);
writer.writeName(getMarshalPropertyRevision());
writer.writeValue(revision);
writer.writeEnd();
} else {
writer.writeValue(id);
}
}

Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/io/github/mmm/entity/id/IdMarshallingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ public void testAllIdTypes() {
check(new LongVersionId<>(Entity.class, 42L, null), "42");
check(new StringVersionId<>(Entity.class, "MyId", null), "\"MyId\"");
check(new UuidVersionId<>(Entity.class, uuid, null), "\"" + uuid + "\"");
}

/**
* Test of {@link IdMarshalling} to JSON for all {@link AbstractVersionId}s with
* {@link AbstractVersionId#getRevision() revision} of {@code 0}.
*/
@Test
public void testWriteWithZeroVersion() {

UUID uuid = UUID.randomUUID();
assertThat(writeJson(new LongVersionId<>(Entity.class, 42L, 0L))).isEqualTo("42");
assertThat(writeJson(new StringVersionId<>(Entity.class, "MyId", 0L))).isEqualTo("\"MyId\"");
assertThat(writeJson(new UuidVersionId<>(Entity.class, uuid, 0L))).isEqualTo("\"" + uuid + '"');
}

public static void check(GenericId<Entity, ?, ?> id, String json) {
Expand Down

0 comments on commit 28a69e1

Please sign in to comment.