1 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 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 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 ( --preIndex ) ); } 42 43 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 name ) 58 { return indexOf( Node.createVariable( name ) ); } 59 60 66 public int lookUp( Node v ) 67 { 68 Integer i = (Integer ) map.get( v ); 69 if (i == null || i.intValue() < 0) return -1; 70 return i.intValue(); 71 } 72 73 80 public int newIndex( Node v ) 81 { 82 Integer already = (Integer ) map.get( v ); 83 int result = already == null ? index++ : -already.intValue() - 1; 84 map.put( v, new Integer ( result ) ); 85 return result; 86 } 87 88 92 public int size() 93 { return map.size(); } 94 95 100 public boolean hasBound( Node v ) 101 { return map.containsKey( v ) && ((Integer ) map.get( v )).intValue() > -1; } 102 103 106 public String toString() 107 { return map.toString(); } 108 } 109 110 139 | Popular Tags |