Example usage for java.util Stack push

List of usage examples for java.util Stack push

Introduction

In this page you can find the example usage for java.util Stack push.

Prototype

public E push(E item) 

Source Link

Document

Pushes an item onto the top of this stack.

Usage

From source file:com.baidu.rigel.biplatform.parser.CompileSection.java

private void sectionProcess(Stack<Node> nodes, Node tokenNode, CalculateNode calcNode) {
    if (nodes.isEmpty()) {
        if (calcNode == null) {
            nodes.push(tokenNode);
        } else {//from w  ww.  java2s. co m
            calcNode.setLeft(tokenNode);
            nodes.push(calcNode);
        }
    } else {
        // ?
        if (!nodes.peek().getNodeType().equals(NodeType.Calculate)) {
            // ??
            throw new NotAllowedOperationException("not allowed");
        }
        CalculateNode node = (CalculateNode) nodes.peek();
        if (calcNode == null) {
            node.setRight(tokenNode);
            return;
        }
        if (node.getOperation().getPriority() >= calcNode.getOperation().getPriority()) {
            node.setRight(tokenNode);
            calcNode.setLeft(node);
            // 
            nodes.pop();
        } else {
            calcNode.setLeft(tokenNode);
        }
        nodes.push(calcNode);
    }
}

From source file:org.apache.storm.daemon.logviewer.utils.DirectoryCleaner.java

/**
 * If totalSize of files exceeds the either the per-worker quota or global quota,
 * Logviewer deletes oldest inactive log files in a worker directory or in all worker dirs.
 * We use the parameter forPerDir to switch between the two deletion modes.
 *
 * @param dirs the list of directories to be scanned for deletion
 * @param quota the per-dir quota or the total quota for the all directories
 * @param forPerDir if true, deletion happens for a single dir; otherwise, for all directories globally
 * @param activeDirs only for global deletion, we want to skip the active logs in activeDirs
 * @return number of files deleted//from  w  ww  .j  a v  a  2 s .  c  o  m
 */
public DeletionMeta deleteOldestWhileTooLarge(List<Path> dirs, long quota, boolean forPerDir,
        Set<Path> activeDirs) throws IOException {
    long totalSize = 0;
    for (Path dir : dirs) {
        try (DirectoryStream<Path> stream = getStreamForDirectory(dir)) {
            for (Path path : stream) {
                totalSize += Files.size(path);
            }
        }
    }
    LOG.debug("totalSize: {} quota: {}", totalSize, quota);
    long toDeleteSize = totalSize - quota;
    if (toDeleteSize <= 0) {
        return DeletionMeta.EMPTY;
    }

    int deletedFiles = 0;
    long deletedSize = 0;
    // the oldest pq_size files in this directory will be placed in PQ, with the newest at the root
    PriorityQueue<Pair<Path, FileTime>> pq = new PriorityQueue<>(PQ_SIZE,
            Comparator.comparing((Pair<Path, FileTime> p) -> p.getRight()).reversed());
    int round = 0;
    final Set<Path> excluded = new HashSet<>();
    while (toDeleteSize > 0) {
        LOG.debug("To delete size is {}, start a new round of deletion, round: {}", toDeleteSize, round);
        for (Path dir : dirs) {
            try (DirectoryStream<Path> stream = getStreamForDirectory(dir)) {
                for (Path path : stream) {
                    if (!excluded.contains(path)) {
                        if (isFileEligibleToSkipDelete(forPerDir, activeDirs, dir, path)) {
                            excluded.add(path);
                        } else {
                            Pair<Path, FileTime> p = Pair.of(path, Files.getLastModifiedTime(path));
                            if (pq.size() < PQ_SIZE) {
                                pq.offer(p);
                            } else if (p.getRight().toMillis() < pq.peek().getRight().toMillis()) {
                                pq.poll();
                                pq.offer(p);
                            }
                        }
                    }
                }
            }
        }
        if (!pq.isEmpty()) {
            // need to reverse the order of elements in PQ to delete files from oldest to newest
            Stack<Pair<Path, FileTime>> stack = new Stack<>();
            while (!pq.isEmpty()) {
                stack.push(pq.poll());
            }
            while (!stack.isEmpty() && toDeleteSize > 0) {
                Pair<Path, FileTime> pair = stack.pop();
                Path file = pair.getLeft();
                final String canonicalPath = file.toAbsolutePath().normalize().toString();
                final long fileSize = Files.size(file);
                final long lastModified = pair.getRight().toMillis();
                //Original implementation doesn't actually check if delete succeeded or not.
                try {
                    Utils.forceDelete(file.toString());
                    LOG.info("Delete file: {}, size: {}, lastModified: {}", canonicalPath, fileSize,
                            lastModified);
                    toDeleteSize -= fileSize;
                    deletedSize += fileSize;
                    deletedFiles++;
                } catch (IOException e) {
                    excluded.add(file);
                }
            }
            pq.clear();
            round++;
            if (round >= MAX_ROUNDS) {
                if (forPerDir) {
                    LOG.warn(
                            "Reach the MAX_ROUNDS: {} during per-dir deletion, you may have too many files in "
                                    + "a single directory : {}, will delete the rest files in next interval.",
                            MAX_ROUNDS, dirs.get(0).toAbsolutePath().normalize());
                } else {
                    LOG.warn("Reach the MAX_ROUNDS: {} during global deletion, you may have too many files, "
                            + "will delete the rest files in next interval.", MAX_ROUNDS);
                }
                break;
            }
        } else {
            LOG.warn("No more files able to delete this round, but {} is over quota by {} MB",
                    forPerDir ? "this directory" : "root directory", toDeleteSize * 1e-6);
        }
    }
    return new DeletionMeta(deletedSize, deletedFiles);
}

