01: /*
02: * JacORB - a free Java ORB
03: *
04: * Copyright (C) 1999-2004 Gerald Brose
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Library General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Library General Public License for more details.
15: *
16: * You should have received a copy of the GNU Library General Public
17: * License along with this library; if not, write to the Free
18: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19: *
20: */
21: package org.jacorb.collection.util;
22:
23: import java.util.*;
24:
25: public class Cach {
26: /* ------------------------------------------------------------------------- */
27: class Node {
28: Object key;
29: Object element;
30:
31: Node(Object key, Object element) {
32: this .key = key;
33: this .element = element;
34: }
35: }
36:
37: /* ------------------------------------------------------------------------- */
38: private Vector data;
39: private int capacity;
40:
41: /* ------------------------------------------------------------------------- */
42: public Cach(int capacity) {
43: data = new Vector(capacity);
44: this .capacity = capacity;
45: }
46:
47: /* ------------------------------------------------------------------------- */
48: public Object getElement(Object key) {
49: Enumeration enumeration = data.elements();
50: while (enumeration.hasMoreElements()) {
51: Node n = (Node) enumeration.nextElement();
52: if (n.key == key) {
53: data.removeElement(n);
54: data.insertElementAt(n, 0);
55: return n.element;
56: }
57: }
58: return null;
59: }
60:
61: /* ------------------------------------------------------------------------- */
62: public void putElement(Object key, Object element) {
63: if (data.size() >= capacity) {
64: data.removeElementAt(data.size() - 1);
65: }
66: Node n = new Node(key, element);
67: data.insertElementAt(n, 0);
68: }
69:
70: /* ------------------------------------------------------------------------- */
71: public void clear() {
72: data.removeAllElements();
73: }
74:
75: }
|