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