package org.apache.helix.model;



import java.util.ArrayList;
import java.util.List;

import org.apache.helix.HelixProperty;
import org.apache.helix.ZNRecord;

/**
 * The history of instances that have served as the leader controller
 */
public class LeaderHistory extends HelixProperty {
  private final static int HISTORY_SIZE = 8;

  public LeaderHistory(String id) {
    super(id);
  }

  public LeaderHistory(ZNRecord record) {
    super(record);
  }

  /**
   * Save up to HISTORY_SIZE number of leaders in FIFO order
   * @param clusterName the cluster the instance leads
   * @param instanceName the name of the leader instance
   */
  public void updateHistory(String clusterName, String instanceName) {
    List<String> list = _record.getListField(clusterName);
    if (list == null) {
      list = new ArrayList<String>();
      _record.setListField(clusterName, list);
    }

    if (list.size() == HISTORY_SIZE) {
      list.remove(0);
    }
    list.add(instanceName);
  }

  @Override
  public boolean isValid() {
    return true;
  }
}
