capture
[rrq/gorite.git] / com / intendico / data / Equals.java
1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
3
4 This file is part of GORITE.
5
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.
10
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.
15
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 **********************************************************************/
19
20 package com.intendico.data;
21
22 import java.util.Vector;
23
24 /**
25  * The Equals class is a query to ensure that two or more given
26  * objects are equal, where any or both of the objects may be {@link
27  * Ref} objects.  If a {@link Ref} object is null, it is set to the
28  * other value.
29  */
30 public class Equals extends Condition {
31
32     /**
33      * The distinct values concerned.
34      */
35     public Object [] values;
36
37     /**
38      * Constructor.
39      */
40     public Equals(Object/*...*/ [] v) {
41         values = v;
42     }
43
44     /**
45      * The {@link Query#copy} method implemented by creating a new
46      * Except object with a copy of the ref, but the same values
47      */
48     //@SuppressWarnings("unchecked")
49     public Query copy(Vector/*<Ref>*/ newrefs) throws Exception {
50         Object [] v = new Object [ values.length ];
51         for ( int i = 0; i < values.length; i++ ) {
52             if ( values[ i ] instanceof Ref ) {
53                 v[ i ] = ((Ref)values[ i ]).find( newrefs );
54             } else {
55                 v[ i ] = values[ i ];
56             }
57         }
58         return new Equals( v );
59     }
60
61     /**
62      * The exception condition. 
63      * @return <em>false</em> if the {@link Ref} object is equal to
64      * any value, otherwise <em>true</em>. Note that any Ref object as
65      * value is also dereferenced.
66      */
67     public boolean condition() {
68         Object x = null;
69         for ( int i = 0; i < values.length; i++ ) {
70             Object value = values[ i ];
71             Object y =  Ref.deref( value );
72             if ( x != null && ! x.equals( y ) )
73                 return false;
74             x = y;
75         }
76         return true;
77     }
78
79     /**
80      * The {@link Query#getRefs} method implemented by adding and Ref
81      * not already contained in the vector.
82      */
83     public Vector/*<Ref>*/ getRefs(Vector/*<Ref>*/ v) {
84         for ( int i = 0; i < values.length; i++ ) {
85             Object x = values[ i ];
86             if ( x instanceof Ref && ! v.contains( x ) )
87                 v.add( (Ref) x );
88         }
89         return v;
90     }
91
92     /**
93      * Returns the textual representation of the query.
94      */
95     public String toString() {
96         StringBuilder s = new StringBuilder( "equals(" );
97         for ( int i = 0; i < values.length; i++ ) {
98             Object x = values[ i ];
99             s.append( "," );
100             if ( x instanceof Ref ) {
101                 s.append( ((Ref) x).getName() );
102                 s.append( "=" );
103             }
104             s.append( x.toString() );
105         }
106         s.append( ")" );
107         return s.toString();
108     }
109 }