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 
  24 /**
  25  * @test
  26  * @modules java.sql.rowset/com.sun.rowset
  27  *          java.sql.rowset/com.sun.rowset.internal
  28  *          java.sql.rowset/com.sun.rowset.providers
  29  */
  30 
  31 package test.rowset.filteredrowset;
  32 
  33 import java.sql.SQLException;
  34 import javax.sql.RowSet;
  35 import javax.sql.rowset.Predicate;
  36 
  37 /*
  38  * Simple implementation of Predicate which is used to filter rows based
  39  * on a City.
  40  */
  41 public class CityFilter implements Predicate {
  42 
  43     private final String[] cities;
  44     private String colName = null;
  45     private int colNumber = -1;
  46 
  47     public CityFilter(String[] cities, String colName) {
  48         this.cities = cities;
  49         this.colName = colName;
  50     }
  51 
  52     public CityFilter(String[] cities, int colNumber) {
  53         this.cities = cities;
  54         this.colNumber = colNumber;
  55     }
  56 
  57     public boolean evaluate(Object value, String colName) {
  58 
  59         if (colName.equalsIgnoreCase(this.colName)) {
  60             for (String city : cities) {
  61                 if (city.equalsIgnoreCase((String) value)) {
  62                     return true;
  63                 }
  64             }
  65         }
  66         return false;
  67     }
  68 
  69     public boolean evaluate(Object value, int colNumber) {
  70 
  71         if (colNumber == this.colNumber) {
  72             for (String city : this.cities) {
  73                 if (city.equalsIgnoreCase((String) value)) {
  74                     return true;
  75                 }
  76             }
  77         }
  78         return false;
  79     }
  80 
  81     public boolean evaluate(RowSet rs) {
  82 
  83         boolean result = false;
  84 
  85         if (rs == null) {
  86             return false;
  87         }
  88 
  89         try {
  90             for (String city : cities) {
  91 
  92                 String val = "";
  93                 if (colNumber > 0) {
  94                     val = (String) rs.getObject(colNumber);
  95                 } else if (colName != null) {
  96                     val = (String) rs.getObject(colName);
  97                 }
  98 
  99                 if (val.equalsIgnoreCase(city)) {
 100                     return true;
 101                 }
 102             }
 103         } catch (SQLException e) {
 104             result = false;
 105         }
 106         return result;
 107     }
 108 }