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

io.atomix.core.election.LeadershipEvent Maven / Gradle / Ivy

There is a newer version: 3.2.0-alpha12
Show newest version
/*
 * Copyright 2014-present Open Networking Foundation
 *
 * 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.
 */
package io.atomix.core.election;

import com.google.common.base.MoreObjects;
import io.atomix.utils.event.AbstractEvent;

import java.util.Objects;

/**
 * Describes leadership election event.
 */
public class LeadershipEvent extends AbstractEvent {

  /**
   * Type of leadership events.
   */
  public enum Type {
    /**
     * Leader changed event.
     */
    CHANGE,
  }

  private final String topic;
  private final Leadership oldLeadership;
  private final Leadership newLeadership;

  /**
   * Creates an event of a given type and for the specified instance and the
   * current time.
   *
   * @param type          leadership event type
   * @param oldLeadership previous leadership
   * @param newLeadership new leadership
   */
  public LeadershipEvent(Type type, String topic, Leadership oldLeadership, Leadership newLeadership) {
    this(type, topic, oldLeadership, newLeadership, System.currentTimeMillis());
  }

  /**
   * Creates an event of a given type and for the specified subject and time.
   *
   * @param type          leadership event type
   * @param oldLeadership previous leadership
   * @param newLeadership new leadership
   * @param time          occurrence time
   */
  public LeadershipEvent(Type type, String topic, Leadership oldLeadership, Leadership newLeadership, long time) {
    super(type, newLeadership, time);
    this.topic = topic;
    this.oldLeadership = oldLeadership;
    this.newLeadership = newLeadership;
  }

  /**
   * Returns the leader election topic.
   *
   * @return the leader election topic
   */
  public String topic() {
    return topic;
  }

  /**
   * Returns the prior leadership for the topic.
   *
   * @return the prior leadership for the topic
   */
  public Leadership oldLeadership() {
    return oldLeadership;
  }

  /**
   * Returns the new leadership for the topic.
   *
   * @return the new leadership for the topic
   */
  public Leadership newLeadership() {
    return newLeadership;
  }

  @Override
  public int hashCode() {
    return Objects.hash(type(), subject(), time());
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj instanceof LeadershipEvent) {
      final LeadershipEvent other = (LeadershipEvent) obj;
      return Objects.equals(this.type(), other.type())
          && Objects.equals(this.subject(), other.subject())
          && Objects.equals(this.time(), other.time());
    }
    return false;
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this.getClass())
        .add("type", type())
        .add("topic", topic())
        .add("oldLeadership", oldLeadership())
        .add("newLeadership", newLeadership())
        .add("time", time())
        .toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy