Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package it.aboutbits.springboot.toolbox.util;

import it.aboutbits.springboot.toolbox.type.identity.EntityId;
import it.aboutbits.springboot.toolbox.type.identity.Identified;
import org.jspecify.annotations.NullMarked;
import org.springframework.data.util.Streamable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@NullMarked
public class MapByIdentity<ID extends EntityId<?>, E extends Identified<ID>> extends HashMap<ID, E> {
public MapByIdentity(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}

public MapByIdentity(int initialCapacity) {
super(initialCapacity);
}

public MapByIdentity() {
super();
}

public MapByIdentity(Map<? extends ID, ? extends E> m) {
super(m);
}

public static <ID extends EntityId<?>, E extends Identified<ID>> MapByIdentity<ID, E> of(Collection<? extends E> items) {
var map = new MapByIdentity<ID, E>(items.size());
items.forEach(item -> map.put(item.getId(), item));
return map;
}

public static <ID extends EntityId<?>, E extends Identified<ID>> MapByIdentity<ID, E> of(Stream<? extends E> items) {
var collected = items.collect(Collectors.toMap(
Identified::getId,
Function.identity()
));

return new MapByIdentity<>(collected);
}

public static <ID extends EntityId<?>, E extends Identified<ID>> MapByIdentity<ID, E> of(Streamable<? extends E> items) {
return of(items.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package it.aboutbits.springboot.toolbox.util;

import it.aboutbits.springboot.toolbox.type.identity.EntityId;
import it.aboutbits.springboot.toolbox.type.identity.Identified;
import lombok.Getter;
import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.data.util.Streamable;

import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

@NullMarked
class MapByIdentityTest {
@Nested
class Of {
@Test
void collection() {
// given
var id1 = new TestEntity.ID(1L);
var id2 = new TestEntity.ID(2L);

var entity1 = new TestEntity(id1);
var entity2 = new TestEntity(id2);

var entities = List.of(entity1, entity2);

// when
var result = MapByIdentity.of(entities);

// then
assertThat(result).hasSize(2)
.containsEntry(id1, entity1)
.containsEntry(id2, entity2);
}

@Test
void stream() {
// given
var id1 = new TestEntity.ID(1L);
var id2 = new TestEntity.ID(2L);

var entity1 = new TestEntity(id1);
var entity2 = new TestEntity(id2);

var entityStream = Stream.of(entity1, entity2);

// when
var result = MapByIdentity.of(entityStream);

// then
assertThat(result).hasSize(2)
.containsEntry(id1, entity1)
.containsEntry(id2, entity2);
}

@Test
void streamable() {
// given
var id1 = new TestEntity.ID(1L);
var id2 = new TestEntity.ID(2L);

var entity1 = new TestEntity(id1);
var entity2 = new TestEntity(id2);

var streamable = Streamable.of(entity1, entity2);

// when
var result = MapByIdentity.of(streamable);

// then
assertThat(result).hasSize(2)
.containsEntry(id1, entity1)
.containsEntry(id2, entity2);
}

@Test
void emptyCollection() {
// given
var entities = List.<TestEntity>of();

// when
var result = MapByIdentity.of(entities);

// then
assertThat(result).isEmpty();
}

@Test
void emptyStream() {
// given
var emptyStream = Stream.<TestEntity>empty();

// when
var result = MapByIdentity.of(emptyStream);

// then
assertThat(result).isEmpty();
}

@Test
void emptyStreamable() {
// given
var emptyStreamable = Streamable.<TestEntity>empty();

// when
var result = MapByIdentity.of(emptyStreamable);

// then
assertThat(result).isEmpty();
}
}

@Getter
private static class TestEntity implements Identified<TestEntity.ID> {
private final ID id;

TestEntity(ID id) {
this.id = id;
}

public record ID(
Long value
) implements EntityId<Long>, Comparable<ID> {
@Override
public String toString() {
return String.valueOf(value());
}

@Override
public int compareTo(ID other) {
return Long.compare(this.value, other.value);
}
}
}
}