1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.rowset.filteredrowset;
  24 
  25 import java.sql.SQLException;
  26 import javax.sql.RowSet;
  27 import javax.sql.rowset.Predicate;
  28 
  29 /*
  30  * Simple implementation of Predicate which is used to filter rows based
  31  * on a City.
  32  */
  33 public class CityFilter implements Predicate {
  34 
  35     private final String[] cities;
  36     private String colName = null;
  37     private int colNumber = -1;
  38 
  39     public CityFilter(String[] cities, String colName) {
  40         this.cities = cities;
  41         this.colName = colName;
  42     }
  43 
  44     public CityFilter(String[] cities, int colNumber) {
  45         this.cities = cities;
  46         this.colNumber = colNumber;
  47     }
  48 
  49     public boolean evaluate(Object value, String colName) {
  50 
  51         if (colName.equalsIgnoreCase(this.colName)) {
  52             for (String city : cities) {
  53                 if (city.equalsIgnoreCase((String) value)) {
  54                     return true;
  55                 }
  56             }
  57         }
  58         return false;
  59     }
  60 
  61     public boolean evaluate(Object value, int colNumber) {
  62 
  63         if (colNumber == this.colNumber) {
  64             for (String city : this.cities) {
  65                 if (city.equalsIgnoreCase((String) value)) {
  66                     return true;
  67                 }
  68             }
  69         }
  70         return false;
  71     }
  72 
  73     public boolean evaluate(RowSet rs) {
  74 
  75         boolean result = false;
  76 
  77         if (rs == null) {
  78             return false;
  79         }
  80 
  81         try {
  82             for (String city : cities) {
  83 
  84                 String val = "";
  85                 if (colNumber > 0) {
  86                     val = (String) rs.getObject(colNumber);
  87                 } else if (colName != null) {
  88                     val = (String) rs.getObject(colName);
  89                 }
  90 
  91                 if (val.equalsIgnoreCase(city)) {
  92                     return true;
  93                 }
  94             }
  95         } catch (SQLException e) {
  96             result = false;
  97         }
  98         return result;
  99     }
 100 }