/
BSXCache.java
158 lines (157 loc) · 3.59 KB
/
BSXCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* This file is part of "JTA - Telnet/SSH for the JAVA(tm) platform".
*
* (c) Matthias L. Jugel, Marcus Meißner 1996-2005. All Rights Reserved.
*
* Please visit http://javatelnet.org/ for updates and contact.
*
* --LICENSE NOTICE--
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* --LICENSE NOTICE--
*
*/
package de.mud.bsx;
/**
* a LRU Cache for BSX Objects.
@author Thomas Kriegelstein (tk4@rb.mud.de)
@version 1.0
*/
public class BSXCache
{
/** the maximum size of cache */
protected int cacheSize = 40;
/** the ids of the objects */
protected String[] ids = new String[cacheSize];
/** the objects */
protected BSXObject[] bsx = new BSXObject[cacheSize];
/** the size of the cache */
protected int size=0;
/** illegal index */
protected final static int NOT_FOUND = -1;
public int size()
{
return size;
}
public String toString()
{
StringBuffer res=new StringBuffer(""+size+" entries\n");
for(int i=cacheSize-size;i<cacheSize;i++)
res.append("\t\t"+i+":\t"+ids[i]+"\n");
return res.toString();
}
public BSXObject getEntry(String id)
{
int index;
BSXObject res;
index=find(id);
if (NOT_FOUND!=index)
{
res=bsx[index];
if (index!=cacheSize-size)
{
remove(index);
size++;
ids[cacheSize-size]=id;
bsx[cacheSize-size]=res;
}
}
else
{
res=null;
}
return res;
}
public void removeEntry(String id)
{
int index;
BSXObject obj;
index=find(id);
if (NOT_FOUND!=index)
{
obj = bsx[index];
obj.flush();
remove(index);
}
}
public void addEntry(String id,BSXObject bsxobject)
{
int index;
index=find(id);
if (NOT_FOUND!=index)
{
remove(index);
}
else
{
if (size==cacheSize)
move(cacheSize/10);
}
size++;
ids[cacheSize-size]=id;
bsx[cacheSize-size]=bsxobject;
}
public boolean containsEntry(String id)
{
int index;
index=find(id);
return (NOT_FOUND!=index);
}
private void remove(int index)
{
if (index!=cacheSize-size)
{
int len;
len=index-(cacheSize-size);
System.arraycopy(ids,cacheSize-size,
ids,cacheSize-size+1,len);
System.arraycopy(bsx,cacheSize-size,
bsx,cacheSize-size+1,len);
}
else
{
ids[cacheSize-size]=null;
bsx[cacheSize-size]=null;
}
size--;
}
private void move(int offset)
{
if (offset>size)
return;
// from - to
for (int i=1;i<=offset;i++)
{
bsx[cacheSize-i].flush();
bsx[cacheSize-i]=null;
ids[cacheSize-i]=null;
}
System.arraycopy(ids,cacheSize-size,
ids,cacheSize-size+offset,cacheSize-offset);
System.arraycopy(bsx,cacheSize-size,
bsx,cacheSize-size+offset,cacheSize-offset);
size-=offset;
}
private int find(String id)
{
int res=NOT_FOUND;
for (int index=cacheSize-size;index<cacheSize;index++)
if (id.equals(ids[index]))
{
res=index;
break;
}
return res;
}
}