From source file:com.consol.citrus.Citrus.java

/**
 * Method to retrieve the full class name for a test.
 * Hierarchy of folders is supported, too.
 *
 * @param startDir directory where to start the search
 * @param testName test name to search for
 * @throws CitrusRuntimeException// www . j a va 2 s .  com
 * @return the class name of the test
 */
private String getClassNameForTest(final String startDir, final String testName) throws FileNotFoundException {
    /* Stack to hold potential sub directories */
    final Stack<File> dirs = new Stack<File>();
    /* start directory */
    final File startdir = new File(startDir);

    if (startdir.isDirectory()) {
        dirs.push(startdir);
    }

    log.info("Starting test search in dir: " + startdir.getAbsolutePath());

    /* walk through the directories */
    while (dirs.size() > 0) {
        File file = dirs.pop();
        File[] found = file.listFiles(new TestCaseFileNameFilter());

        for (int i = 0; i < found.length; i++) {
            /* Subfolder support */
            if (found[i].isDirectory()) {
                dirs.push(found[i]);
            } else {
                if ((testName + XML_FILE_EXTENSION).equalsIgnoreCase(found[i].getName())) {
                    String fileName = found[i].getPath();
                    fileName = fileName.substring(0, (fileName.length() - XML_FILE_EXTENSION.length()));

                    if (fileName.startsWith(File.separator)) {
                        fileName = fileName.substring(File.separator.length());
                    }

                    //replace operating system path separator and translate to class package string
                    fileName = fileName.substring(
                            startDir.startsWith(File.separator) ? startDir.length() - 1 : startDir.length())
                            .replace(File.separatorChar, '.');

                    if (log.isDebugEnabled()) {
                        log.debug("Found test '" + fileName + "'");
                    }

                    return fileName;
                }
            }
        }
    }

    throw new CitrusRuntimeException(
            "Could not find test with name '" + testName + "'. Test directory is: " + startDir);
}

From source file:net.iponweb.hadoop.streaming.parquet.TextRecordWriterWrapper.java

