com.akrema.stringeval.utils.ExprUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.akrema.stringeval.utils.ExprUtils.java

Source

/*
 *  ===============================================================
 *  * Copyright  2014   <http://Akrema.darvour-tut.com>
 *  ==============================================================
 *  ***************This file is part of StringEval*******************
 *
 *  *StringEval is free software: you can redistribute it and/or modify
 *  *it under the terms of the GNU General Public License as published by
 *  *the Free Software Foundation, either version 3 of the License, or
 *  *(at your option) any later version.
 *
 *  *StringEval is distributed in the hope that it will be useful,
 *  *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  *GNU General Public License for more details.
 *
 *  *You should have received a copy of the GNU General Public License
 *  *along with StringEval.  If not, see <http://www.gnu.org/licenses/>.
 *  ================================================================
 *  ==Abdelhak Akermi
 *  ==Tunisia
 *  ==Mednine-RTE Gabes
 *  ==Contact : Akrema@darvour-tut.com/abdelhak.akermi@gmail.com
 *  ==================================================================
 */

package com.akrema.stringeval.utils;

import com.akrema.stringeval.StringCONST;
import com.akrema.stringeval.exception.InputStringException;
import org.apache.commons.lang.text.StrTokenizer;

import java.util.HashMap;

/**
 * @author A.Akermi
 */
public class ExprUtils {

    /**
     * extract arguments from the input string
     * and specify if we have integer or string arguments
     *
     * @param inputstring the parameters in the expression
     * @return map contain number of argument+value
     */
    public static HashMap<Integer, Object> extractArguments(String inputstring) throws InputStringException {
        HashMap<Integer, Object> args = new HashMap<Integer, Object>();

        StrTokenizer tokenizer = new StrTokenizer(inputstring, StringCONST.DELM);
        if (tokenizer.size() > 3) {
            throw new InputStringException(" the arguments list must be 3 ");
        }
        int index = 1;
        while (tokenizer.hasNext()) {
            String ch = tokenizer.nextToken();
            if (ch.matches("\\d+")) {
                args.put(index, Integer.parseInt(ch));
                index++;
            } else {
                args.put(index, ch);
                index++;
            }

        }

        return args;
    }

    /**
     * check if a string is a digit or not
     *
     * @param str passed string
     * @return True if the str match the pattern
     */
    public static boolean isNumeric(String str) {
        return str.matches("-?\\d+(\\.\\d+)?");
    }

    public static boolean IsSpecialCaracter(char character) {

        return character == ' ' || character == '\t' || character == '\n' || character == '\r' || character == '\f';

    }

    /**
     * check if a string is a defined or true
     * caracter or not
     *
     * @param str the passed string
     * @return true if <code>Character.isDefined !=-1</code>
     */
    public static int getTypeArgs(String str) {
        str.trim();
        int type = 2;
        if (str.length() == 1)
            type = StringCONST.TYPE_CHAR;
        if (ExprUtils.isNumeric(str))
            type = StringCONST.TYPE_INTEGER;
        if (str.length() > 1)
            type = StringCONST.TYPE_STRING;

        return type;
    }

    /* public static void main(String[] args) {
    System.out.println(getTypeArgs("\'d\'"));
     }*/
}