Skip to content

Commit

Permalink
Improved lazy initialization of EntityProxy either with a nid, or wit…
Browse files Browse the repository at this point in the history
…h UUIDs and a description.
  • Loading branch information
kec committed Oct 4, 2024
1 parent e3cfac0 commit 7afb4f1
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions terms/src/main/java/dev/ikm/tinkar/terms/EntityProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dev.ikm.tinkar.common.id.PublicIds;
import dev.ikm.tinkar.common.service.PrimitiveData;
import dev.ikm.tinkar.component.Component;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -47,8 +48,6 @@ public class EntityProxy implements EntityFacade, PublicId {
*/
protected EntityProxy(int nid) {
this.cachedNid = nid;
this.uuids = PrimitiveData.publicId(nid).asUuidArray();
this.description = PrimitiveData.text(nid);
}

protected EntityProxy(String description, UUID[] uuids) {
Expand All @@ -62,6 +61,17 @@ protected EntityProxy(String description, PublicId publicId) {
this.description = description;
}

public UUID[] uuids() {
if (uuids == null) {
if (cachedNid == 0) {
throw new IllegalStateException("Nid and UUIDs not initialized");
} else {
uuids = PrimitiveData.publicId(nid()).asUuidArray();
}
}
return uuids;
}

public static EntityProxy make(String description, PublicId publicId) {
return new EntityProxy(description, publicId.asUuidArray());
}
Expand All @@ -75,7 +85,7 @@ public static EntityProxy make(String description, UUID[] uuids) {
}

public PublicId publicId() {
return PublicIds.of(uuids);
return PublicIds.of(uuids());
}

@Override
Expand Down Expand Up @@ -122,14 +132,17 @@ public final String description() {
@Override
public final int nid() {
if (cachedNid == 0) {
if (uuids == null) {
throw new IllegalStateException("Nid and UUIDs not initialized");
}
cachedNid = PrimitiveData.get().nidForUuids(uuids);
}
return cachedNid;
}

@Override
public ImmutableList<UUID> asUuidList() {
return PublicId.super.asUuidList();
return Lists.immutable.of(uuids());
}

@Override
Expand Down

0 comments on commit 7afb4f1

Please sign in to comment.