@Override
public void write(Text key, Text value) throws IOException {

    Group grp = factory.newGroup();

    String[] strK = key.toString().split(TAB, -1);
    String[] strV = value == null ? new String[0] : value.toString().split(TAB, -1);
    String kv_combined[] = (String[]) ArrayUtils.addAll(strK, strV);

    Iterator<PathAction> ai = recorder.iterator();

    Stack<Group> groupStack = new Stack<>();
    groupStack.push(grp);
    int i = 0;/*from  www.  j  a  va 2 s.c  om*/

    try {
        while (ai.hasNext()) {

            PathAction a = ai.next();
            switch (a.getAction()) {
            case GROUPEND:
                grp = groupStack.pop();
                break;

            case GROUPSTART:
                groupStack.push(grp);
                grp = grp.addGroup(a.getName());
                break;

            case FIELD:
                String s = null;
                PrimitiveType.PrimitiveTypeName primType = a.getType();
                String colName = a.getName();

                if (i < kv_combined.length)
                    s = kv_combined[i++];

                if (s == null) {
                    if (a.getRepetition() == Type.Repetition.OPTIONAL) {
                        i++;
                        continue;
                    }
                    s = primType == PrimitiveType.PrimitiveTypeName.BINARY ? "" : "0";
                }

                // If we have 'repeated' field, assume that we should expect JSON-encoded array
                // Convert array and append all values
                int repetition = 1;
                boolean repeated = false;
                ArrayList<String> s_vals = null;
                if (a.getRepetition() == Type.Repetition.REPEATED) {
                    repeated = true;
                    s_vals = new ArrayList<>();
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode node = mapper.readTree(s);
                    Iterator<JsonNode> itr = node.iterator();
                    repetition = 0;
                    while (itr.hasNext()) {

                        String o;
                        switch (primType) {
                        case BINARY:
                            o = itr.next().getTextValue(); // No array-of-objects!
                            break;
                        case BOOLEAN:
                            o = String.valueOf(itr.next().getBooleanValue());
                            break;
                        default:
                            o = String.valueOf(itr.next().getNumberValue());
                        }

                        s_vals.add(o);
                        repetition++;
                    }
                }

                for (int j = 0; j < repetition; j++) {

                    if (repeated)
                        // extract new s
                        s = s_vals.get(j);

                    try {
                        switch (primType) {

                        case INT32:
                            grp.append(colName, new Double(Double.parseDouble(s)).intValue());
                            break;
                        case INT64:
                        case INT96:
                            grp.append(colName, new Double(Double.parseDouble(s)).longValue());
                            break;
                        case DOUBLE:
                            grp.append(colName, Double.parseDouble(s));
                            break;
                        case FLOAT:
                            grp.append(colName, Float.parseFloat(s));
                            break;
                        case BOOLEAN:
                            grp.append(colName, s.equalsIgnoreCase("true") || s.equals("1"));
                            break;
                        case BINARY:
                            grp.append(colName, Binary.fromString(s));
                            break;
                        default:
                            throw new RuntimeException("Can't handle type " + primType);
                        }
                    } catch (NumberFormatException e) {

                        grp.append(colName, 0);
                    }
                }
            }
        }

        realWriter.write(null, (SimpleGroup) grp);

    } catch (InterruptedException e) {
        Thread.interrupted();
        throw new IOException(e);
    } catch (Exception e) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(out));
        throw new RuntimeException("Failed on record " + grp + ", schema=" + schema + ", path action="
                + recorder + " exception = " + e.getClass() + ", msg=" + e.getMessage() + ", cause="
                + e.getCause() + ", trace=" + out.toString());
    }

}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.ReversedLinesFileReaderTestParamFile.java

@Test
public void testDataIntegrityWithBufferedReader() throws URISyntaxException, IOException {
    File testFileIso = createFile(folder.newFile(), data);
    reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, buffSize, encoding);

    Stack<String> lineStack = new Stack<String>();

    bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(testFileIso), encoding));
    String line;/*from  w w  w .j av  a2s  .  c  o m*/

    // read all lines in normal order
    while ((line = bufferedReader.readLine()) != null) {
        lineStack.push(line);
    }

    // read in reverse order and compare with lines from stack
    while ((line = reversedLinesFileReader.readLine()) != null) {
        String lineFromBufferedReader = lineStack.pop();
        assertEquals(lineFromBufferedReader, line);
    }

}

