001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: EnhancedNodeCache.java,v 1.6 2008/01/02 12:10:16 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.util.cache;
007:
008: import com.hp.hpl.jena.enhanced.EnhNode;
009: import com.hp.hpl.jena.graph.Node;
010:
011: /**
012: EnhancedNodeCache - the cache to use for enhanced nodes (mapping a Node
013: to one of the enhanced nodes it can represent).
014:
015: <p>The cache methods do not need to be synchronised. Java guarantees that
016: access/update of reference values is atomic. The <code>get</code> method
017: does a single read operation of the cache, and then checks that the
018: returned element matches the key, only returning legal objects; changes
019: to the cache subsequently don't affect the correctness of the result.
020:
021: <p>The <code>put</code> method updates the appropriate cache entry as
022: a one-shot deal. gets on different slots don't matter. Gets on the same
023: slot have either completed (and thus don't care about the change) or
024: are about to happen (and will be equally happy with the old or new
025: value).
026:
027: <p>Synchronisation *is* required when updating the EnhNode sibling ring,
028: but that doesn't happen here.
029:
030: @author kers
031: */
032: public class EnhancedNodeCache implements Cache {
033: protected String name;
034:
035: protected EnhNode[] elements;
036:
037: protected boolean enabled = true;
038:
039: protected long gets, puts, hits;
040:
041: public EnhancedNodeCache(String name, int size) {
042: this .name = name;
043: this .elements = new EnhNode[size];
044: }
045:
046: public Object get(Object key) {
047: if (enabled) {
048: gets += 1;
049: Node n = (Node) key;
050: int i = hashNode(n);
051: EnhNode result = elements[i];
052: if (result != null && result.asNode().equals(key)) {
053: hits += 1;
054: return result;
055: }
056: }
057: return null;
058: }
059:
060: public void put(Object key, Object value) {
061: if (enabled) {
062: puts += 1;
063: Node n = (Node) key;
064: int i = hashNode(n);
065: elements[i] = (EnhNode) value;
066: }
067: }
068:
069: /**
070: * @param n
071: * @return
072: */
073: protected int hashNode(Node n) {
074: return (n.hashCode() & 0x7fffffff) % elements.length;
075: }
076:
077: public boolean getEnabled() {
078: return enabled;
079: }
080:
081: public boolean setEnabled(boolean enabled) {
082: boolean result = this .enabled;
083: this .enabled = enabled;
084: return result;
085: }
086:
087: public void clear() {
088: for (int i = 0; i < elements.length; i += 1)
089: elements[i] = null;
090: }
091:
092: public long getGets() {
093: return gets;
094: }
095:
096: public long getPuts() {
097: return puts;
098: }
099:
100: public long getHits() {
101: return hits;
102: }
103:
104: }
105:
106: /*
107: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
108: All rights reserved.
109:
110: Redistribution and use in source and binary forms, with or without
111: modification, are permitted provided that the following conditions
112: are met:
113:
114: 1. Redistributions of source code must retain the above copyright
115: notice, this list of conditions and the following disclaimer.
116:
117: 2. Redistributions in binary form must reproduce the above copyright
118: notice, this list of conditions and the following disclaimer in the
119: documentation and/or other materials provided with the distribution.
120:
121: 3. The name of the author may not be used to endorse or promote products
122: derived from this software without specific prior written permission.
123:
124: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
125: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
126: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
127: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
128: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
129: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
130: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
131: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
132: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
133: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
134: */
|