Search in sources :

Example 1 with IdSetEventFunction

use of io.spine.server.entity.idfunc.IdSetEventFunction in project core-java by SpineEventEngine.

the class IdSetFunctions method put.

/**
     * Puts a function into the map.
     *
     * @param eventClass the class of the event handled by the function
     * @param func       the function instance
     * @param <E>        the type of the event message
     */
public <E extends Message> void put(Class<E> eventClass, IdSetEventFunction<I, E> func) {
    final EventClass clazz = EventClass.of(eventClass);
    @SuppressWarnings("unchecked") final IdSetEventFunction<I, Message> // since we want to store {@code IdSetFunction}s for various event types.
    casted = (IdSetEventFunction<I, Message>) func;
    map.put(clazz, casted);
}
Also used : EventClass(io.spine.type.EventClass) Message(com.google.protobuf.Message) IdSetEventFunction(io.spine.server.entity.idfunc.IdSetEventFunction)

Example 2 with IdSetEventFunction

use of io.spine.server.entity.idfunc.IdSetEventFunction in project core-java by SpineEventEngine.

the class IdSetFunctions method get.

/**
     * Obtains a function for the passed event class.
     *
     * @param eventClass the class of the event message
     * @param <E>        the type of the event message
     * @return the function wrapped into {@code Optional} or empty {@code Optional}
     * if there is no matching function
     */
public <E extends Message> Optional<IdSetEventFunction<I, E>> get(Class<E> eventClass) {
    final EventClass clazz = EventClass.of(eventClass);
    final IdSetEventFunction<I, Message> func = map.get(clazz);
    // we ensure the type when we put into the map.
    @SuppressWarnings("unchecked") final IdSetEventFunction<I, E> result = (IdSetEventFunction<I, E>) func;
    return Optional.fromNullable(result);
}
Also used : EventClass(io.spine.type.EventClass) Message(com.google.protobuf.Message) IdSetEventFunction(io.spine.server.entity.idfunc.IdSetEventFunction)

Example 3 with IdSetEventFunction

use of io.spine.server.entity.idfunc.IdSetEventFunction in project core-java by SpineEventEngine.

the class ProjectionRepositoryShould method use_id_set_function.

@Test
public void use_id_set_function() {
    final IdSetEventFunction<ProjectId, ProjectCreated> delegateFn = new IdSetEventFunction<ProjectId, ProjectCreated>() {

        @Override
        public Set<ProjectId> apply(ProjectCreated message, EventContext context) {
            return newHashSet();
        }
    };
    final IdSetEventFunction<ProjectId, ProjectCreated> idSetFunction = spy(delegateFn);
    repository().addIdSetFunction(ProjectCreated.class, idSetFunction);
    final Event event = createEvent(PRODUCER_ID, projectCreated());
    repository().dispatch(event);
    final ProjectCreated expectedEventMessage = Events.getMessage(event);
    final EventContext context = event.getContext();
    verify(idSetFunction).apply(eq(expectedEventMessage), eq(context));
}
Also used : EventContext(io.spine.base.EventContext) ProjectId(io.spine.test.projection.ProjectId) Event(io.spine.base.Event) IdSetEventFunction(io.spine.server.entity.idfunc.IdSetEventFunction) ProjectCreated(io.spine.test.projection.event.ProjectCreated) Test(org.junit.Test)

Aggregations

IdSetEventFunction (io.spine.server.entity.idfunc.IdSetEventFunction)3 Message (com.google.protobuf.Message)2 EventClass (io.spine.type.EventClass)2 Event (io.spine.base.Event)1 EventContext (io.spine.base.EventContext)1 ProjectId (io.spine.test.projection.ProjectId)1 ProjectCreated (io.spine.test.projection.event.ProjectCreated)1 Test (org.junit.Test)1