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

com.eventsourcing.cep.protocols.NameProtocol Maven / Gradle / Ivy

The 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.cep.protocols;

import com.eventsourcing.EntityHandle;
import com.eventsourcing.Protocol;
import com.eventsourcing.Repository;
import com.eventsourcing.cep.events.NameChanged;
import com.eventsourcing.queries.ModelCollectionQuery;
import com.eventsourcing.queries.ModelLoader;
import com.eventsourcing.queries.ModelQueries;
import com.googlecode.cqengine.resultset.ResultSet;
import org.unprotocols.coss.Draft;
import org.unprotocols.coss.RFC;

import java.util.Optional;
import java.util.stream.Stream;

import static com.eventsourcing.index.EntityQueryFactory.equal;
import static com.googlecode.cqengine.stream.StreamFactory.streamOf;

@Draft @RFC(url = "http://rfc.eventsourcing.com/spec:3/CEP")
public interface NameProtocol extends Protocol, ModelQueries {
    default String name() {
        Optional last = latestAssociatedEntity(NameChanged.class, NameChanged.REFERENCE_ID, NameChanged.TIMESTAMP);
        if (last.isPresent()) {
            return last.get().name();
        } else {
            return null;
        }
    }

    class NamedModelCollectionQuery implements ModelCollectionQuery {

        private final String name;
        private final ModelLoader loader;

        public NamedModelCollectionQuery(String name, ModelLoader loader) {
            this.name = name;
            this.loader = loader;
        }

        @Override public Stream getCollectionStream(Repository repository) {
            ResultSet> resultSet = repository
                    .query(NameChanged.class, equal(NameChanged.NAME, name));
            return streamOf(resultSet)
                    .map(h -> loader.load(repository, h.get().reference()).get())
                    .onClose(resultSet::close);
        }
    }

    static  ModelCollectionQuery named(String name, ModelLoader loader) {
        return new NamedModelCollectionQuery<>(name, loader);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy