All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eventsourcing.queries.ModelQueries Maven / Gradle / Ivy

There is a newer version: 0.4.6
Show newest version
/**
 * 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()); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy