com.eventsourcing.queries.ModelQueries Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eventsourcing-queries Show documentation
Show all versions of eventsourcing-queries Show documentation
Event capture and querying framework for Java
/**
* Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.eventsourcing.queries;
import com.eventsourcing.Entity;
import com.eventsourcing.EntityHandle;
import com.eventsourcing.Model;
import com.eventsourcing.Repository;
import com.eventsourcing.index.EntityIndex;
import com.googlecode.cqengine.resultset.ResultSet;
import java.util.Optional;
import java.util.UUID;
import static com.eventsourcing.index.EntityQueryFactory.equal;
/**
* Combines all standard queries into one:
*
* - {@link LatestAssociatedEntryQuery}
*
*/
public interface ModelQueries extends Model, LatestAssociatedEntryQuery {
/**
* Lookup an entity by a unique ID.
*
* @param repository repository
* @param klass entity klass
* @param keyAttribute entity ID attribute
* @param id ID
* @param entity type
* @return Non-empty {@link Optional} if the entity is found, empty otherwise
*/
static Optional
lookup(Repository repository, Class klass, EntityIndex keyAttribute, UUID id) {
try (ResultSet> resultSet = repository.query(klass, equal(keyAttribute, id))) {
if (resultSet.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(resultSet.uniqueResult().get());
}
}
}
}