KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > xmloutput > impl > Relation


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: Relation.java,v 1.5 2005/02/21 12:22:31 andy_seaborne Exp $
28  *
29  */

30
31 package com.hp.hpl.jena.xmloutput.impl;
32
33 import java.util.*;
34 import com.hp.hpl.jena.util.iterator.*;
35
36 /**
37  * A sparse 2 dimensional array of boolean indexed by Object.
38  *
39  * Complete with transitive closure algorithm.
40  * @author jjc
41  * @version Release='$Name: $' Revision='$Revision: 1.5 $' Date='$Date: 2005/02/21 12:22:31 $'
42  */

43 class Relation {
44     final private Map rows;
45     final private Map cols;
46     final private Set index;
47     /** The empty Relation.
48      */

49     public Relation() {
50         rows = new HashMap();
51         cols = new HashMap();
52         index = new HashSet();
53     }
54     /** <code>a</code> is now related to <code>b</code>
55      *
56      */

57     synchronized public void set(Object JavaDoc a, Object JavaDoc b) {
58         index.add(a);
59         index.add(b);
60         innerAdd(rows, a, b);
61         innerAdd(cols, b, a);
62     }
63     /** Uniquely <code>a</code> is now related to uniquely <code>b</code>.
64      *
65      * When this is called any other <code>a</code> related to this <code>b</code> is removed.
66      * When this is called any other <code>b</code> related to this <code>a</code> is removed.
67      *
68      */

69     synchronized public void set11(Object JavaDoc a, Object JavaDoc b) {
70         clearX(a, forward(a));
71         clearX(backward(b), b);
72         set(a, b);
73     }
74     /** Uniquely <code>a</code> is now related to <code>b</code>.
75      * Many <code>b</code>'s can be related to each <code>a</code>.
76      * When this is called any other <code>a</code> related to this <code>b</code> is removed.
77      */

78     synchronized public void set1N(Object JavaDoc a, Object JavaDoc b) {
79         clearX(backward(b), b);
80         set(a, b);
81     }
82     /** <code>a</code> is now related to uniquely <code>b</code>.
83      * Many <code>a</code>'s can be related to each <code>b</code>.
84      * When this is called any other <code>b</code> related to this <code>a</code> is removed.
85      */

86     synchronized public void setN1(Object JavaDoc a, Object JavaDoc b) {
87         clearX(a, forward(a));
88         set(a, b);
89     }
90     /** <code>a</code> is now related to <code>b</code>
91      *
92      */

93     synchronized public void setNN(Object JavaDoc a, Object JavaDoc b) {
94         set(a, b);
95     }
96     /** <code>a</code> is now <em>not</em> related to <code>b</code>
97      *
98      */

99     synchronized public void clear(Object JavaDoc a, Object JavaDoc b) {
100         innerClear(rows, a, b);
101         innerClear(cols, b, a);
102     }
103     private void clearX(Set s, Object JavaDoc b) {
104         if (s == null)
105             return;
106         Iterator it = s.iterator();
107         while (it.hasNext())
108             clear(it.next(), b);
109     }
110     private void clearX(Object JavaDoc a, Set s) {
111         if (s == null)
112             return;
113         Iterator it = s.iterator();
114         while (it.hasNext())
115             clear(a, it.next());
116     }
117     static private void innerAdd(Map s, Object JavaDoc a, Object JavaDoc b) {
118         Set vals = (Set) s.get(a);
119         if (vals == null) {
120             vals = new HashSet();
121             s.put(a, vals);
122         }
123         vals.add(b);
124     }
125     static private void innerClear(Map s, Object JavaDoc a, Object JavaDoc b) {
126         Set vals = (Set) s.get(a);
127         if (vals != null) {
128             vals.remove(b);
129         }
130     }
131     /** Is <code>a</code> related to <code>b</code>?
132      *
133      */

134     public boolean get(Object JavaDoc a, Object JavaDoc b) {
135         Set vals = (Set) rows.get(a);
136         return vals != null && vals.contains(b);
137     }
138     /**
139      * Takes this to its transitive closure.
140     See B. Roy. <b>Transitivité et connexité.</b> <i>C.R. Acad. Sci.</i> Paris <b>249</b>, 1959 pp 216-218.
141     or
142     S. Warshall, <b>A theorem on Boolean matrices</b>, <i>Journal of the ACM</i>, <b>9</b>(1), 1962, pp11-12
143     
144     
145      */

146     synchronized public void transitiveClosure() {
147         Iterator j = index.iterator();
148         while (j.hasNext()) {
149             Object JavaDoc oj = j.next();
150             Set si = (Set) cols.get(oj);
151             Set sk = (Set) rows.get(oj);
152             if (si != null && sk != null) {
153                 Iterator i = si.iterator();
154                 while (i.hasNext()) {
155                     Object JavaDoc oi = i.next();
156                     if (oi != oj) {
157                         Iterator k = sk.iterator();
158                         while (k.hasNext()) {
159                             Object JavaDoc ok = k.next();
160                             if (ok != oj)
161                                 set(oi, ok);
162                         }
163                     }
164                 }
165             }
166         }
167     }
168     /**
169      * The set of <code>a</code> such that <code>a</code> is related to <code>a</code>.
170      *
171      */

172     synchronized public Set getDiagonal() {
173         Set rslt = new HashSet();
174         Iterator it = index.iterator();
175         while (it.hasNext()) {
176             Object JavaDoc o = it.next();
177             if (get(o, o))
178                 rslt.add(o);
179         }
180         return rslt;
181     }
182     /**
183      * The set of <code>b</code> such that <code>a</code> is related to <code>b</code>.
184      *
185      */

186     public Set forward(Object JavaDoc a) {
187         return (Set) rows.get(a);
188     }
189     /**
190      * The set of <code>a</code> such that <code>a</code> is related to <code>b</code>.
191      *
192      */

193     public Set backward(Object JavaDoc b) {
194         return (Set) cols.get(b);
195     }
196     /**
197      * An Iterator over the pairs of the Relation.
198      * Each pair is returned as a java.util.Map.Entry.
199      * The first element is accessed through <code>getKey()</code>,
200      * the second through <code>getValue()</code>.
201      *@see java.util.Map.Entry
202      */

203     public Iterator iterator() {
204         return new IteratorIterator(new Map1Iterator(new Map1() {
205             // Convert a Map.Entry into an iterator over Map.Entry
206
public Object JavaDoc map1(Object JavaDoc o) {
207                 Map.Entry pair = (Map.Entry) o;
208                 final Object JavaDoc a = pair.getKey();
209                 Set bs = (Set) pair.getValue();
210                 return new Map1Iterator(
211                 // Converts a b into a Map.Entry pair.
212
new Map1() {
213                     public Object JavaDoc map1(Object JavaDoc b) {
214                         return new PairEntry(a, b);
215                     }
216                 }, bs.iterator());
217             }
218         }, rows.entrySet().iterator()));
219     }
220     
221     synchronized public Relation copy() {
222         Relation rslt = new Relation();
223         Iterator it = iterator();
224         while ( it.hasNext() ) {
225             Map.Entry e = (Map.Entry)it.next();
226             rslt.set(e.getKey(),e.getValue());
227         }
228         return rslt;
229     }
230 }
231
Popular Tags