package org.nfunk.jep.function;

import java.util.Stack;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.type.Complex;

public class Comparative extends PostfixMathCommand {
  int id;
  
  double tolerance;
  
  public static final int LT = 0;
  
  public static final int GT = 1;
  
  public static final int LE = 2;
  
  public static final int GE = 3;
  
  public static final int NE = 4;
  
  public static final int EQ = 5;
  
  public Comparative(int paramInt) {
    this.id = paramInt;
    this.numberOfParameters = 2;
    this.tolerance = 1.0E-6D;
  }
  
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object1 = paramStack.pop();
    Object object2 = paramStack.pop();
    boolean bool = false;
    switch (this.id) {
      case 0:
        bool = lt(object2, object1);
        break;
      case 1:
        bool = gt(object2, object1);
        break;
      case 2:
        bool = le(object2, object1);
        break;
      case 3:
        bool = ge(object2, object1);
        break;
      case 4:
        bool = ne(object2, object1);
        break;
      case 5:
        bool = eq(object2, object1);
        break;
    } 
    if (bool) {
      paramStack.push(new Double(1.0D));
    } else {
      paramStack.push(new Double(0.0D));
    } 
  }
  
  public boolean lt(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex || paramObject2 instanceof Complex)
      throw new ParseException("< not defined for complex numbers"); 
    if (paramObject1 instanceof Number && paramObject2 instanceof Number) {
      double d1 = ((Number)paramObject1).doubleValue();
      double d2 = ((Number)paramObject2).doubleValue();
      return (d1 < d2);
    } 
    throw new ParseException("< not defined for object of type " + paramObject1.getClass().getName() + " and " + paramObject1.getClass().getName());
  }
  
  public boolean gt(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex || paramObject2 instanceof Complex)
      throw new ParseException("> not defined for complex numbers"); 
    if (paramObject1 instanceof Number && paramObject2 instanceof Number) {
      double d1 = ((Number)paramObject1).doubleValue();
      double d2 = ((Number)paramObject2).doubleValue();
      return (d1 > d2);
    } 
    throw new ParseException("> not defined for object of type " + paramObject1.getClass().getName() + " and " + paramObject1.getClass().getName());
  }
  
  public boolean le(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex || paramObject2 instanceof Complex)
      throw new ParseException("<= not defined for complex numbers"); 
    if (paramObject1 instanceof Number && paramObject2 instanceof Number) {
      double d1 = ((Number)paramObject1).doubleValue();
      double d2 = ((Number)paramObject2).doubleValue();
      return (d1 <= d2);
    } 
    throw new ParseException("<= not defined for object of type " + paramObject1.getClass().getName() + " and " + paramObject1.getClass().getName());
  }
  
  public boolean ge(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex || paramObject2 instanceof Complex)
      throw new ParseException(">= not defined for complex numbers"); 
    if (paramObject1 instanceof Number && paramObject2 instanceof Number) {
      double d1 = ((Number)paramObject1).doubleValue();
      double d2 = ((Number)paramObject2).doubleValue();
      return (d1 >= d2);
    } 
    throw new ParseException(">= not defined for object of type " + paramObject1.getClass().getName() + " and " + paramObject1.getClass().getName());
  }
  
  public boolean eq(Object paramObject1, Object paramObject2) throws ParseException {
    return (paramObject1 instanceof Complex && paramObject2 instanceof Complex) ? ((Complex)paramObject1).equals((Complex)paramObject2, this.tolerance) : ((paramObject1 instanceof Complex && paramObject2 instanceof Double) ? ((Complex)paramObject1).equals(new Complex((Number)paramObject2), this.tolerance) : ((paramObject2 instanceof Complex && paramObject1 instanceof Double) ? ((Complex)paramObject2).equals(new Complex((Number)paramObject1), this.tolerance) : paramObject1.equals(paramObject2)));
  }
  
  public boolean ne(Object paramObject1, Object paramObject2) throws ParseException {
    return (paramObject1 instanceof Complex && paramObject2 instanceof Complex) ? (!((Complex)paramObject1).equals((Complex)paramObject2, this.tolerance)) : ((paramObject1 instanceof Complex && paramObject2 instanceof Double) ? (!((Complex)paramObject1).equals(new Complex((Number)paramObject2), this.tolerance)) : ((paramObject2 instanceof Complex && paramObject1 instanceof Double) ? (!((Complex)paramObject2).equals(new Complex((Number)paramObject1), this.tolerance)) : (!paramObject1.equals(paramObject2))));
  }
  
  public double getTolerance() {
    return this.tolerance;
  }
  
  public void setTolerance(double paramDouble) {
    this.tolerance = paramDouble;
  }
}