From source file:fr.ritaly.dungeonmaster.item.ItemManager.java

@Override
public synchronized void addItem(Item item, Sector sector) {
    Validate.notNull(item, "The given item is null");
    Validate.notNull(sector, "The given sector is null");

    if (items == null) {
        items = new EnumMap<Sector, Stack<Item>>(Sector.class);
    }/*from  ww w .  j a v  a2  s. c o m*/

    Stack<Item> stack = items.get(sector);

    if (stack == null) {
        items.put(sector, stack = new Stack<Item>());
    }

    stack.push(item);

    fireItemAddedEvent(item, sector);
}

From source file:com.scaleunlimited.cascading.DistCp.java

/** Delete the dst files/dirs which do not exist in src */
static private void deleteNonexisting(FileSystem dstfs, FileStatus dstroot, Path dstsorted, FileSystem jobfs,
        Path jobdir, JobConf jobconf, Configuration conf) throws IOException {
    if (!dstroot.isDir()) {
        throw new IOException("dst must be a directory when option " + Options.DELETE.cmd
                + " is set, but dst (= " + dstroot.getPath() + ") is not a directory.");
    }/*from w w w.j a  va 2s . com*/

    //write dst lsr results
    final Path dstlsr = new Path(jobdir, "_distcp_dst_lsr");
    final SequenceFile.Writer writer = SequenceFile.createWriter(jobfs, jobconf, dstlsr, Text.class,
            FileStatus.class, SequenceFile.CompressionType.NONE);
    try {
        //do lsr to get all file statuses in dstroot
        final Stack<FileStatus> lsrstack = new Stack<FileStatus>();
        for (lsrstack.push(dstroot); !lsrstack.isEmpty();) {
            final FileStatus status = lsrstack.pop();
            if (status.isDir()) {
                for (FileStatus child : dstfs.listStatus(status.getPath())) {
                    String relative = makeRelative(dstroot.getPath(), child.getPath());
                    writer.append(new Text(relative), child);
                    lsrstack.push(child);
                }
            }
        }
    } finally {
        checkAndClose(writer);
    }

    //sort lsr results
    final Path sortedlsr = new Path(jobdir, "_distcp_dst_lsr_sorted");
    SequenceFile.Sorter sorter = new SequenceFile.Sorter(jobfs, new Text.Comparator(), Text.class,
            FileStatus.class, jobconf);
    sorter.sort(dstlsr, sortedlsr);

    //compare lsr list and dst list  
    SequenceFile.Reader lsrin = null;
    SequenceFile.Reader dstin = null;
    try {
        lsrin = new SequenceFile.Reader(jobfs, sortedlsr, jobconf);
        dstin = new SequenceFile.Reader(jobfs, dstsorted, jobconf);

        //compare sorted lsr list and sorted dst list
        final Text lsrpath = new Text();
        final FileStatus lsrstatus = new FileStatus();
        final Text dstpath = new Text();
        final Text dstfrom = new Text();
        final FsShell shell = new FsShell(conf);
        final String[] shellargs = { "-rmr", null };

        boolean hasnext = dstin.next(dstpath, dstfrom);
        for (; lsrin.next(lsrpath, lsrstatus);) {
            int dst_cmp_lsr = dstpath.compareTo(lsrpath);
            for (; hasnext && dst_cmp_lsr < 0;) {
                hasnext = dstin.next(dstpath, dstfrom);
                dst_cmp_lsr = dstpath.compareTo(lsrpath);
            }

            if (dst_cmp_lsr == 0) {
                //lsrpath exists in dst, skip it
                hasnext = dstin.next(dstpath, dstfrom);
            } else {
                //lsrpath does not exist, delete it
                String s = new Path(dstroot.getPath(), lsrpath.toString()).toString();
                if (shellargs[1] == null || !isAncestorPath(shellargs[1], s)) {
                    shellargs[1] = s;
                    int r = 0;
                    try {
                        r = shell.run(shellargs);
                    } catch (Exception e) {
                        throw new IOException("Exception from shell.", e);
                    }
                    if (r != 0) {
                        throw new IOException(
                                "\"" + shellargs[0] + " " + shellargs[1] + "\" returns non-zero value " + r);
                    }
                }
            }
        }
    } finally {
        checkAndClose(lsrin);
        checkAndClose(dstin);
    }
}

