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

com.github.rahulsom.grooves.grails.RxSnapshotSource Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
package com.github.rahulsom.grooves.grails;

import com.github.rahulsom.grooves.api.AggregateType;
import com.github.rahulsom.grooves.api.events.BaseEvent;
import com.github.rahulsom.grooves.api.snapshots.Snapshot;
import com.github.rahulsom.grooves.queries.QuerySupport;
import com.github.rahulsom.grooves.queries.internal.BaseQuery;
import grails.gorm.rx.RxEntity;
import rx.Observable;

import java.util.Date;

import static com.github.rahulsom.grooves.grails.QueryUtil.*;
import static org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod;

/**
 * Supplies Snapshots from a Blocking Gorm Source.
 *
 * @param   The aggregate over which the query executes
 * @param     The type of the Event's id field
 * @param       The type of the Event
 * @param  The type of the Snapshot's id field
 * @param    The type of the Snapshot
 *
 * @author Rahul Somasunderam
 */
public interface RxSnapshotSource<
        AggregateIdT,
        AggregateT extends AggregateType,
        EventIdT,
        EventT extends BaseEvent,
        SnapshotIdT,
        SnapshotT extends Snapshot
                & RxEntity,
        QueryT extends BaseQuery
        > extends QuerySupport {

    Class getSnapshotClass();

    @Override
    default Observable getSnapshot(long maxPosition, AggregateT aggregate) {
        //noinspection unchecked
        return (Observable) (maxPosition == Long.MAX_VALUE ?
                invokeStaticMethod(getSnapshotClass(),
                        SNAPSHOTS_BY_AGGREGATE,
                        new Object[]{aggregate.getId(), LATEST_BY_POSITION}) :
                invokeStaticMethod(
                        getSnapshotClass(),
                        SNAPSHOTS_BY_POSITION,
                        new Object[]{aggregate.getId(), maxPosition, LATEST_BY_POSITION}));
    }

    @Override
    default Observable getSnapshot(Date maxTimestamp, AggregateT aggregate) {
        //noinspection unchecked
        return (Observable) (maxTimestamp == null ?
                invokeStaticMethod(getSnapshotClass(),
                        SNAPSHOTS_BY_AGGREGATE,
                        new Object[]{aggregate.getId(), LATEST_BY_POSITION}) :
                invokeStaticMethod(getSnapshotClass(),
                        SNAPSHOTS_BY_TIMETTAMP,
                        new Object[]{aggregate.getId(), maxTimestamp, LATEST_BY_POSITION}));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy