Skip to content

Commit

Permalink
use JDK17 record for hosh.spi.Record.Entry
Browse files Browse the repository at this point in the history
  • Loading branch information
dfa1 committed Dec 23, 2023
1 parent 72131c1 commit f8ffff4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 72 deletions.
4 changes: 2 additions & 2 deletions modules/text/src/main/java/hosh/modules/text/TextModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ private Record trimByKey(Record record, Key key) {
Iterator<Entry> entries = record.entries().iterator();
while (entries.hasNext()) {
Entry entry = entries.next();
if (entry.getKey().equals(key)) {
builder = builder.entry(key, trim(entry.getValue()));
if (entry.key().equals(key)) {
builder = builder.entry(key, trim(entry.value()));
} else {
builder = builder.entry(entry);
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/main/java/hosh/runtime/AutoTableChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ private Map<Key, Integer> calculatePaddings(Collection<Record> records) {
Iterator<Record.Entry> entries = record.entries().iterator();
while (entries.hasNext()) {
Record.Entry entry = entries.next();
String formattedValue = entry.getValue().show(Locale.getDefault());
String formattedValue = entry.value().show(Locale.getDefault());
int valueLength = lengthFor(formattedValue);
maxLengthPerColumn.compute(entry.getKey(), (k, v) -> v == null ? Math.max(k.name().length(), valueLength) : Math.max(v, valueLength));
maxLengthPerColumn.compute(entry.key(), (k, v) -> v == null ? Math.max(k.name().length(), valueLength) : Math.max(v, valueLength));
}
}
Map<Key, Integer> result = new HashMap<>();
Expand All @@ -146,8 +146,8 @@ private void sendRow(Map<Key, Integer> paddings, Record record, OutputChannel ou
List<String> formattedValues = new ArrayList<>(record.size());
while (entries.hasNext()) {
Record.Entry entry = entries.next();
formatter.append(formatterFor(paddings.get(entry.getKey())));
formattedValues.add(entry.getValue().show(locale));
formatter.append(formatterFor(paddings.get(entry.key())));
formattedValues.add(entry.value().show(locale));
}
String row = String.format(formatter.toString(), formattedValues.toArray());
out.send(Records.singleton(Keys.TEXT, Values.ofText(row)));
Expand Down
80 changes: 27 additions & 53 deletions spi/src/main/java/hosh/spi/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,66 +34,40 @@
*/
public interface Record {

/**
* Yields a new Record with the specified mapping as last one.
*/
Record append(Key key, Value value);
/**
* Yields a new Record with the specified mapping as last one.
*/
Record append(Key key, Value value);

/**
* Yields a new Record with the specified mapping as first one.
*/
Record prepend(Key key, Value value);
/**
* Yields a new Record with the specified mapping as first one.
*/
Record prepend(Key key, Value value);

Stream<Key> keys();
Stream<Key> keys();

Stream<Value> values();
Stream<Value> values();

Stream<Entry> entries();
Stream<Entry> entries();

Optional<Value> value(Key key);
Optional<Value> value(Key key);

int size();
int size();

/**
* An immutable value object representing a key/value pair.
* Key and value cannot be null.
*/
class Entry {
/**
* An immutable value object representing a key/value pair.
* Key and value cannot be null.
*/
record Entry(Key key, Value value) {

private final Key key;
public Entry {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
}

private final Value value;

public Entry(Key key, Value value) {
this.key = Objects.requireNonNull(key);
this.value = Objects.requireNonNull(value);
}

public Key getKey() {
return key;
}

public Value getValue() {
return value;
}

@Override
public final boolean equals(Object obj) {
if (obj instanceof Entry that) {
return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value);
} else {
return false;
}
}

@Override
public final int hashCode() {
return Objects.hash(key, value);
}

@Override
public String toString() {
return String.format("Entry[key=%s,value=%s]", key, value);
}
}
@Override
public String toString() {
return String.format("Entry[key=%s,value=%s]", key, value);
}
}
}
20 changes: 10 additions & 10 deletions spi/src/main/java/hosh/spi/Records.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ public Singleton(Entry entry) {

@Override
public Stream<Key> keys() {
return Stream.of(entry).map(Entry::getKey);
return Stream.of(entry).map(Entry::key);
}

@Override
public Stream<Value> values() {
return Stream.of(entry).map(Entry::getValue);
return Stream.of(entry).map(Entry::value);
}

@Override
Expand All @@ -176,8 +176,8 @@ public Record prepend(Key newKey, Value newValue) {

@Override
public Optional<Value> value(Key wantedKey) {
if (Objects.equals(this.entry.getKey(), wantedKey)) {
return Optional.of(this.entry.getValue());
if (Objects.equals(this.entry.key(), wantedKey)) {
return Optional.of(this.entry.value());
} else {
return Optional.empty();
}
Expand All @@ -204,7 +204,7 @@ public final boolean equals(Object obj) {

@Override
public String toString() {
return String.format("Record[data={%s=%s}]", entry.getKey(), entry.getValue());
return String.format("Record[data={%s=%s}]", entry.key(), entry.value());
}

}
Expand Down Expand Up @@ -236,14 +236,14 @@ public Record prepend(Key key, Value value) {
public Stream<Key> keys() {
return Arrays
.stream(entries)
.map(Entry::getKey);
.map(Entry::key);
}

@Override
public Stream<Value> values() {
return Arrays
.stream(entries)
.map(Entry::getValue);
.map(Entry::value);
}

@Override
Expand All @@ -254,8 +254,8 @@ public Stream<Entry> entries() {
@Override
public Optional<Value> value(Key key) {
for (Entry entry : entries) {
if (entry.getKey().equals(key)) {
return Optional.of(entry.getValue());
if (entry.key().equals(key)) {
return Optional.of(entry.value());
}
}
return Optional.empty();
Expand Down Expand Up @@ -285,7 +285,7 @@ public String toString() {
return String.format("Record[data={%s}]",
Arrays
.stream(entries)
.map(e -> String.format("%s=%s", e.getKey(), e.getValue()))
.map(e -> String.format("%s=%s", e.key(), e.value()))
.collect(Collectors.joining(",")));
}

Expand Down
6 changes: 3 additions & 3 deletions spi/src/test/java/hosh/spi/RecordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void equalsContract() {
EqualsVerifier.forClass(Records.Empty.class).verify();
EqualsVerifier.forClass(Records.Singleton.class).withNonnullFields("entry").verify();
EqualsVerifier.forClass(Records.Generic.class).withNonnullFields("entries").verify();
EqualsVerifier.forClass(Record.Entry.class).verify();
EqualsVerifier.forClass(Record.Entry.class).withNonnullFields("key", "value").verify();
}

@Test
Expand Down Expand Up @@ -155,8 +155,8 @@ void generic() {
void entry() {
Record.Entry entry = new Record.Entry(Keys.NAME, Values.none());
assertThat(entry).hasToString("Entry[key=Key['name'],value=None]");
assertThat(entry.getKey()).isEqualTo(Keys.NAME);
assertThat(entry.getValue()).isEqualTo(Values.none());
assertThat(entry.key()).isEqualTo(Keys.NAME);
assertThat(entry.value()).isEqualTo(Values.none());
}

@Test
Expand Down

0 comments on commit f8ffff4

Please sign in to comment.