KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > mem > NodeToTriplesMap


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: NodeToTriplesMap.java,v 1.16 2005/02/21 12:03:46 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.mem;
8
9 import java.util.*;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.util.CollectionFactory;
13 import com.hp.hpl.jena.util.iterator.*;
14
15 /**
16     NodeToTriplesMap: a map from nodes to sets of triples.
17     @author kers
18 */

19 public abstract class NodeToTriplesMap
20     {
21     /**
22          The map from nodes to Set(Triple).
23     */

24     private Map map = CollectionFactory.createHashedMap();
25     
26     /**
27           The number of triples held in this NTM, maingained incrementally (because
28           it's a pain to compute from scratch).
29     */

30     private int size = 0;
31     
32     /**
33          The nodes which appear in the index position of the stored triples; useful
34          for eg listSubjects().
35     */

36     public Iterator domain()
37         { return map.keySet().iterator(); }
38     
39     /**
40          Subclasses must over-ride to return the node at the index position in the
41          triple <code>t</code>; should be equivalent to one of getSubject(),
42          getPredicate(), or getObject().
43     */

44     public abstract Node getIndexNode( Triple t );
45     
46     /**
47          Add <code>t</code> to this NTM; the node <code>o</code> <i>must</i>
48          be the index node of the triple. Answer <code>true</code> iff the triple
49          was not previously in the set, ie, it really truly has been added.
50     */

51     public boolean add( Node o, Triple t )
52         {
53         Set s = (Set) map.get( o );
54         if (s == null) map.put( o, s = CollectionFactory.createHashedSet() );
55         if (s.add( t )) { size += 1; return true; } else return false;
56         }
57
58     /**
59          Remove <code>t</code> from this NTM; the node <code>o</code> <i>must</i>
60          be the index node of the triple. Answer <code>true</code> iff the triple
61          was previously in the set, ie, it really truly has been removed.
62     */

63     public boolean remove( Node o, Triple t )
64         {
65         Set s = (Set) map.get( o );
66         if (s == null)
67             return false;
68         else
69             {
70             boolean result = s.remove( t );
71             if (result) size -= 1;
72             if (s.isEmpty()) map.put( o, null );
73             return result;
74             }
75         }
76
77     /**
78          Answer an iterator over all the triples in this NTM which have index node
79          <code>o</code>.
80     */

81     public Iterator iterator( Node o )
82         {
83         Set s = (Set) map.get( o );
84         return s == null ? NullIterator.instance : s.iterator();
85         }
86     
87     /**
88          Answer an iterator over all the triples in this NTM.
89     */

90     public ExtendedIterator iterator()
91         {
92         final Iterator nodes = domain();
93         return new NiceIterator()
94             {
95             private Iterator current = NullIterator.instance;
96             
97             public Object JavaDoc next()
98                 {
99                 if (hasNext() == false) noElements( "NodeToTriples iterator" );
100                 return current.next();
101                 }
102             
103             public boolean hasNext()
104                 {
105                 while (true)
106                     {
107                     if (current.hasNext()) return true;
108                     if (nodes.hasNext() == false) return false;
109                     current = iterator( (Node) nodes.next() );
110                     }
111                 }
112             
113             public void remove()
114                 {
115                 current.remove();
116                 size -= 1;
117                 }
118             };
119         }
120     
121     /**
122          Clear this NTM; it will contain no triples.
123     */

124     public void clear()
125         { map.clear(); size = 0; }
126     
127     public int size()
128         { return size; }
129     
130     public boolean isEmpty()
131         { return size == 0; }
132
133     /**
134          Answer an iterator over all the triples in this NTM which are accepted by
135          <code>pattern</code>.
136     */

137     public ExtendedIterator iterator( Triple pattern )
138         {
139         return iterator() .filterKeep ( new TripleMatchFilter( pattern ) );
140         }
141     
142     /**
143          Answer an iterator over all the triples in this NTM with index node
144          <code>x</code> and which are accepted by <code>pattern</code>.
145     */

146     public ExtendedIterator iterator( Node x, Triple pattern )
147         {
148         return new FilterIterator( new TripleMatchFilter( pattern ), iterator( x ) );
149         }
150
151     /**
152          Remove the triple <code>t</code>, returning true iff it was originally present.
153     */

154     public boolean remove( Triple t )
155         { return remove( getIndexNode( t ), t ); }
156
157     /**
158         Answer true iff this NTM contains the concrete triple <code>t</code>.
159     */

160     public boolean contains( Triple t )
161         {
162         Set s = (Set) map.get( getIndexNode( t ) );
163         return s == null ? false : s.contains( t );
164         }
165     }
166
167 /*
168     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
169     All rights reserved.
170
171     Redistribution and use in source and binary forms, with or without
172     modification, are permitted provided that the following conditions
173     are met:
174
175     1. Redistributions of source code must retain the above copyright
176        notice, this list of conditions and the following disclaimer.
177
178     2. Redistributions in binary form must reproduce the above copyright
179        notice, this list of conditions and the following disclaimer in the
180        documentation and/or other materials provided with the distribution.
181
182     3. The name of the author may not be used to endorse or promote products
183        derived from this software without specific prior written permission.
184
185     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195 */
Popular Tags