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

org.opentcs.data.TCSObjectEvent Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
/**
 * Copyright (c) The openTCS Authors.
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.data;

import java.io.Serializable;
import static java.util.Objects.requireNonNull;

/**
 * Instances of this class represent events emitted by/for business objects.
 *
 * @author Stefan Walter (Fraunhofer IML)
 */
public class TCSObjectEvent
    implements Serializable {

  /**
   * The current state of the object for which this event was created.
   */
  private final TCSObject currentObjectState;
  /**
   * The previous state of the object for which this event was created.
   */
  private final TCSObject previousObjectState;
  /**
   * This event's type.
   */
  private final Type type;

  /**
   * Creates a new TCSObjectEvent.
   *
   * @param currentObjectState The current state of the object for which this
   * event was created. Value is irrelevant/may be null if
   * eventType is OBJECT_REMOVED.
   * @param previousObjectState The previous state of the object for which this
   * event was created.Value is irrelevant/may be null if
   * eventType is OBJECT_CREATED.
   * @param eventType The event's type.
   * @throws NullPointerException If eventType is
   * null.
   * @throws IllegalArgumentException If either currentObjectState
   * or previousObjectState is null while
   * eventType does not have an appropriate value.
   */
  public TCSObjectEvent(TCSObject currentObjectState,
                        TCSObject previousObjectState,
                        Type eventType) {
    this.type = requireNonNull(eventType, "eventType");
    if (currentObjectState == null && !Type.OBJECT_REMOVED.equals(eventType)) {
      throw new IllegalArgumentException("currentObjectState == null but "
          + "eventType != OBJECT_REMOVED");
    }
    if (previousObjectState == null && !Type.OBJECT_CREATED.equals(eventType)) {
      throw new IllegalArgumentException("previousObjectState == null but "
          + "eventType != OBJECT_CREATED");
    }
    this.currentObjectState = currentObjectState;
    this.previousObjectState = previousObjectState;
  }

  /**
   * Returns the current state of the object for which this event was created.
   *
   * @return The current state of the object for which this event was created.
   */
  public TCSObject getCurrentObjectState() {
    return currentObjectState;
  }

  /**
   * Returns the previous state of the object for which this event was created.
   *
   * @return The previous state of the object for which this event was created.
   */
  public TCSObject getPreviousObjectState() {
    return previousObjectState;
  }

  /**
   * Returns the current state of the object for which this event was created,
   * or, if the current state is null, the previous state.
   *
   * @return The current or the previous state of the object for which this
   * event was created.
   */
  public TCSObject getCurrentOrPreviousObjectState() {
    if (currentObjectState != null) {
      return currentObjectState;
    }
    else {
      return previousObjectState;
    }
  }

  /**
   * Returns this event's type.
   *
   * @return This event's type.
   */
  public Type getType() {
    return type;
  }

  @Override
  public String toString() {
    return "TCSObjectEvent{"
        + "type=" + type
        + ", currentObjectState=" + currentObjectState
        + ", previousObjectState=" + previousObjectState
        + '}';
  }

  /**
   * Indicates the type of an event, which can be helpful with filtering events.
   */
  public static enum Type {

    /**
     * Indicates that the referenced object has been newly created.
     */
    OBJECT_CREATED,
    /**
     * Indicates that the referenced object has been modified.
     */
    OBJECT_MODIFIED,
    /**
     * Indicates that the referenced object is no longer a valid kernel object.
     */
    OBJECT_REMOVED;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy