1 30 31 package com.hp.hpl.jena.xmloutput.impl; 32 33 import java.util.*; 34 import com.hp.hpl.jena.util.iterator.*; 35 36 43 class Relation { 44 final private Map rows; 45 final private Map cols; 46 final private Set index; 47 49 public Relation() { 50 rows = new HashMap(); 51 cols = new HashMap(); 52 index = new HashSet(); 53 } 54 57 synchronized public void set(Object a, Object b) { 58 index.add(a); 59 index.add(b); 60 innerAdd(rows, a, b); 61 innerAdd(cols, b, a); 62 } 63 69 synchronized public void set11(Object a, Object b) { 70 clearX(a, forward(a)); 71 clearX(backward(b), b); 72 set(a, b); 73 } 74 78 synchronized public void set1N(Object a, Object b) { 79 clearX(backward(b), b); 80 set(a, b); 81 } 82 86 synchronized public void setN1(Object a, Object b) { 87 clearX(a, forward(a)); 88 set(a, b); 89 } 90 93 synchronized public void setNN(Object a, Object b) { 94 set(a, b); 95 } 96 99 synchronized public void clear(Object a, Object b) { 100 innerClear(rows, a, b); 101 innerClear(cols, b, a); 102 } 103 private void clearX(Set s, Object 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 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 a, Object 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 a, Object b) { 126 Set vals = (Set) s.get(a); 127 if (vals != null) { 128 vals.remove(b); 129 } 130 } 131 134 public boolean get(Object a, Object b) { 135 Set vals = (Set) rows.get(a); 136 return vals != null && vals.contains(b); 137 } 138 146 synchronized public void transitiveClosure() { 147 Iterator j = index.iterator(); 148 while (j.hasNext()) { 149 Object 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 oi = i.next(); 156 if (oi != oj) { 157 Iterator k = sk.iterator(); 158 while (k.hasNext()) { 159 Object ok = k.next(); 160 if (ok != oj) 161 set(oi, ok); 162 } 163 } 164 } 165 } 166 } 167 } 168 172 synchronized public Set getDiagonal() { 173 Set rslt = new HashSet(); 174 Iterator it = index.iterator(); 175 while (it.hasNext()) { 176 Object o = it.next(); 177 if (get(o, o)) 178 rslt.add(o); 179 } 180 return rslt; 181 } 182 186 public Set forward(Object a) { 187 return (Set) rows.get(a); 188 } 189 193 public Set backward(Object b) { 194 return (Set) cols.get(b); 195 } 196 203 public Iterator iterator() { 204 return new IteratorIterator(new Map1Iterator(new Map1() { 205 public Object map1(Object o) { 207 Map.Entry pair = (Map.Entry) o; 208 final Object a = pair.getKey(); 209 Set bs = (Set) pair.getValue(); 210 return new Map1Iterator( 211 new Map1() { 213 public Object map1(Object 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 |