Skip to content

Commit

Permalink
Use records for some things
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jan 2, 2024
1 parent 9904ce3 commit 11c36a7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 84 deletions.
7 changes: 1 addition & 6 deletions bosk-core/src/main/java/io/vena/bosk/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -567,11 +566,7 @@ protected boolean matchesImpl(Path other) {

private static final Interner<InternKey, Path> INTERNER = new Interner<>();

@Value
static class InternKey {
Path prefix;
String segment;

record InternKey(Path prefix, String segment) {
@Override
public String toString() {
return identityHashCode(prefix) + "/" + segment;
Expand Down
10 changes: 4 additions & 6 deletions bosk-core/src/main/java/io/vena/bosk/bytecode/LocalVariable.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.vena.bosk.bytecode;

import lombok.Value;
import org.objectweb.asm.Type;

@Value
public class LocalVariable {
Type type;
int slot;
}
public record LocalVariable(
Type type,
int slot
) { }
67 changes: 31 additions & 36 deletions bosk-gson/src/main/java/io/vena/bosk/gson/GsonAdapterCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ private interface FieldWritePlan {
/**
* The basic, un-optimized, canonical way to write a field.
*/
@Value
private static class OrdinaryFieldWritePlan implements FieldWritePlan {
private record OrdinaryFieldWritePlan() implements FieldWritePlan {
/**
* {@inheritDoc}
*/
Expand All @@ -248,8 +247,7 @@ public void generateFieldWrite(String name, ClassBuilder<Codec> cb, Gson gson, L
* where we can't look up the field's type adapter because we're still in the midst
* of compiling that very adapter itself.
*/
@Value
private static class StaticallyBoundFieldWritePlan implements FieldWritePlan {
private record StaticallyBoundFieldWritePlan() implements FieldWritePlan {
/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -280,14 +278,10 @@ public void generateFieldWrite(String name, ClassBuilder<Codec> cb, Gson gson, L
/**
* A stackable wrapper that writes an <code>{@link Optional}&lt;T&gt;</code> given
* a {@link FieldWritePlan} for <code>T</code>.
*
* @param valueWriter Handles the value inside the {@link Optional}.
*/
@Value
private static class OptionalFieldWritePlan implements FieldWritePlan {
/**
* Handles the value inside the {@link Optional}.
*/
FieldWritePlan valueWriter;

private record OptionalFieldWritePlan(FieldWritePlan valueWriter) implements FieldWritePlan {
/**
* {@inheritDoc}
*/
Expand All @@ -297,7 +291,7 @@ public void generateFieldWrite(String name, ClassBuilder<Codec> cb, Gson gson, L
LocalVariable optional = cb.popToLocal();
cb.pushLocal(optional);
cb.invoke(OPTIONAL_IS_PRESENT);
cb.ifTrue(()-> {
cb.ifTrue(() -> {
// Unwrap
cb.pushLocal(optional);
cb.invoke(OPTIONAL_GET);
Expand All @@ -313,35 +307,36 @@ public void generateFieldWrite(String name, ClassBuilder<Codec> cb, Gson gson, L
* A stackable wrapper to implement {@link DerivedRecord} semantics: writes {@link Entity}
* fields as {@link Reference References}, and all other fields are written using the
* supplied {@link #nonEntityWriter}.
*
* @param nodeClassName Just for error messages
*/
@Value
private static class ReferencingFieldWritePlan implements FieldWritePlan {
FieldWritePlan nonEntityWriter;
String nodeClassName; // Just for error messages

/**
* {@inheritDoc}
*/
@Override
public void generateFieldWrite(String name, ClassBuilder<Codec> cb, Gson gson, LocalVariable jsonWriter, Type type) {
Class<?> parameterClass = rawClass(type);
boolean isEntity = Entity.class.isAssignableFrom(parameterClass);
if (isEntity) {
if (ReflectiveEntity.class.isAssignableFrom(parameterClass)) {
cb.castTo(ReflectiveEntity.class);
cb.invoke(REFLECTIVE_ENTITY_REFERENCE);
// Recurse to write the Reference
generateFieldWrite(name, cb, gson, jsonWriter, parameterizedType(Reference.class, type));
private record ReferencingFieldWritePlan(
FieldWritePlan nonEntityWriter,
String nodeClassName
) implements FieldWritePlan {
/**
* {@inheritDoc}
*/
@Override
public void generateFieldWrite(String name, ClassBuilder<Codec> cb, Gson gson, LocalVariable jsonWriter, Type type) {
Class<?> parameterClass = rawClass(type);
boolean isEntity = Entity.class.isAssignableFrom(parameterClass);
if (isEntity) {
if (ReflectiveEntity.class.isAssignableFrom(parameterClass)) {
cb.castTo(ReflectiveEntity.class);
cb.invoke(REFLECTIVE_ENTITY_REFERENCE);
// Recurse to write the Reference
generateFieldWrite(name, cb, gson, jsonWriter, parameterizedType(Reference.class, type));
} else {
throw new IllegalArgumentException(String.format("%s %s cannot contain Entity that is not a ReflectiveEntity: \"%s\"", DerivedRecord.class.getSimpleName(), nodeClassName, name));
}
} else if (Catalog.class.isAssignableFrom(parameterClass)) {
throw new IllegalArgumentException(String.format("%s %s cannot contain Catalog \"%s\" (try Listing?)", DerivedRecord.class.getSimpleName(), nodeClassName, name));
} else {
throw new IllegalArgumentException(String.format("%s %s cannot contain Entity that is not a ReflectiveEntity: \"%s\"", DerivedRecord.class.getSimpleName(), nodeClassName, name));
nonEntityWriter.generateFieldWrite(name, cb, gson, jsonWriter, type);
}
} else if (Catalog.class.isAssignableFrom(parameterClass)) {
throw new IllegalArgumentException(String.format("%s %s cannot contain Catalog \"%s\" (try Listing?)", DerivedRecord.class.getSimpleName(), nodeClassName, name));
} else {
nonEntityWriter.generateFieldWrite(name, cb, gson, jsonWriter, type);
}
}
}

/**
* Implements the Gson {@link TypeAdapter} interface using a {@link Codec} object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,7 @@ public interface FieldModerator {
*
* @author Patrick Doyle
*/
@Value
private static class StateTreeNodeFieldModerator implements FieldModerator {
Type nodeType;

private record StateTreeNodeFieldModerator(Type nodeType) implements FieldModerator {
@Override
public JavaType typeOf(JavaType parameterType) {
return parameterType;
Expand All @@ -661,10 +658,7 @@ public Object valueFor(JavaType parameterType, Object deserializedValue) {
*
* @author Patrick Doyle
*/
@Value
private static class DerivedRecordFieldModerator implements FieldModerator {
Type nodeType;

private record DerivedRecordFieldModerator(Type nodeType) implements FieldModerator {
@Override
public JavaType typeOf(JavaType parameterType) {
if (reflectiveEntity(parameterType)) {
Expand All @@ -680,7 +674,7 @@ public JavaType typeOf(JavaType parameterType) {
public Object valueFor(JavaType parameterType, Object deserializedValue) {
if (reflectiveEntity(parameterType)) {
// The deserialized value is a Reference; what we want is Reference.value()
return ((Reference<?>)deserializedValue).value();
return ((Reference<?>) deserializedValue).value();
} else {
return deserializedValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.Value;
import lombok.Getter;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonInvalidOperationException;
Expand Down Expand Up @@ -43,10 +43,13 @@
class BsonSurgeon {
final List<GraftPoint> graftPoints;

@Value(staticConstructor = "of")
public static class GraftPoint {
Reference<? extends EnumerableByIdentifier<?>> containerRef;
Reference<?> entryPlaceholderRef;
record GraftPoint (
Reference<? extends EnumerableByIdentifier<?>> containerRef,
Reference<?> entryPlaceholderRef
) {
public static GraftPoint of(Reference<? extends EnumerableByIdentifier<?>> containerRef, Reference<?> entryPlaceholderRef) {
return new GraftPoint(containerRef, entryPlaceholderRef);
}

public GraftPoint boundTo(Identifier id) {
return GraftPoint.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public List<ChangeStreamDocument<BsonDocument>> pop(ChangeStreamDocument<BsonDoc
return transactionsInProgress.remove(TransactionID.from(finalEvent));
}

@Value
private static class TransactionID {
BsonDocument lsid;
BsonInt64 txnNumber;
private record TransactionID(BsonDocument lsid, BsonInt64 txnNumber) {
private TransactionID {
requireNonNull(lsid);
requireNonNull(txnNumber);
}

public static TransactionID from(ChangeStreamDocument<?> event) {
return new TransactionID(requireNonNull(event.getLsid()), requireNonNull(event.getTxnNumber()));
return new TransactionID(event.getLsid(), event.getTxnNumber());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.stream.Stream;
import lombok.Value;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
Expand Down Expand Up @@ -595,14 +594,13 @@ private BsonString rootDocumentID() {
/**
* Represents an earlier version of the entity before some fields were added.
*/
@Value
public static class OldEntity implements Entity {
Identifier id;
String string;
public record OldEntity(
Identifier id,
String string,
// We need catalog and sideTable because we use them in our PandoConfiguration
Catalog<OldEntity> catalog;
SideTable<OldEntity, OldEntity> sideTable;

Catalog<OldEntity> catalog,
SideTable<OldEntity, OldEntity> sideTable
) implements Entity {
public static OldEntity withString(String value, Bosk<OldEntity> bosk) throws InvalidTypeException {
return new OldEntity(
rootID,
Expand All @@ -617,15 +615,15 @@ public static OldEntity withString(String value, Bosk<OldEntity> bosk) throws In
* A version of {@link TestEntity} where the {@link Optional} {@link TestEntity#values()}
* field has a default (and some other fields have been deleted).
*/
@Value
public static class UpgradeableEntity implements Entity {
Identifier id;
String string;
Catalog<TestEntity> catalog;
Listing<TestEntity> listing;
SideTable<TestEntity, TestEntity> sideTable;
Optional<TestValues> values;

public record UpgradeableEntity(
Identifier id,
String string,
Catalog<TestEntity> catalog,
Listing<TestEntity> listing,
SideTable<TestEntity, TestEntity> sideTable,
Optional<TestValues> values
) implements Entity {
@Override
public Optional<TestValues> values() {
return Optional.of(values.orElse(TestValues.blank()));
}
Expand Down

0 comments on commit 11c36a7

Please sign in to comment.