001package org.cpsolver.studentsct.filter;
002
003import org.cpsolver.studentsct.model.Student;
004
005/**
006 * This student filter combines two given student filters with logical operation
007 * AND or OR.
008 * 
009 * @version StudentSct 1.3 (Student Sectioning)<br>
010 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
011 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
012 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
013 * <br>
014 *          This library is free software; you can redistribute it and/or modify
015 *          it under the terms of the GNU Lesser General Public License as
016 *          published by the Free Software Foundation; either version 3 of the
017 *          License, or (at your option) any later version. <br>
018 * <br>
019 *          This library is distributed in the hope that it will be useful, but
020 *          WITHOUT ANY WARRANTY; without even the implied warranty of
021 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022 *          Lesser General Public License for more details. <br>
023 * <br>
024 *          You should have received a copy of the GNU Lesser General Public
025 *          License along with this library; if not see
026 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
027 */
028public class CombinedStudentFilter implements StudentFilter {
029    /** AND */
030    public static final int OP_AND = 0;
031    /** OR */
032    public static final int OP_OR = 1;
033    private StudentFilter iFirst, iSecond;
034    private int iOp;
035
036    /**
037     * Constructor
038     * 
039     * @param first
040     *            first filter
041     * @param second
042     *            second filter
043     * @param op
044     *            logical operation (either {@link CombinedStudentFilter#OP_AND}
045     *            or {@link CombinedStudentFilter#OP_OR} )
046     */
047    public CombinedStudentFilter(StudentFilter first, StudentFilter second, int op) {
048        iFirst = first;
049        iSecond = second;
050        iOp = op;
051    }
052
053    /**
054     * A student is accepted if it is accepted by the first and/or the second
055     * filter
056     */
057    @Override
058    public boolean accept(Student student) {
059        switch (iOp) {
060            case OP_OR:
061                return iFirst.accept(student) || iSecond.accept(student);
062            case OP_AND:
063            default:
064                return iFirst.accept(student) && iSecond.accept(student);
065        }
066    }
067
068    @Override
069    public String getName() {
070        switch (iOp) {
071            case OP_OR:
072                return iFirst.getName() + " OR " + iSecond.getName();
073            case OP_AND:
074            default:
075                return iFirst.getName() + " AND " + iSecond.getName();
076        }
077    }
078}