1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
4 This file is part of GORITE.
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GORITE 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 Lesser General Public
14 License for more details.
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE. If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
20 package com.intendico.data;
22 import java.util.Observer;
23 import java.util.Vector;
24 import java.util.Iterator;
27 * A utility class for including a boolean method in query
28 * processing. The typical use is as an inline extension that
29 * implements the {@link #condition} method.
31 abstract public class Condition implements Query {
34 * An always true Condition query.
36 public static Condition TRUE = new Condition() {
37 public boolean condition() {
43 * Control flag to mark whether the condition has been tested or
46 public boolean tested = false;
49 * Cache of initially unbound variables. These are captured on
50 * construction, just in case there is an extension that takes
51 * variables. In that case, the next() method on failing must
52 * restore the incoming unbound variables to be unbound.
54 public Vector variables;
57 * The {@link Query#reset} method implemented by noting the
58 * condition as not tested.
60 public void reset() throws Exception {
65 * The {@link Query#copy} method implemented by using this same
68 public Query copy(Vector/*<Ref>*/ newrefs) throws Exception {
73 * The {@link Query#next} method implemented by returning as per
74 * the {@link #condition} method on its first invocation, then
75 * returning false without invoking the {@link #condition} method,
76 * until {@link #reset()}. This method also captures any unbound
77 * variables (an extension class may have variables), and clears
78 * these before returning false.
80 public boolean next() throws Exception {
82 variables = getRefs( new Vector() );
83 if ( variables.size() > 0 ) {
84 for ( Iterator i = variables.iterator(); i.hasNext(); ) {
85 if ( ((Ref) i.next()).get() != null ) {
89 if ( variables.size() == 0 )
98 if ( variables != null ) {
99 for ( Iterator i = variables.iterator(); i.hasNext(); ) {
100 ((Ref) i.next()).clear();
107 * The {@link Query#getRefs} method implemented by adding
108 * nothing. An actual condition that uses {@link Ref} objects
109 * should override this method.
111 public Vector/*<Ref>*/ getRefs(Vector/*<Ref>*/ v) {
116 * The {@link Query#addObserver} method implemented by doing
117 * nothing. An actual condition that is triggering should override
118 * this method as well as {@link #deleteObserver}.
120 public void addObserver(Observer x) {
124 * The {@link Query#deleteObserver} method implemented by doing
125 * nothing. An actual condition that is triggering should override
126 * this method as well as {@link #addObserver}.
128 public void deleteObserver(Observer x) {
132 * The {@link Query#addable} method is implemented by a reset
135 public boolean addable() {
139 } catch (Exception e) {
146 * The {@link Query#add} method implemented by returning false. An
147 * actual condition that gets updated should override this method.
149 public boolean add() {
154 * Implements {@link Query#removable} by calling {@link #addable}
156 public boolean removable()
162 * Implements {@link Query#remove} by returning false;
164 public boolean remove() {
169 * The actual condition returning <em>true</em> or
170 * <em>false</em>. If it is appropriate to re-test the condition
171 * several times before resetting, the actual condition may also
172 * set the {@link #tested} flag.
174 abstract public boolean condition() throws Exception;
177 * Returns a {@link java.lang.String} representation of the
180 public String toString() {
181 return "Condition#" + System.identityHashCode( this );