Content of file Batch.java

/*
 * *********************************************************************************************************************
 *
 * SolidBlue 3: Data safety
 * http://tidalwave.it/projects/solidblue3
 *
 * Copyright (C) 2023 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/solidblue3j-src
 * git clone https://github.com/tidalwave-it/solidblue3j-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.datamanager.model;

import javax.annotation.concurrent.Immutable;
import jakarta.annotation.Nonnull;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import it.tidalwave.util.Id;
import it.tidalwave.messagebus.MessageBus;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import static lombok.AccessLevel.PRIVATE;

/***********************************************************************************************************************
 *
 * An event representing an object being processed.
 *
 * @stereotype  Event
 * @author      Fabrizio Giudici
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor @EqualsAndHashCode @ToString
public class Batch
  {
    private static final MessageBus DUMMY_MB = new MessageBus() // FIXME
{ @Override public <T> void publish (@Nonnull T message) { } @Override public <T> void publish (@Nonnull Class<T> topic, @Nonnull T message) { } @Override public <T> void subscribe (@Nonnull Class<T> topic, @Nonnull Listener<T> listener) { } @Override public void unsubscribe (@Nonnull Listener<?> listener) { } }; public static final Batch DUMMY = new Batch(DUMMY_MB, Id.of("-"), "", false); /******************************************************************************************************************* * An event representing the start of a batch process. * @stereotype Event ******************************************************************************************************************/ @Immutable @RequiredArgsConstructor(access = PRIVATE) @EqualsAndHashCode @ToString public static class StartEvent { @Nonnull private final Batch batch; } /******************************************************************************************************************* * An event representing the end of a batch process. * @stereotype Event ******************************************************************************************************************/ @Immutable @RequiredArgsConstructor(access = PRIVATE) @EqualsAndHashCode @ToString public static class EndEvent { @Nonnull private final Batch batch; } /******************************************************************************************************************* * An event representing an object being processed. * @stereotype Event ******************************************************************************************************************/ @Immutable @RequiredArgsConstructor(access = PRIVATE) @Getter @EqualsAndHashCode @ToString public static class ProcessingEvent { @Nonnull private final Batch batch; @Nonnull private final String name; @Nonnull private final Object object; private final int step; private final Optional<Integer> stepCount; } @Nonnull @ToString.Exclude @EqualsAndHashCode.Exclude private final MessageBus messageBus; @Nonnull private final Id id; @Getter @Nonnull private final String name; @Getter private final boolean cancellable; private final AtomicBoolean cancelled = new AtomicBoolean(); private final AtomicInteger stepSequence = new AtomicInteger(1); private Optional<Integer> stepCount = Optional.empty(); @Getter @Setter @Nonnull private String phaseName = ""; /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ public void cancel() { if (!cancellable) { throw new IllegalArgumentException("Cannot be cancelled"); } cancelled.set(true); } /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ public boolean isCancelled() { return cancelled.get(); } /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ @Nonnull public StartEvent startEvent() { return new StartEvent(this); } /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ @Nonnull public void processingEvent (@Nonnull final Object object) { messageBus.publish(new ProcessingEvent(this, phaseName, object, stepSequence.getAndIncrement(), stepCount)); } /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ @Nonnull public EndEvent endEvent() { return new EndEvent(this); } /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ public void setStepCount (final int stepCount) { this.stepCount = Optional.of(stepCount); } /******************************************************************************************************************* * * FIXME: javadoc * ******************************************************************************************************************/ public void resetStepSequence() { stepSequence.set(1); } }