package net.sf.jasperreports.engine.fill;

import java.io.Serializable;
import net.sf.jasperreports.engine.JRGroup;

public class JREvaluationTime implements Serializable {
  private static final long serialVersionUID = 10200L;
  
  public static final JREvaluationTime EVALUATION_TIME_REPORT = new JREvaluationTime((byte)2, null, null);
  
  public static final JREvaluationTime EVALUATION_TIME_PAGE = new JREvaluationTime((byte)3, null, null);
  
  public static final JREvaluationTime EVALUATION_TIME_COLUMN = new JREvaluationTime((byte)4, null, null);
  
  public static final JREvaluationTime EVALUATION_TIME_NOW = new JREvaluationTime((byte)1, null, null);
  
  private final byte type;
  
  private final String groupName;
  
  private final int bandId;
  
  private final int hash;
  
  public static JREvaluationTime getGroupEvaluationTime(String groupName) {
    return new JREvaluationTime((byte)5, groupName, null);
  }
  
  public static JREvaluationTime getBandEvaluationTime(JRFillBand band) {
    return new JREvaluationTime((byte)6, null, band);
  }
  
  public static JREvaluationTime getEvaluationTime(byte type, JRGroup group, JRFillBand band) {
    switch (type) {
      case 2:
        evaluationTime = EVALUATION_TIME_REPORT;
        return evaluationTime;
      case 3:
        evaluationTime = EVALUATION_TIME_PAGE;
        return evaluationTime;
      case 4:
        evaluationTime = EVALUATION_TIME_COLUMN;
        return evaluationTime;
      case 5:
        evaluationTime = getGroupEvaluationTime(group.getName());
        return evaluationTime;
      case 6:
        evaluationTime = getBandEvaluationTime(band);
        return evaluationTime;
    } 
    JREvaluationTime evaluationTime = null;
    return evaluationTime;
  }
  
  private JREvaluationTime(byte type, String groupName, JRFillBand band) {
    this.type = type;
    this.groupName = groupName;
    this.bandId = (band == null) ? 0 : band.getId();
    this.hash = computeHash();
  }
  
  private int computeHash() {
    int hashCode = this.type;
    hashCode = 31 * hashCode + ((this.groupName == null) ? 0 : this.groupName.hashCode());
    hashCode = 31 * hashCode + this.bandId;
    return hashCode;
  }
  
  public boolean equals(Object obj) {
    if (obj == this)
      return true; 
    JREvaluationTime e = (JREvaluationTime)obj;
    boolean eq = (e.type == this.type);
    if (eq)
      switch (this.type) {
        case 5:
          eq = this.groupName.equals(e.groupName);
          break;
        case 6:
          eq = (this.bandId == e.bandId);
          break;
      }  
    return eq;
  }
  
  public int hashCode() {
    return this.hash;
  }
}
