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 javax.sql.RowSet;
  26 import javax.sql.rowset.Predicate;
  27 
  28 /*
  29  * Simple implementation of Predicate which is used to filter rows based
  30  * on the Primary Key.
  31  */
  32 public class PrimaryKeyFilter implements Predicate {
  33 
  34     private final int lo;
  35     private final int hi;
  36     private String colName = null;
  37     private int colNumber = -1;
  38 
  39     public PrimaryKeyFilter(int lo, int hi, int colNumber) {
  40         this.lo = lo;
  41         this.hi = hi;
  42         this.colNumber = colNumber;
  43     }
  44 
  45     public PrimaryKeyFilter(int lo, int hi, String colName) {
  46         this.lo = lo;
  47         this.hi = hi;
  48         this.colName = colName;
  49     }
  50 
  51     public boolean evaluate(Object value, String columnName) {
  52 
  53         boolean result = false;
  54         if (columnName.equalsIgnoreCase(this.colName)) {
  55             int columnValue = ((Integer) value);
  56             result = (columnValue >= this.lo) && (columnValue <= this.hi);
  57         }
  58         return result;
  59     }
  60 
  61     public boolean evaluate(Object value, int columnNumber) {
  62 
  63         boolean result = false;
  64         if (this.colNumber == columnNumber) {
  65             int columnValue = (Integer) value;
  66             result = (columnValue >= this.lo) && (columnValue <= this.hi);
  67         }
  68         return result;
  69     }
  70 
  71     public boolean evaluate(RowSet rs) {
  72 
  73         boolean result = false;
  74         try {
  75             int columnValue = -1;
  76 
  77             if (this.colNumber > 0) {
  78                 columnValue = rs.getInt(this.colNumber);
  79             } else if (this.colName != null) {
  80                 columnValue = rs.getInt(this.colName);
  81             }
  82             if ((columnValue >= this.lo) && (columnValue <= this.hi)) {
  83                 result = true;
  84             }
  85 
  86         } catch (Exception e) {
  87             System.out.println("Error:" + e.getMessage());
  88             result = false;
  89         }
  90         return result;
  91     }
  92 
  93 }