/**
 * Copyright 2015 Chanh Nguyen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mohawk.webcrawler.lang.operator;

import com.mohawk.webcrawler.lang.BaseOperator;
import com.mohawk.webcrawler.lang.LangCore;
import com.mohawk.webcrawler.lang.ScriptContext;
import com.mohawk.webcrawler.lang.Variable;

public class HasInSet_Operator implements BaseOperator {

    @Override
    public int numOfParams() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public OperReturnType returnType() {
        // TODO Auto-generated method stub
        return OperReturnType.BOOLEAN;
    }

    @Override
    public Object run(ScriptContext pageContext, Object... params)
            throws Exception {

        Object p1 = LangCore.resolveParameter(pageContext, params[0]);
        Object p1Val = null;
        Object p2 = LangCore.resolveParameter(pageContext, params[1]);

        if (p1 instanceof Variable) {
            p1Val = ((Variable) p1).getValue();
        }

        //System.out.println("varValue>> " + params[0] + ":" + varValue);

        if (p1Val instanceof String) {
            String strValue = (String) p1Val; //System.out.println("hasInSet>> " + strValue);
            Object[] set = (Object[]) p2;
            for (Object s : set) {
                if (strValue.indexOf((String) s) != -1) {
                    return true;
                }
            }
        } else if (p1Val instanceof Number) {
            Number numValue = (Number) p1Val;
            Object[] set = (Object[]) p2;
            for (Object s : set) {
                if (numValue == ((Number) s)) {
                    return true;
                }
            }
        }

        return false;
    }

}

@@ -1519,83 +1519,8 @@
  %7D%0A%0A
-        //System.out.println(%22varValue%3E%3E %22 + params%5B0%5D + %22:%22 + varValue);%0A%0A
     
@@ -1603,77 +1603,154 @@
 Val;
- //System.out.println(%22hasInSet%3E%3E %22 + strValue);%0A            Object%5B%5D
+%0A            Object%5B%5D set;%0A            if (p2 instanceof String) %7B%0A                set = new Object%5B%5D %7B p2 %7D;%0A            %7D else %7B%0A               
  set
@@ -1759,32 +1759,47 @@
  (Object%5B%5D) p2;%0A
+            %7D%0A%0A
             for 

Fix error when set only contains one entry in ~has.