From source file:com.pinterest.hdfsbackup.distcp.DistCp.java

/**
 * Initialize DFSCopyFileMapper specific job-configuration.
 * @param conf : The dfs/mapred configuration.
 * @param jobConf : The handle to the jobConf object to be initialized.
 * @param args Arguments//from w ww . ja  v a 2 s .  c  o m
 */
private static void setup(Configuration conf, JobConf jobConf, final Arguments args) throws IOException {
    jobConf.set(DST_DIR_LABEL, args.dst.toUri().toString());

    //set boolean values
    final boolean update = args.flags.contains(Options.UPDATE);
    final boolean overwrite = !update && args.flags.contains(Options.OVERWRITE);
    jobConf.setBoolean(Options.UPDATE.propertyname, update);
    jobConf.setBoolean(Options.OVERWRITE.propertyname, overwrite);
    jobConf.setBoolean(Options.IGNORE_READ_FAILURES.propertyname,
            args.flags.contains(Options.IGNORE_READ_FAILURES));
    jobConf.setBoolean(Options.PRESERVE_STATUS.propertyname, args.flags.contains(Options.PRESERVE_STATUS));

    final String randomId = getRandomId();
    JobClient jClient = new JobClient(jobConf);
    Path jobDirectory = new Path(jClient.getSystemDir(), NAME + "_" + randomId);
    jobConf.set(JOB_DIR_LABEL, jobDirectory.toString());

    FileSystem dstfs = args.dst.getFileSystem(conf);
    boolean dstExists = dstfs.exists(args.dst);
    boolean dstIsDir = false;
    if (dstExists) {
        dstIsDir = dstfs.getFileStatus(args.dst).isDir();
    }

    // default logPath
    Path logPath = args.log;
    if (logPath == null) {
        String filename = "_distcp_logs_" + randomId;
        if (!dstExists || !dstIsDir) {
            Path parent = args.dst.getParent();
            if (!dstfs.exists(parent)) {
                dstfs.mkdirs(parent);
            }
            logPath = new Path(parent, filename);
        } else {
            logPath = new Path(args.dst, filename);
        }
    }
    FileOutputFormat.setOutputPath(jobConf, logPath);

    // create src list, dst list
    FileSystem jobfs = jobDirectory.getFileSystem(jobConf);

    Path srcfilelist = new Path(jobDirectory, "_distcp_src_files");
    jobConf.set(SRC_LIST_LABEL, srcfilelist.toString());
    SequenceFile.Writer src_writer = SequenceFile.createWriter(jobfs, jobConf, srcfilelist, LongWritable.class,
            FilePair.class, SequenceFile.CompressionType.NONE);

    Path dstfilelist = new Path(jobDirectory, "_distcp_dst_files");
    SequenceFile.Writer dst_writer = SequenceFile.createWriter(jobfs, jobConf, dstfilelist, Text.class,
            Text.class, SequenceFile.CompressionType.NONE);

    Path dstdirlist = new Path(jobDirectory, "_distcp_dst_dirs");
    jobConf.set(DST_DIR_LIST_LABEL, dstdirlist.toString());
    SequenceFile.Writer dir_writer = SequenceFile.createWriter(jobfs, jobConf, dstdirlist, Text.class,
            FilePair.class, SequenceFile.CompressionType.NONE);

    // handle the case where the destination directory doesn't exist
    // and we've only a single src directory OR we're updating/overwriting
    // the contents of the destination directory.
    final boolean special = (args.srcs.size() == 1 && !dstExists) || update || overwrite;
    int srcCount = 0, cnsyncf = 0, dirsyn = 0;
    long fileCount = 0L, byteCount = 0L, cbsyncs = 0L;
    try {
        for (Iterator<Path> srcItr = args.srcs.iterator(); srcItr.hasNext();) {
            final Path src = srcItr.next();
            FileSystem srcfs = src.getFileSystem(conf);
            FileStatus srcfilestat = srcfs.getFileStatus(src);
            Path root = special && srcfilestat.isDir() ? src : src.getParent();
            if (srcfilestat.isDir()) {
                ++srcCount;
            }

            Stack<FileStatus> pathstack = new Stack<FileStatus>();
            for (pathstack.push(srcfilestat); !pathstack.empty();) {
                FileStatus cur = pathstack.pop();
                FileStatus[] children = srcfs.listStatus(cur.getPath());
                for (int i = 0; i < children.length; i++) {
                    boolean skipfile = false;
                    final FileStatus child = children[i];
                    final String dst = makeRelative(root, child.getPath());
                    ++srcCount;

                    if (child.isDir()) {
                        pathstack.push(child);
                    } else {
                        //skip file if the src and the dst files are the same.
                        skipfile = update && sameFile(srcfs, child, dstfs, new Path(args.dst, dst));
                        //skip file if it exceed file limit or size limit
                        skipfile |= fileCount == args.filelimit || byteCount + child.getLen() > args.sizelimit;

                        if (!skipfile) {
                            ++fileCount;
                            byteCount += child.getLen();

                            if (LOG.isTraceEnabled()) {
                                LOG.trace("adding file " + child.getPath());
                            }

                            ++cnsyncf;
                            cbsyncs += child.getLen();
                            if (cnsyncf > SYNC_FILE_MAX || cbsyncs > BYTES_PER_MAP) {
                                src_writer.sync();
                                dst_writer.sync();
                                cnsyncf = 0;
                                cbsyncs = 0L;
                            }
                        }
                    }

                    if (!skipfile) {
                        src_writer.append(new LongWritable(child.isDir() ? 0 : child.getLen()),
                                new FilePair(child, dst));
                    }

                    dst_writer.append(new Text(dst), new Text(child.getPath().toString()));
                }

                if (cur.isDir()) {
                    String dst = makeRelative(root, cur.getPath());
                    dir_writer.append(new Text(dst), new FilePair(cur, dst));
                    if (++dirsyn > SYNC_FILE_MAX) {
                        dirsyn = 0;
                        dir_writer.sync();
                    }
                }
            }
        }
    } finally {
        checkAndClose(src_writer);
        checkAndClose(dst_writer);
        checkAndClose(dir_writer);
    }

    FileStatus dststatus = null;
    try {
        dststatus = dstfs.getFileStatus(args.dst);
    } catch (FileNotFoundException fnfe) {
        LOG.info(args.dst + " does not exist.");
    }

    // create dest path dir if copying > 1 file
    if (dststatus == null) {
        if (srcCount > 1 && !dstfs.mkdirs(args.dst)) {
            throw new IOException("Failed to create" + args.dst);
        }
    }

    final Path sorted = new Path(jobDirectory, "_distcp_sorted");
    checkDuplication(jobfs, dstfilelist, sorted, conf);

    if (dststatus != null && args.flags.contains(Options.DELETE)) {
        deleteNonexisting(dstfs, dststatus, sorted, jobfs, jobDirectory, jobConf, conf);
    }

    Path tmpDir = new Path(
            (dstExists && !dstIsDir) || (!dstExists && srcCount == 1) ? args.dst.getParent() : args.dst,
            "_distcp_tmp_" + randomId);
    jobConf.set(TMP_DIR_LABEL, tmpDir.toUri().toString());
    LOG.info("srcCount=" + srcCount);
    jobConf.setInt(SRC_COUNT_LABEL, srcCount);
    jobConf.setLong(TOTAL_SIZE_LABEL, byteCount);
    setMapCount(byteCount, jobConf);
}

