
api.DatabaseConnection Maven / Gradle / Ivy
package api;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* Connects to a database
* @param The type of the schema the database has to fulfill.
* For an SQL database S should be an instance of SQLSchema.
* @param Type of the data that is to be stored in the database.
* @param Type of the predicates for read and update operations.
* @param Type of the predicates for insert operations.
*/
public interface DatabaseConnection {
class Value{
private final String value;
public Value(String value) {
assert value != null;
this.value = value;
}
public String get(){
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Value value1 = (Value) o;
return value.equals(value1.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return "Value{" +
"value='" + value + '\'' +
'}';
}
}
/**
* Builds up the database according to the given schema.
* Data will be deleted, if it conflicts with the schema.
* @param schemas The schema the database shall meet
*/
void init(Collection schemas);
/**
* Inserts a row into the table.
* @param data The data to be inserted
* @param predicates Predicates that have to match before inserting the given data.
* Each time one of the predicates match, the data will be inserted into
* the database. If you want the data to be inserted just once, make sure
* only one predicate matches once. If no predicates are given, no data will
* be inserted.
*/
void insert(D data, Collection> predicates);
/**
* Reads data from the database.
* If one or more predicates are given only data that matches
* the predicates will be returned.
* @param predicates Predicates the data should be matched against.
* If no predicates are given, all data of the database will be returned.
* @return Stream of data that has been found
*/
Stream read(Collection> predicates);
}