KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > Mapping


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: Mapping.java,v 1.14 2005/02/21 11:52:15 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.util.CollectionFactory;
11
12 import java.util.*;
13
14 /**
15     this class is used to record the mapping from [variable] Node's to
16     the indexes they are bound to in a Query. Nodes bound to negative values
17     are predeclared; the negative value is converted on index allocation.
18 */

19
20 public class Mapping implements VariableIndexes
21     {
22     private Map map;
23     
24     private int index = 0;
25     private int preIndex = 0;
26     
27     /**
28         Create a new mapping in which all variables are unbound and the variables
29         of <code>preDeclare</code> will be allocated the first slots in the map in their
30         natural order. [This is so that the query domain elements that come out of the
31         matching process will be positioned to be suitable as query answers.]
32     */

33     public Mapping( Node [] preDeclare )
34         {
35         this.map = CollectionFactory.createHashedMap();
36         index = preDeclare.length;
37         for (int i = 0; i < preDeclare.length; i += 1) preDeclare( preDeclare[i] );
38         }
39         
40     private void preDeclare( Node v )
41         { map.put( v, new Integer JavaDoc( --preIndex ) ); }
42         
43     /**
44         get the index of a node in the mapping; undefined if the
45         node is not mapped.
46         
47         @param v the node to look up
48         @return the index of v in the mapping
49     */

50     public int indexOf( Node v )
51         {
52         int res = lookUp(v);
53         if (res < 0) throw new Query.UnboundVariableException( v );
54         return res;
55         }
56         
57     public int indexOf( String JavaDoc name )
58         { return indexOf( Node.createVariable( name ) ); }
59
60     /**
61         get the index of a node in the mapping; return -1
62         if the node is not mapped.
63         @param v the node to look up
64         @return the index of v in the mapping
65     */

66     public int lookUp( Node v )
67         {
68         Integer JavaDoc i = (Integer JavaDoc) map.get( v );
69         if (i == null || i.intValue() < 0) return -1;
70         return i.intValue();
71         }
72
73     /**
74         allocate an index to the node _v_. _v_ must not already
75         be mapped.
76         
77         @param v the node to be given an index
78         @return the value of the allocated index
79     */

80     public int newIndex( Node v )
81         {
82         Integer JavaDoc already = (Integer JavaDoc) map.get( v );
83         int result = already == null ? index++ : -already.intValue() - 1;
84         map.put( v, new Integer JavaDoc( result ) );
85         return result;
86         }
87         
88     /**
89         Answer the number of names currently held in the map
90         @return the number of names in the map
91     */

92     public int size()
93         { return map.size(); }
94         
95     /**
96         Answer true iff we have already bound v (predeclaration doesn't count)
97         @param v the node to look up
98         @return true iff this mapping has seen a binding occurance of v
99     */

100     public boolean hasBound( Node v )
101         { return map.containsKey( v ) && ((Integer JavaDoc) map.get( v )).intValue() > -1; }
102         
103     /**
104         @return a string representing this mapping
105     */

106     public String JavaDoc toString()
107         { return map.toString(); }
108     }
109
110 /*
111     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
112     All rights reserved.
113
114     Redistribution and use in source and binary forms, with or without
115     modification, are permitted provided that the following conditions
116     are met:
117
118     1. Redistributions of source code must retain the above copyright
119        notice, this list of conditions and the following disclaimer.
120
121     2. Redistributions in binary form must reproduce the above copyright
122        notice, this list of conditions and the following disclaimer in the
123        documentation and/or other materials provided with the distribution.
124
125     3. The name of the author may not be used to endorse or promote products
126        derived from this software without specific prior written permission.
127
128     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
129     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
131     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
132     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
133     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
137     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138 */

139
Popular Tags