angry1980.audio.dao.DAO Maven / Gradle / Ivy
package angry1980.audio.dao;
import com.google.common.collect.ImmutableList;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
public interface DAO {
default Optional get(long id){
return Optional.ofNullable(tryToGet(id));
}
//to support java versions less then 8
T tryToGet(long id);
//todo: use paging
default Collection getAllOrEmpty(){
return getAll().orElseGet(ImmutableList::of);
}
default Optional> getAll(){
return Optional.ofNullable(tryToGetAll());
}
//to support java versions less then 8
Collection tryToGetAll();
default Optional create(T entity){
return Optional.of(tryToCreate(entity));
}
default Optional> createAll(Collection entities){
return Optional.ofNullable(entities)
.map(this::tryToCreateAll);
}
//to support java versions less then 8
default Collection tryToCreateAll(Collection entities){
return entities.stream()
.map(this::tryToCreate)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
//to support java versions less then 8
T tryToCreate(T entity);
}