Package eu.bitfish.jcf.parser

Source Code of eu.bitfish.jcf.parser.AbstractParser

/*
* This file is part of JCF.

* JCF 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.
*
* JCF 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 JCF.  If not, see <http://www.gnu.org/licenses/>.
*/
package eu.bitfish.jcf.parser;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import eu.bitfish.jcf.common.config.ConfigManager;
import eu.bitfish.jcf.common.expression.AbstractExpression;
import eu.bitfish.jcf.parser.exception.ParsingException;

/**
* Abstract super class for all parser implementations.
*
* @author Michael Sieber
*
*/
public abstract class AbstractParser {
  private final LoadingCache<String, AbstractExpression> QUERY_CACHE;

  {
    long maxSize = ConfigManager.getConfig().getLong(
        ConfigManager.PARSER_CACHE_MAX_SIZE);
    long expireAfterAccess = ConfigManager.getConfig().getLong(
        ConfigManager.PARSER_CACHE_EXPIRE_AFTER_ACCESS);

    QUERY_CACHE = CacheBuilder.newBuilder().maximumSize(maxSize)
        .expireAfterAccess(expireAfterAccess, TimeUnit.MINUTES)
        .build(new CacheLoader<String, AbstractExpression>() {

          @Override
          public AbstractExpression load(String query) throws Exception {
            return doParse(query);
          }

        });
  }

  /**
   * Parse a query. For performance reasons short-curcuit evaluation of
   * boolean expressions will be used. The parser will always evaluate 'and'
   * operations before 'or' operations as mentioned in the java documentation
   * for operator precedence
   * (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)
   *
   * @param query
   *            The query to parse
   * @return The expression tree for further processing
   * @throws ParsingException
   *             Thrown on any errors during parsing
   */
  public AbstractExpression parse(String query) throws ParsingException {
    try {
      return QUERY_CACHE.get(query);
    } catch (ExecutionException e) {
      throw new ParsingException(e);
    }
  }

  /**
   * Parse the query.
   *
   * @param query
   *            The query to parse
   * @return The Expression tree for further processing
   * @throws ParsingException
   *             Thrown on any errors during parsing
   */
  protected abstract AbstractExpression doParse(String query) throws ParsingException;
}
TOP

Related Classes of eu.bitfish.jcf.parser.AbstractParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.