1 6 7 package com.hp.hpl.jena.graph; 8 9 import com.hp.hpl.jena.shared.*; 10 import java.util.*; 11 12 19 public class Triple implements TripleMatch { 20 private final Node subj, pred, obj; 21 22 public Triple( Node s, Node p, Node o ) 23 { 24 if (s == null) throw new UnsupportedOperationException ( "subject cannot be null" ); 25 if (p == null) throw new UnsupportedOperationException ( "predicate cannot be null" ); 26 if (o == null) throw new UnsupportedOperationException ( "object cannot be null" ); 27 subj = s; 28 pred = p; 29 obj = o; 30 } 31 32 35 public String toString() 36 { return toString( PrefixMapping.Standard ); } 37 38 public String toString( PrefixMapping pm ) 39 { 40 return subj.toString( pm, true ) 41 + " @" + pred.toString( pm, true ) 42 + " " + obj.toString( pm, true ); 43 } 44 45 48 public Node getSubject() { 49 return subj; 50 } 51 52 55 public Node getPredicate() { 56 return pred; 57 } 58 59 62 public Node getObject() { 63 return obj; 64 } 65 66 public Node getMatchSubject() 67 { return anyToNull( subj ); } 68 69 public Node getMatchPredicate() 70 { return anyToNull( pred ); } 71 72 public Node getMatchObject() 73 { return anyToNull( obj ); } 74 75 private static Node anyToNull( Node n ) 76 { return Node.ANY.equals( n ) ? null : n; } 77 78 private static Node nullToAny( Node n ) 79 { return n == null ? Node.ANY : n; } 80 81 public Triple asTriple() 82 { return this; } 83 84 public boolean isConcrete() 85 { return subj.isConcrete() && pred.isConcrete() && obj.isConcrete(); } 86 87 91 public boolean equals(Object o) 92 { return o instanceof Triple && ((Triple) o).sameAs( subj, pred, obj ); } 93 94 97 public boolean sameAs( Node s, Node p, Node o ) 98 { return subj.equals( s ) && pred.equals( p ) && obj.equals( o ); } 99 100 public boolean matches( Triple other ) 101 { return other.matchedBy( subj, pred, obj ); } 102 103 public boolean matches( Node s, Node p, Node o ) 104 { return subj.matches( s ) && pred.matches( p ) && obj.matches( o ); } 105 106 private boolean matchedBy( Node s, Node p, Node o ) 107 { return s.matches( subj ) && p.matches( pred ) && o.matches( obj ); } 108 109 public boolean subjectMatches( Node s ) 110 { return subj.matches( s ); } 111 112 public boolean predicateMatches( Node p ) 113 { return pred.matches( p ); } 114 115 public boolean objectMatches( Node o ) 116 { return obj.matches( o ); } 117 118 122 public int hashCode() 123 { return hashCode( subj, pred, obj ); } 124 125 131 public static int hashCode( Node s, Node p, Node o ) 132 { return (s.hashCode() >> 1) ^ p.hashCode() ^ (o.hashCode() << 1); } 133 134 140 public static Triple create( Node s, Node p, Node o ) 141 { 142 Triple already = cache.get( s, p, o ); 143 return already == null ? cache.put( new Triple( s, p, o ) ) : already; 144 } 145 146 149 protected static TripleCache cache = new TripleCache(); 150 151 public static Triple createMatch( Node s, Node p, Node o ) 152 { return Triple.create( nullToAny( s ), nullToAny( p ), nullToAny( o ) ); } 153 154 160 161 public static Triple create( String fact ) 162 { return create( PrefixMapping.Standard, fact ); } 163 164 168 public static Triple create( PrefixMapping pm, String fact ) 169 { 170 StringTokenizer st = new StringTokenizer( fact ); 171 Node sub = Node.create( pm, st.nextToken() ); 172 Node pred = Node.create( pm, st.nextToken() ); 173 Node obj = Node.create( pm, st.nextToken() ); 174 return Triple.create( sub, pred, obj ); 175 } 176 177 public static final Triple ANY = Triple.create( Node.ANY, Node.ANY, Node.ANY ); 178 } 179 180 209 | Popular Tags |