From source file:ch.zhaw.icclab.tnova.expressionsolver.OTFlyEval.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/*from  w  w w.jav a  2  s.c  o m*/
@Produces(MediaType.APPLICATION_JSON)
public Response evalOnTheFly(String incomingMsg) {
    JSONObject incoming = (JSONObject) JSONValue.parse(incomingMsg);
    JSONObject outgoing = new JSONObject();
    String result = "";
    try {
        String expression = (String) incoming.get("exp");
        JSONArray vals = (JSONArray) incoming.get("values");
        Stack<Double> param = new Stack<Double>();
        for (int i = 0; i < vals.size(); i++) {
            Double val = new Double((String) vals.get(i));
            param.push(val);
        }
        double threshold = Double.parseDouble((String) incoming.get("threshold"));
        result = evaluateExpression(expression, param, threshold);
    } catch (Exception ex) {
        if (App.showExceptions)
            ex.printStackTrace();
    }
    logger.info("received expression: " + incoming.get("exp"));
    logger.info("expression evaluation result: " + result);
    //construct proper JSON response.
    outgoing.put("info", "t-nova expression evaluation service");
    if (result != null && result.length() != 0) {
        outgoing.put("result", result);
        outgoing.put("status", "ok");
        KPI.expressions_evaluated += 1;
        KPI.api_calls_success += 1;
        return Response.ok(outgoing.toJSONString(), MediaType.APPLICATION_JSON_TYPE).build();
    } else {
        outgoing.put("status", "execution failed");
        outgoing.put("msg",
                "malformed request parameters, check your expression or parameter list for correctness.");
        KPI.expressions_evaluated += 1;
        KPI.api_calls_failed += 1;
        return Response.status(Response.Status.BAD_REQUEST).entity(outgoing.toJSONString())
                .encoding(MediaType.APPLICATION_JSON).build();
    }
}

