1 6 package com.hp.hpl.jena.util.cache; 7 8 import com.hp.hpl.jena.enhanced.EnhNode; 9 import com.hp.hpl.jena.graph.Node; 10 11 15 public class EnhancedNodeCache implements Cache 16 { 17 protected String name; 18 19 protected EnhNode [] elements; 20 21 protected boolean enabled = true; 22 23 protected long gets, puts, hits; 24 25 public EnhancedNodeCache( String name, int size ) 26 { this.name = name; 27 this.elements = new EnhNode[size]; } 28 29 public Object get( Object key ) 30 { 31 if (enabled) 32 { 33 gets += 1; 34 Node n = (Node) key; 35 int i = hashNode( n ); 36 EnhNode result = elements[i]; 37 if (result != null && result.asNode().equals( key )) 38 { 39 hits += 1; 40 return result; 41 } 42 } 43 return null; 44 } 45 46 public void put( Object key, Object value ) 47 { 48 if (enabled) 49 { 50 puts += 1; 51 Node n = (Node) key; 52 int i = hashNode( n ) ; 53 elements[i] = (EnhNode) value; 54 } 55 } 56 57 61 protected int hashNode( Node n ) 62 { return (n.hashCode() & 0x7fffffff) % elements.length; } 63 64 public boolean getEnabled() 65 { return enabled; } 66 67 public boolean setEnabled( boolean enabled ) 68 { boolean result = this.enabled; 69 this.enabled = enabled; 70 return result; } 71 72 public void clear() 73 { for (int i = 0; i < elements.length; i += 1) elements[i] = null; } 74 75 public long getGets() 76 { return gets; } 77 78 public long getPuts() 79 { return puts; } 80 81 public long getHits() 82 { return hits; } 83 84 } 85 86 87 | Popular Tags |