Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/Predicate.java
40948 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset;2627import javax.sql.*;28import java.sql.*;2930/**31* The standard interface that provides the framework for all32* <code>FilteredRowSet</code> objects to describe their filters.33*34* <h2>1.0 Background</h2>35* The <code>Predicate</code> interface is a standard interface that36* applications can implement to define the filter they wish to apply to a37* a <code>FilteredRowSet</code> object. A <code>FilteredRowSet</code>38* object consumes implementations of this interface and enforces the39* constraints defined in the implementation of the method <code>evaluate</code>.40* A <code>FilteredRowSet</code> object enforces the filter constraints in a41* bi-directional manner: It outputs only rows that are within42* the constraints of the filter; and conversely, it inserts, modifies, or updates43* only rows that are within the constraints of the filter.44*45* <h2>2.0 Implementation Guidelines</h2>46* In order to supply a predicate for the <code>FilteredRowSet</code>.47* this interface must be implemented. At this time, the JDBC RowSet48* Implementations (JSR-114) does not specify any standard filters definitions.49* By specifying a standard means and mechanism for a range of filters to be50* defined and deployed with both the reference and vendor implementations51* of the <code>FilteredRowSet</code> interface, this allows for a flexible52* and application motivated implementations of <code>Predicate</code> to emerge.53* <p>54* A sample implementation would look something like this:55* <pre>{@code56* public class Range implements Predicate {57*58* private int[] lo;59* private int[] hi;60* private int[] idx;61*62* public Range(int[] lo, int[] hi, int[] idx) {63* this.lo = lo;64* this.hi = hi;65* this.idx = idx;66* }67*68* public boolean evaluate(RowSet rs) {69*70* // Check the present row determine if it lies71* // within the filtering criteria.72*73* for (int i = 0; i < idx.length; i++) {74* int value;75* try {76* value = (Integer) rs.getObject(idx[i]);77* } catch (SQLException ex) {78* Logger.getLogger(Range.class.getName()).log(Level.SEVERE, null, ex);79* return false;80* }81*82* if (value < lo[i] && value > hi[i]) {83* // outside of filter constraints84* return false;85* }86* }87* // Within filter constraints88* return true;89* }90* }91* }</pre>92* <P>93* The example above implements a simple range predicate. Note, that94* implementations should but are not required to provide <code>String</code>95* and integer index based constructors to provide for JDBC RowSet Implementation96* applications that use both column identification conventions.97*98* @author Jonathan Bruce, Amit Handa99* @since 1.5100*101*/102103// <h2>3.0 FilteredRowSet Internals</h2>104// internalNext, First, Last. Discuss guidelines on how to approach this105// and cite examples in reference implementations.106public interface Predicate {107/**108* This method is typically called a <code>FilteredRowSet</code> object109* internal methods (not public) that control the <code>RowSet</code> object's110* cursor moving from row to the next. In addition, if this internal method111* moves the cursor onto a row that has been deleted, the internal method will112* continue to ove the cursor until a valid row is found.113* @param rs The {@code RowSet} to be evaluated114* @return <code>true</code> if there are more rows in the filter;115* <code>false</code> otherwise116*/117public boolean evaluate(RowSet rs);118119120/**121* This method is called by a <code>FilteredRowSet</code> object122* to check whether the value lies between the filtering criterion (or criteria123* if multiple constraints exist) set using the <code>setFilter()</code> method.124* <P>125* The <code>FilteredRowSet</code> object will use this method internally126* while inserting new rows to a <code>FilteredRowSet</code> instance.127*128* @param value An <code>Object</code> value which needs to be checked,129* whether it can be part of this <code>FilterRowSet</code> object.130* @param column a <code>int</code> object that must match the131* SQL index of a column in this <code>RowSet</code> object. This must132* have been passed to <code>Predicate</code> as one of the columns133* for filtering while initializing a <code>Predicate</code>134* @return <code>true</code> if row value lies within the filter;135* <code>false</code> otherwise136* @throws SQLException if the column is not part of filtering criteria137*/138public boolean evaluate(Object value, int column) throws SQLException;139140/**141* This method is called by the <code>FilteredRowSet</code> object142* to check whether the value lies between the filtering criteria set143* using the setFilter method.144* <P>145* The <code>FilteredRowSet</code> object will use this method internally146* while inserting new rows to a <code>FilteredRowSet</code> instance.147*148* @param value An <code>Object</code> value which needs to be checked,149* whether it can be part of this <code>FilterRowSet</code>.150*151* @param columnName a <code>String</code> object that must match the152* SQL name of a column in this <code>RowSet</code>, ignoring case. This must153* have been passed to <code>Predicate</code> as one of the columns for filtering154* while initializing a <code>Predicate</code>155*156* @return <code>true</code> if value lies within the filter; <code>false</code> otherwise157*158* @throws SQLException if the column is not part of filtering criteria159*/160public boolean evaluate(Object value, String columnName) throws SQLException;161162}163164165