Skip to content

Commit

Permalink
Implementation of ConceptEnumerationFacade, and related changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kec committed Oct 2, 2024
1 parent 8eb40f5 commit d492224
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* <p>
* T is the class this is a key for, to help code comprehension
*/
public class PublicIdStringKey<T> implements Comparable<PublicIdStringKey>, Encodable {
public class PublicIdStringKey<T> implements PublicIdWithString<PublicIdStringKey> {

final PublicId publicId;
String string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2015 Integrated Knowledge Management ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.ikm.tinkar.common.id;

import dev.ikm.tinkar.common.binary.Encodable;

/**
* An interface that can be implemented by an enum, and can also be made compatible
* with a future concept binding that may exist in a starter set or elsewhere.
* @param <T>
*/
public interface PublicIdWithString<T> extends Comparable<PublicIdStringKey>, Encodable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
public interface ConceptEntity<V extends ConceptEntityVersion>
extends Entity<V>,
ConceptChronology<V>,
ConceptFacade,
IdentifierData {
ConceptFacade {

@Override
ImmutableList<V> versions();
Expand Down
14 changes: 14 additions & 0 deletions entity/src/main/java/dev/ikm/tinkar/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ default T getVersionFast(int stampNid) {
return null;
}

default Optional<T> getVersion(PublicId stampId) {
return Optional.ofNullable(getVersionFast(stampId));
}

default T getVersionFast(PublicId stampId) {
int stampNid = nid(stampId);
for (T version : versions()) {
if (version.stampNid() == stampNid) {
return version;
}
}
return null;
}

default IntIdSet stampNids() {
MutableIntList stampNids = IntLists.mutable.withInitialCapacity(versions().size());
for (EntityVersion version : versions()) {
Expand Down
4 changes: 4 additions & 0 deletions entity/src/main/java/dev/ikm/tinkar/entity/EntityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ default int nidForUuids(UUID... uuids) {
default <T extends Entity<V>, V extends EntityVersion> Optional<T> getEntity(Component component) {
return getEntity(nidForPublicId(component.publicId()));
}
default <T extends Entity<V>, V extends EntityVersion> Optional<T> getEntity(PublicId publicId) {
return getEntity(nidForPublicId(publicId));
}

default <T extends Entity<V>, V extends EntityVersion> Optional<T> getEntity(int nid) {
T entity = getEntityFast(nid);
Expand All @@ -119,6 +122,7 @@ default <T extends Entity<V>, V extends EntityVersion> Optional<T> getEntity(Imm
return getEntity(nidForUuids(uuidList));
}


default int nidForUuids(ImmutableList<UUID> uuidList) {
return nidForPublicId(PublicIds.of(uuidList.toArray(new UUID[uuidList.size()])));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.ikm.tinkar.entity;

import dev.ikm.tinkar.common.id.PublicId;
import dev.ikm.tinkar.common.id.PublicIds;

import java.util.UUID;
import java.util.function.LongConsumer;
Expand Down Expand Up @@ -60,6 +61,6 @@ default void forEach(LongConsumer consumer) {
}

default PublicId publicId() {
return this;
return PublicIds.of(asUuidArray());
}
}
5 changes: 5 additions & 0 deletions entity/src/main/java/dev/ikm/tinkar/entity/PatternRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.ikm.tinkar.entity;

import dev.ikm.tinkar.common.id.PublicId;
import dev.ikm.tinkar.common.id.PublicIds;
import dev.ikm.tinkar.common.util.Validator;
import io.soabase.recordbuilder.core.RecordBuilder;
import org.eclipse.collections.api.list.ImmutableList;
Expand Down Expand Up @@ -98,4 +99,8 @@ public PatternAnalogueBuilder analogueBuilder() {
public PatternAnalogueBuilder without(PatternEntityVersion versionToAdd) {
return analogueBuilder().remove(versionToAdd);
}

public PublicId publicId() {
return PublicIds.of(asUuidArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.ikm.tinkar.entity;

import dev.ikm.tinkar.common.id.PublicId;
import dev.ikm.tinkar.common.id.PublicIds;
import dev.ikm.tinkar.common.service.PrimitiveData;
import dev.ikm.tinkar.common.util.Validator;
import dev.ikm.tinkar.terms.PatternFacade;
Expand Down Expand Up @@ -141,4 +142,8 @@ public SemanticAnalogueBuilder without(SemanticVersionRecord versionToAdd) {
return analogueBuilder().remove(versionToAdd);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static dev.ikm.tinkar.common.util.time.DateTimeUtil.SEC_FORMATTER;

public interface StampEntity<V extends StampEntityVersion> extends Entity<V>,
Stamp<V>, Component, Version, IdentifierData {
Stamp<V>, Component, Version {
@Override
default State state() {
return lastVersion().state();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public record StampRecord(
long mostSignificantBits, long leastSignificantBits,
long[] additionalUuidLongs, int nid,
ImmutableList<StampVersionRecord> versions)
implements StampEntity<StampVersionRecord>, ImmutableEntity<StampVersionRecord>, StampRecordBuilder.With {
implements StampEntity<StampVersionRecord>, ImmutableEntity<StampVersionRecord>, IdentifierData, StampRecordBuilder.With {

private static StampRecord nonExistentStamp;
public StampRecord {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright © 2015 Integrated Knowledge Management ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.ikm.tinkar.terms;

import dev.ikm.tinkar.common.binary.DecoderInput;
import dev.ikm.tinkar.common.binary.Encodable;
import dev.ikm.tinkar.common.binary.EncoderOutput;
import dev.ikm.tinkar.common.id.PublicId;
import dev.ikm.tinkar.common.id.PublicIds;
import dev.ikm.tinkar.common.service.PrimitiveData;
import dev.ikm.tinkar.common.util.uuid.UuidUtil;

public interface ConceptEnumerationFacade<E extends Enum<E>>
extends ConceptFacade, Encodable {
ConceptFacade conceptForEnum();

String name();

default PublicId publicId() {
return this.conceptForEnum().publicId();
}

default int nid() {
return PrimitiveData.nid(this.conceptForEnum().publicId());
}

default E enumValue() {
return (E) this;
}

static <E extends Enum<E>> E decode(DecoderInput in, Class<E> enumClass) {
switch (Encodable.checkVersion(in)) {
default:
String encodedName = in.readString();
PublicId encodedId = PublicIds.of(UuidUtil.fromString(in.readString()));
ConceptEnumerationFacade<E> enumElement = (ConceptEnumerationFacade<E>) Enum.valueOf(enumClass, encodedName);
if (enumElement.conceptForEnum().publicId().equals(encodedId)) {
return enumElement.enumValue();
}
throw new IllegalStateException("Unknown enum concept " + encodedName + "with id " + encodedId);
}
}

@Override
default void encode(EncoderOutput out) {
out.writeString(this.name());
out.writeString(UuidUtil.toString(this.conceptForEnum().publicId()));
}

}
63 changes: 36 additions & 27 deletions terms/src/main/java/dev/ikm/tinkar/terms/EntityProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package dev.ikm.tinkar.terms;

import dev.ikm.tinkar.common.id.PublicId;
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.list.ImmutableList;

import java.util.Arrays;
import java.util.UUID;
Expand Down Expand Up @@ -67,9 +69,8 @@ public static EntityProxy make(String description, UUID[] uuids) {
return new EntityProxy(description, uuids);
}

@Override
public final PublicId publicId() {
return this;
public PublicId publicId() {
return PublicIds.of(uuids);
}

@Override
Expand All @@ -85,14 +86,14 @@ public boolean equals(Object o) {
return Arrays.equals(this.uuids, other.uuids);
}
}
if (o instanceof ComponentWithNid) {
return this.nid() == ((ComponentWithNid) o).nid();
if (o instanceof ComponentWithNid componentWithNid) {
return this.nid() == componentWithNid.nid();
}
if (o instanceof PublicId) {
return PublicId.equals(this, (PublicId) o);
if (o instanceof PublicId publicId) {
return PublicId.equals(this.publicId(), publicId);
}
if (o instanceof Component) {
return PublicId.equals(this, ((Component) o).publicId());
if (o instanceof Component component) {
return PublicId.equals(this.publicId(), component.publicId());
}
return false;
}
Expand All @@ -101,7 +102,7 @@ public boolean equals(Object o) {
public String toString() {
return this.getClass().getSimpleName() + "{"
+ description() +
" " + Arrays.toString(asUuidArray()) +
" " + Arrays.toString(uuids) +
"<" + cachedNid +
">}";
}
Expand All @@ -114,36 +115,44 @@ public final String description() {
}

@Override
public UUID[] asUuidArray() {
if (this.uuids == null) {
this.uuids = PrimitiveData.publicId(nid()).asUuidArray();
public final int nid() {
if (cachedNid == 0) {
cachedNid = PrimitiveData.get().nidForUuids(uuids);
}
return this.uuids;
return cachedNid;
}

@Override
public int uuidCount() {
return asUuidArray().length;
public ImmutableList<UUID> asUuidList() {
return PublicId.super.asUuidList();
}

@Override
public void forEach(LongConsumer consumer) {
for (UUID uuid : asUuidArray()) {
consumer.accept(uuid.getMostSignificantBits());
consumer.accept(uuid.getLeastSignificantBits());
}
public UUID[] asUuidArray() {
return publicId().asUuidArray();
}

@Override
public int compareTo(PublicId o) {
return publicId().compareTo(o);
}

@Override
public final int nid() {
if (cachedNid == 0) {
cachedNid = PrimitiveData.get().nidForUuids(uuids);
}
return cachedNid;
public boolean contains(UUID uuid) {
return publicId().contains(uuid);
}

@Override
public int uuidCount() {
return publicId().uuidCount();
}

@Override
public void forEach(LongConsumer consumer) {
publicId().forEach(consumer);
}

public static class Concept extends EntityProxy implements ConceptFacade, PublicId {
public static class Concept extends EntityProxy implements ConceptFacade {


private Concept(int conceptNid) {
Expand Down

0 comments on commit d492224

Please sign in to comment.