From source file:com.ms.commons.test.math.expression.util.MathExpressionParseUtil.java

private static Stack<String> parseTokenStack(String expr) throws MathParseException {
    Stack<String> expressionStack = new Stack<String>();
    StringBuilder lastTokens = new StringBuilder();
    char[] cs = expr.toCharArray();
    boolean isLastBracketsBlock = false;
    int fromIndex = 0;
    int endIndex = expr.length() - 1;
    for (int i = fromIndex; i <= endIndex; i++) {
        char c = cs[i];
        switch (c) {
        case '(':
            if (isLastBracketsBlock) {
                throw new MathParseException("Error token '(' or ')' useage in '" + expr + "'.");
            }/*from w  w w  . ja  v a2s  .  c o m*/
            int nextCloseBracket = findNextCloseBracket(cs, (i + 1), endIndex);
            if (nextCloseBracket == -1) {
                throw new MathParseException("Token '(' or ')' not paried in '" + expr + "'.");
            }
            for (int x = i + 1; x < nextCloseBracket; x++) {
                lastTokens.append(cs[x]);
            }
            i = nextCloseBracket;
            isLastBracketsBlock = true;
            break;
        case '+':
        case '-':
        case '*':
        case '/':
            checkIndex(i, fromIndex, endIndex, cs, lastTokens);
            expressionStack.push(lastTokens.toString());
            expressionStack.push(String.valueOf(c));
            lastTokens = new StringBuilder();
            isLastBracketsBlock = false;
            break;
        default:
            if (c == ')') {
                throw new MathParseException("Error token ')' in '" + expr + "'.");
            }
            if (isLastBracketsBlock) {
                throw new MathParseException("Error token '(' or ')' useage in '" + expr + "'.");
            }
            // check more ilgeal char ?
            lastTokens.append(c);
            break;
        }
    }
    if (lastTokens.length() > 0) {
        expressionStack.add(lastTokens.toString());
    }
    return expressionStack;
}