package com.chenyang.druid.sql.ast.expr;

import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.SQLExprImpl;
import com.chenyang.druid.sql.ast.SQLName;
import com.chenyang.druid.sql.ast.SQLObject;
import com.chenyang.druid.sql.ast.SQLReplaceable;
import com.chenyang.druid.sql.visitor.SQLASTVisitor;
import java.util.Collections;
import java.util.List;

public class SQLSequenceExpr extends SQLExprImpl implements SQLReplaceable {
   private SQLName sequence;
   private Function function;

   public SQLSequenceExpr() {
   }

   public SQLSequenceExpr(SQLName sequence, Function function) {
      this.sequence = sequence;
      this.function = function;
   }

   public SQLSequenceExpr clone() {
      SQLSequenceExpr x = new SQLSequenceExpr();
      if (this.sequence != null) {
         x.setSequence(this.sequence.clone());
      }

      x.function = this.function;
      return x;
   }

   protected void accept0(SQLASTVisitor visitor) {
      if (visitor.visit(this) && this.sequence != null) {
         this.sequence.accept(visitor);
      }

      visitor.endVisit(this);
   }

   public boolean replace(SQLExpr expr, SQLExpr target) {
      if (this.sequence == expr) {
         this.setSequence((SQLName)target);
         return true;
      } else {
         return false;
      }
   }

   public List<SQLObject> getChildren() {
      return Collections.singletonList(this.sequence);
   }

   public SQLName getSequence() {
      return this.sequence;
   }

   public void setSequence(SQLName sequence) {
      this.sequence = sequence;
   }

   public Function getFunction() {
      return this.function;
   }

   public void setFunction(Function function) {
      this.function = function;
   }

   public int hashCode() {
      int prime = 31;
      int result = 1;
      result = 31 * result + (this.function == null ? 0 : this.function.hashCode());
      result = 31 * result + (this.sequence == null ? 0 : this.sequence.hashCode());
      return result;
   }

   public boolean equals(Object obj) {
      if (this == obj) {
         return true;
      } else if (obj == null) {
         return false;
      } else if (this.getClass() != obj.getClass()) {
         return false;
      } else {
         SQLSequenceExpr other = (SQLSequenceExpr)obj;
         if (this.function != other.function) {
            return false;
         } else {
            if (this.sequence == null) {
               if (other.sequence != null) {
                  return false;
               }
            } else if (!this.sequence.equals(other.sequence)) {
               return false;
            }

            return true;
         }
      }
   }

   public static enum Function {
      NextVal("NEXTVAL"),
      CurrVal("CURRVAL"),
      PrevVal("PREVVAL");

      public final String name;
      public final String name_lcase;

      private Function(String name) {
         this.name = name;
         this.name_lcase = name.toLowerCase();
      }
   }
}
