Skip to content

Instantly share code, notes, and snippets.

@manuel-mauky
Last active August 29, 2015 14:21
Show Gist options
  • Save manuel-mauky/00d0ef995c659d2b6d71 to your computer and use it in GitHub Desktop.
Save manuel-mauky/00d0ef995c659d2b6d71 to your computer and use it in GitHub Desktop.
import java.util.Objects;
import java.util.UUID;
public abstract class Identity {
private final String id;
public Identity() {
this.id = UUID.randomUUID().toString();
}
public String getId() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Identity identity = (Identity) o;
return Objects.equals(id, identity.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "[id=" + id.substring(0,8) + "...]";
}
}
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public abstract class InMemoryRepository<T extends Identity> implements Repository<T> {
private Set<T> entities = new HashSet<>();
@Override
public Set<T> get() {
return Collections.unmodifiableSet(entities);
}
@Override
public void persist(T entity) {
entities.add(entity);
}
@Override
public void remove(T entity) {
entities.remove(entity);
}
}
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public interface Repository<T extends Identity> {
Set<T> get();
default Set<T> get(Predicate<T> predicate) {
return get()
.stream()
.filter(predicate)
.collect(Collectors.toSet());
}
default Optional<T> get(String id) {
return get()
.stream()
.filter(entity -> entity.getId().equals(id))
.findAny();
}
void persist(T entity);
default void persist(T ... entities) {
persist(Arrays.asList(entities));
}
default void persist(Collection<T> entities) {
entities.forEach(this::persist);
}
void remove(T entity);
default void remove(String id) {
remove(entity -> entity.getId().equals(id));
}
default void remove(T ... entities) {
remove(Arrays.asList(entities));
}
default void remove(Collection<T> entities) {
entities.forEach(this::remove);
}
default void remove(Predicate<T> predicate) {
get(predicate).forEach(this::remove);
}
}
public class TestEntity extends Identity {
private String value;
public TestEntity(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class TestEntityRepository extends InMemoryRepository<TestEntity> {
}
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
public class TestEntityRepositoryTest {
Repository<TestEntity> repository;
TestEntity entity1;
TestEntity entity2;
TestEntity entity3;
@Before
public void setup() {
repository = new TestEntityRepository();
entity1 = new TestEntity("test 1");
entity2 = new TestEntity("test 2");
entity3 = new TestEntity("test 3");
}
@Test
public void testPersist() {
assertThat(repository.get()).isEmpty();
repository.persist(entity1);
assertThat(repository.get()).contains(entity1);
repository.persist(entity1, entity2);
assertThat(repository.get()).containsOnly(entity1, entity2);
List<TestEntity> entityList = new ArrayList<>();
entityList.add(entity3);
repository.persist(entityList);
assertThat(repository.get()).containsOnly(entity1, entity2, entity3);
}
@Test
public void testRemove() {
repository.persist(entity1, entity2, entity3);
repository.remove(entity1);
assertThat(repository.get()).doesNotContain(entity1);
repository.remove(entity2, entity3);
assertThat(repository.get()).isEmpty();
repository.persist(entity1, entity2, entity3);
List<TestEntity> entityList = new ArrayList<>();
entityList.add(entity3);
repository.remove(entityList);
assertThat(repository.get()).doesNotContain(entity3);
repository.remove(item -> item.getValue().endsWith("2"));
assertThat(repository.get()).doesNotContain(entity2).contains(entity1);
repository.remove(entity1.getId());
assertThat(repository.get()).isEmpty();
}
@Test
public void testGet() {
repository.persist(entity1, entity2, entity3);
assertThat(repository.get()).contains(entity1, entity2, entity3);
final Optional<TestEntity> entityOptional = repository.get(entity2.getId());
assertThat(entityOptional.isPresent()).isTrue();
assertThat(entityOptional.get()).isEqualTo(entity2);
final Optional<TestEntity> entityOptional2 = repository.get("some other id");
assertThat(entityOptional2.isPresent()).isFalse();
final Set<TestEntity> entities = repository.get(item -> item.getValue().startsWith("test"));
assertThat(entities).contains(entity1, entity2, entity3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment