forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalRef.java
More file actions
170 lines (150 loc) · 4.72 KB
/
Copy pathGlobalRef.java
File metadata and controls
170 lines (150 loc) · 4.72 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
/*-*- mode: Java; tab-width:8 -*-*/
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package php.java.bridge;
/**
* A global array of object references that the client keeps during
* the connection (int -> Object mappings). After connection shutdown
* the request-handling bridge instance and its global ref array are
* destroyed.
*
* We guarantee that the first ref ID is 1 and that each new ref ID is
* n+1. This can be used to avoid round-trips by "guessing" the next
* object ID, see java_begin_document()/java_end_document().
*/
class GlobalRef {
/**
* The default size (prime).
*/
public static final int DEFAULT_SIZE=1021;
private int threshold;
private Entry[] globalRef;
private int id, count;
class Entry {
int id;
Object value;
Entry next;
public Entry(int id, Object value, Entry entry) {
this.id = id;
this.value = value;
this.next = entry;
}
}
public GlobalRef(int initialCapacity) {
id = 1;
count = 0;
globalRef = new Entry[initialCapacity];
threshold = (initialCapacity>>>2)*3;
}
/**
* Create a new global ref table. Must be called for each new or
* recycled JavaBridge instance.
*
*/
public GlobalRef() {
this(DEFAULT_SIZE);
}
/**
* Get the object associated with the ref ID
* @param id The ref ID
* @return The associated object.
* @throws NullPointerException if ref ID does not exist.
*/
public Object get(int id) {
int index = (id & 0x7FFFFFFF) % globalRef.length;
for (Entry e = globalRef[index]; e != null; e = e.next) {
if(e.id == id) return e.value;
}
throw new NullPointerException("cannot manipulate the object #"+id+" which has already been destroyed by PHP");
}
/**
* Remove an element from the table.
* @param id The ref ID.
*/
public void remove(int id) {
int index = (id & 0x7FFFFFFF) % globalRef.length;
for (Entry e=globalRef[index], prev=null; e!=null; prev=e, e=e.next) {
if (e.id == id) {
if (prev!=null) prev.next=e.next; else globalRef[index]=e.next;
--count;
}
}
}
/**
* Return a string representation of the global ref table.
* @return The string representation.
*/
public String dump() {
StringBuffer result = new StringBuffer();
for (int i=0;i<count;i++) {
if (globalRef[i]!=null) {
for(Entry e=globalRef[i]; e!= null; e=e.next)
result.append("globalRef["+i+"]="+JavaBridge.objectDebugDescription(e.value)+"\n");
}
}
return result.toString();
}
public String toString() {
return "GlobalRef: " + dump();
}
/**
* Append an object to the global ref table.
* @param value The value, may be null or PhpNull
* @return The ref ID.
*/
public int append(Object value) {
return put(id++, value);
}
private int put(int id, Object value) {
int index = (id & 0x7FFFFFFF ) % globalRef.length;
for (Entry e = globalRef[index]; e != null; e = e.next) {
if (e.id==id) {
e.value = value;
return id;
}
}
if (count >= threshold) {
rehash();
return put(id, value);
}
globalRef[index] = new Entry(id, value, globalRef[index]);
++count;
return id;
}
private void rehash() {
int oldCapacity = globalRef.length;
Entry oldTable[] = globalRef;
int newCapacity = (oldCapacity << 1) + 1;
Entry newTable[] = new Entry[newCapacity];
threshold = (newCapacity>>>2)*3;
globalRef = newTable;
for (int i=oldCapacity; i-->0;) {
for (Entry old=oldTable[i]; old != null;) {
Entry e = old;
old = old.next;
int index = (e.id & 0x7FFFFFFF) % newCapacity;
e.next = newTable[index];
newTable[index] = e;
}
}
}
}