package net.sf.jasperreports.crosstabs.fill;

import net.sf.jasperreports.crosstabs.JRCrosstabMeasure;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.JRVariable;
import net.sf.jasperreports.engine.fill.JRDefaultIncrementerFactory;
import net.sf.jasperreports.engine.fill.JRExtendedIncrementerFactory;
import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
import net.sf.jasperreports.engine.fill.JRFillVariable;
import net.sf.jasperreports.engine.fill.JRIncrementerFactoryCache;

public class JRFillCrosstabMeasure implements JRCrosstabMeasure {
  protected JRCrosstabMeasure parentMeasure;
  
  protected JRFillVariable variable;
  
  protected JRExtendedIncrementerFactory incrementerFactory;
  
  protected JRPercentageCalculator percentageCalculator;
  
  public JRFillCrosstabMeasure(JRCrosstabMeasure measure, JRFillObjectFactory factory) {
    factory.put(measure, this);
    this.parentMeasure = measure;
    this.variable = factory.getVariable(measure.getVariable());
    this.incrementerFactory = createIncrementerFactory();
    this.percentageCalculator = createPercentageCalculator();
  }
  
  public String getName() {
    return this.parentMeasure.getName();
  }
  
  public String getValueClassName() {
    return this.parentMeasure.getValueClassName();
  }
  
  public Class getValueClass() {
    return this.parentMeasure.getValueClass();
  }
  
  public JRExpression getValueExpression() {
    return this.parentMeasure.getValueExpression();
  }
  
  public byte getCalculation() {
    return this.parentMeasure.getCalculation();
  }
  
  public String getIncrementerFactoryClassName() {
    return this.parentMeasure.getIncrementerFactoryClassName();
  }
  
  public Class getIncrementerFactoryClass() {
    return this.parentMeasure.getIncrementerFactoryClass();
  }
  
  public byte getPercentageOfType() {
    return this.parentMeasure.getPercentageOfType();
  }
  
  public JRVariable getVariable() {
    return (JRVariable)this.variable;
  }
  
  public JRFillVariable getFillVariable() {
    return this.variable;
  }
  
  public JRExtendedIncrementerFactory getIncrementerFactory() {
    return this.incrementerFactory;
  }
  
  public JRPercentageCalculator getPercentageCalculator() {
    return this.percentageCalculator;
  }
  
  private JRExtendedIncrementerFactory createIncrementerFactory() {
    JRExtendedIncrementerFactory incrFactory;
    String incrementerFactoryClassName = getIncrementerFactoryClassName();
    if (incrementerFactoryClassName == null) {
      incrFactory = JRDefaultIncrementerFactory.getFactory(getValueClass());
    } else {
      incrFactory = (JRExtendedIncrementerFactory)JRIncrementerFactoryCache.getInstance(getIncrementerFactoryClass());
    } 
    return incrFactory;
  }
  
  public JRPercentageCalculator createPercentageCalculator() {
    JRPercentageCalculator percentageCalc;
    if (getPercentageOfType() == 1) {
      percentageCalc = JRPercentageCalculatorFactory.getPercentageCalculator(getPercentageCalculatorClass(), getValueClass());
    } else {
      percentageCalc = null;
    } 
    return percentageCalc;
  }
  
  public String getPercentageCalculatorClassName() {
    return this.parentMeasure.getPercentageCalculatorClassName();
  }
  
  public Class getPercentageCalculatorClass() {
    return this.parentMeasure.getPercentageCalculatorClass();
  }
  
  public Object clone() {
    return null;
  }
}
