KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > cache > EnhancedNodeCache


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

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 /**
12  EnhancedNodeCache
13  @author kers
14  */

15 public class EnhancedNodeCache implements Cache
16     {
17     protected String JavaDoc name;
18     
19     protected EnhNode [] elements;
20     
21     protected boolean enabled = true;
22     
23     protected long gets, puts, hits;
24     
25     public EnhancedNodeCache( String JavaDoc name, int size )
26         { this.name = name;
27         this.elements = new EnhNode[size]; }
28
29     public Object JavaDoc get( Object JavaDoc 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 JavaDoc key, Object JavaDoc 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     /**
58      * @param n
59      * @return
60     */

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 /*
88     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
89     All rights reserved.
90
91     Redistribution and use in source and binary forms, with or without
92     modification, are permitted provided that the following conditions
93     are met:
94
95     1. Redistributions of source code must retain the above copyright
96     notice, this list of conditions and the following disclaimer.
97
98     2. Redistributions in binary form must reproduce the above copyright
99     notice, this list of conditions and the following disclaimer in the
100     documentation and/or other materials provided with the distribution.
101
102     3. The name of the author may not be used to endorse or promote products
103     derived from this software without specific prior written permission.
104
105     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
106     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
107     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
108     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
109     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
110     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
111     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
112     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
113     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
114     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
115 */
Popular Tags