KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > classicStore > DeathObjectBuffer


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package org.ozoneDB.core.storage.classicStore;
10
11 import org.ozoneDB.DxLib.DxHashMap;
12 import org.ozoneDB.DxLib.DxMap;
13 import org.ozoneDB.core.ObjectID;
14 import org.ozoneDB.core.storage.classicStore.DeathObject;
15
16 /**
17  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
18  */

19 public class DeathObjectBuffer extends Object JavaDoc {
20
21     /** all objects ordered by their ObjectID */
22     public DxMap objects;
23
24     /** head and tail of the double linked list */
25     DeathObject head;
26     DeathObject tail;
27
28     int bufferSize = 0;
29
30
31     /** the constructor */
32     public DeathObjectBuffer() {
33         objects = new DxHashMap();
34         head = new DeathObject();
35         tail = new DeathObject();
36         head.previous = tail;
37         tail.next = head;
38     }
39
40
41     /** returns the current size of the buffer */
42     public final int size() {
43         return bufferSize;
44     }
45
46
47     /** returns the current count of objects of the buffer */
48     public final int count() {
49         return objects.count();
50     }
51
52
53     /**
54      */

55     public void updateSize( DeathObject obj, boolean sub ) {
56         bufferSize += sub ? -obj.size() : obj.size();
57     }
58
59
60     /**
61      * adds the object to the bottom of the stack
62      */

63     public boolean add( DeathObject obj ) {
64         boolean result = objects.addForKey( obj, obj.objID() );
65         if (result) {
66             popOnTop( obj );
67             bufferSize += obj.size();
68         }
69         return result;
70     }
71
72
73     /**
74      * removes the object from the stack
75      */

76     public DeathObject remove( ObjectID oid ) {
77         //env.logWriter.newEntry ("DeathObjectBuffer.remove: " + oid, LogWriter.DEBUG3);
78
DeathObject obj = (DeathObject)objects.removeForKey( oid );
79         if (obj != null) {
80             removeFromStack( obj );
81             bufferSize -= obj.size();
82         }
83         return obj;
84     }
85
86
87     /**
88      * moves the specified object to the top of the stack
89      */

90     public boolean moveToTop( ObjectID oid ) {
91         DeathObject obj = (DeathObject)objects.elementForKey( oid );
92         if (obj != null) {
93             removeFromStack( obj );
94             popOnTop( obj );
95         }
96         return obj != null;
97     }
98
99
100     /**
101      * removes and returns the object at the bottom
102      */

103     public DeathObject pushFromBottom() {
104         DeathObject obj = tail.next;
105         if (obj == head) {
106             return null;
107         }
108         return remove( obj.objID() );
109     }
110
111
112     /**
113      * returns the DeathObject for the specified ObjectID
114      */

115     public DeathObject objectForId( ObjectID oid ) {
116         return (DeathObject)objects.elementForKey( oid );
117     }
118
119
120     /**
121      * pops the object on the top of the stack
122      */

123     private void popOnTop( DeathObject obj ) {
124         head.previous.next = obj;
125         obj.previous = head.previous;
126         obj.next = head;
127         head.previous = obj;
128     }
129
130
131     /**
132      * pops the object on the bottom of the stack
133      */

134     private void popOnBottom( DeathObject obj ) {
135         tail.next.previous = obj;
136         obj.next = tail.next;
137         obj.previous = tail;
138         tail.next = obj;
139     }
140
141
142     /**
143      * removes the object from the stack
144      */

145     private void removeFromStack( DeathObject obj ) {
146         obj.previous.next = obj.next;
147         obj.next.previous = obj.previous;
148     }
149 }
150
Popular Tags