1 /*
   2  * Copyright (c) 2003, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sql.rowset;
  27 
  28 import javax.sql.*;
  29 import java.sql.*;
  30 
  31 /**
  32  * The standard interface that provides the framework for all
  33  * <code>FilteredRowSet</code> objects to describe their filters.
  34  *
  35  * <h3>1.0 Background</h3>
  36  * The <code>Predicate</code> interface is a standard interface that
  37  * applications can implement to define the filter they wish to apply to a
  38  * a <code>FilteredRowSet</code> object. A <code>FilteredRowSet</code>
  39  * object consumes implementations of this interface and enforces the
  40  * constraints defined in the implementation of the method <code>evaluate</code>.
  41  * A <code>FilteredRowSet</code> object enforces the filter constraints in a
  42  * bi-directional manner: It outputs only rows that are within
  43  * the constraints of the filter; and conversely, it inserts, modifies, or updates
  44  * only rows that are within the constraints of the filter.
  45  *
  46  * <h3>2.0 Implementation Guidelines</h3>
  47  * In order to supply a predicate for the <code>FilteredRowSet</code>.
  48  * this interface must be implemented.  At this time, the JDBC RowSet
  49  * Implementations (JSR-114) does not specify any standard filters definitions.
  50  * By specifying a standard means and mechanism for a range of filters to be
  51  * defined and deployed with both the reference and vendor implementations
  52  * of the <code>FilteredRowSet</code> interface, this allows for a flexible
  53  * and application motivated implementations of <code>Predicate</code> to emerge.
  54  * <p>
  55  * A sample implementation would look something like this:
  56  * <pre>{@code
  57  *    public class Range implements Predicate {
  58  *
  59  *       private int[] lo;
  60  *       private int[] hi;
  61  *       private int[] idx;
  62  *
  63  *       public Range(int[] lo, int[] hi, int[] idx) {
  64  *          this.lo = lo;
  65  *          this.hi = hi;
  66  *          this.idx = idx;
  67  *       }
  68  *
  69  *      public boolean evaluate(RowSet rs) {
  70  *
  71  *          // Check the present row determine if it lies
  72  *          // within the filtering criteria.
  73  *
  74  *          for (int i = 0; i < idx.length; i++) {
  75  *             int value;
  76  *             try {
  77  *                 value = (Integer) rs.getObject(idx[i]);
  78  *             } catch (SQLException ex) {
  79  *                 Logger.getLogger(Range.class.getName()).log(Level.SEVERE, null, ex);
  80  *                 return false;
  81  *             }
  82  *
  83  *             if (value < lo[i] && value > hi[i]) {
  84  *                 // outside of filter constraints
  85  *                 return false;
  86  *             }
  87  *         }
  88  *         // Within filter constraints
  89  *        return true;
  90  *      }
  91  *   }
  92  * }</pre>
  93  * <P>
  94  * The example above implements a simple range predicate. Note, that
  95  * implementations should but are not required to provide <code>String</code>
  96  * and integer index based constructors to provide for JDBC RowSet Implementation
  97  * applications that use both column identification conventions.
  98  *
  99  * @author Jonathan Bruce, Amit Handa
 100  * @since 1.5
 101  *
 102  */
 103 
 104  // <h3>3.0 FilteredRowSet Internals</h3>
 105  // internalNext, Frist, Last. Discuss guidelines on how to approach this
 106  // and cite examples in reference implementations.
 107 public interface Predicate {
 108     /**
 109      * This method is typically called a <code>FilteredRowSet</code> object
 110      * internal methods (not public) that control the <code>RowSet</code> object's
 111      * cursor moving  from row to the next. In addition, if this internal method
 112      * moves the cursor onto a row that has been deleted, the internal method will
 113      * continue to ove the cursor until a valid row is found.
 114      * @param rs The {@code RowSet} to be evaluated
 115      * @return <code>true</code> if there are more rows in the filter;
 116      *     <code>false</code> otherwise
 117      */
 118     public boolean evaluate(RowSet rs);
 119 
 120 
 121     /**
 122      * This method is called by a <code>FilteredRowSet</code> object
 123      * to check whether the value lies between the filtering criterion (or criteria
 124      * if multiple constraints exist) set using the <code>setFilter()</code> method.
 125      * <P>
 126      * The <code>FilteredRowSet</code> object will use this method internally
 127      * while inserting new rows to a <code>FilteredRowSet</code> instance.
 128      *
 129      * @param value An <code>Object</code> value which needs to be checked,
 130      *        whether it can be part of this <code>FilterRowSet</code> object.
 131      * @param column a <code>int</code> object that must match the
 132      *        SQL index of a column in this <code>RowSet</code> object. This must
 133      *        have been passed to <code>Predicate</code> as one of the columns
 134      *        for filtering while initializing a <code>Predicate</code>
 135      * @return <code>true</code> if row value lies within the filter;
 136      *     <code>false</code> otherwise
 137      * @throws SQLException if the column is not part of filtering criteria
 138      */
 139     public boolean evaluate(Object value, int column) throws SQLException;
 140 
 141     /**
 142      * This method is called by the <code>FilteredRowSet</code> object
 143      * to check whether the value lies between the filtering criteria set
 144      * using the setFilter method.
 145      * <P>
 146      * The <code>FilteredRowSet</code> object will use this method internally
 147      * while inserting new rows to a <code>FilteredRowSet</code> instance.
 148      *
 149      * @param value An <code>Object</code> value which needs to be checked,
 150      * whether it can be part of this <code>FilterRowSet</code>.
 151      *
 152      * @param columnName a <code>String</code> object that must match the
 153      *        SQL name of a column in this <code>RowSet</code>, ignoring case. This must
 154      *        have been passed to <code>Predicate</code> as one of the columns for filtering
 155      *        while initializing a <code>Predicate</code>
 156      *
 157      * @return <code>true</code> if value lies within the filter; <code>false</code> otherwise
 158      *
 159      * @throws SQLException if the column is not part of filtering criteria
 160      */
 161     public boolean evaluate(Object value, String columnName) throws SQLException;
 162 
 163 }