Package eu.bitfish.jcf.processor

Source Code of eu.bitfish.jcf.processor.DefaultProcessor

/*
* 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.processor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import eu.bitfish.jcf.common.expression.AbstractExpression;
import eu.bitfish.jcf.parser.exception.ParsingException;
import eu.bitfish.jcf.processor.exception.FilterException;

/**
* Default processor implementation.
*
* @author Michael Sieber
*
*/
public class DefaultProcessor extends AbstractProcessor {

  @Override
  public boolean validate(final String query) {
    try {
      getParser().parse(query);
      return true;
    } catch (Exception e) {
      return false;
    }
  }

  @Override
  public <T> List<T> execute(final Collection<T> collection,
      final String query) throws ParsingException, FilterException {
    AbstractExpression ex = getParser().parse(query);

    // TODO allow all whitespaces in value!!!

    final List<T> filtered = new ArrayList<>();
    for (T obj : collection) {
      if (ex.evaluate(obj)) {
        filtered.add(obj);

        // if the order is set we have to add all items before limiting
        // the result
        if (ex.getLimit() != -1 && ex.getOrder() == null
            && ex.getLimit() == filtered.size()) {
          break;
        }
      }
    }

    if (ex.getOrder() != null) {
      Collections.sort(filtered, ex.getOrder());

      if (ex.getLimit() != -1) {
        return filtered.subList(0, ex.getLimit());
      }
    }

    return filtered;
  }
}
TOP

Related Classes of eu.bitfish.jcf.processor.DefaultProcessor

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.