forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpMap.java
More file actions
191 lines (177 loc) · 5.15 KB
/
Copy pathPhpMap.java
File metadata and controls
191 lines (177 loc) · 5.15 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge;
/*
* 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.
*/
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* Maps php iterator to java iterator.
* @author jostb
*
*/
abstract class PhpMap {
JavaBridge _bridge;
Object value;
Class componentType;
boolean keyType; //false: key is integer (array), true: key is string (hash)
protected PhpMap(JavaBridge bridge, Object value, boolean keyType) {
this._bridge=bridge;
this.value=value;
this.keyType=keyType;
this.componentType = value.getClass().getComponentType();
init();
}
protected Object coerce(Object val) {
return _bridge.coerce(componentType, val, _bridge.request.response);
}
protected abstract void init();
/**
* Returns the object at the current position.
* @return The current object.
*/
public abstract Object currentData();
/**
* Returns the key at the current position.
* @return The current key, either a string or a number.
*/
public abstract Object currentKey();
/**
* Forward one element.
* @return true if move was possible, false otherwise.
*/
public abstract boolean moveForward();
/**
* Checks if it is possible to advance one element
* @return true if next element exists, false otherwise
*/
public abstract boolean hasMore();
/**
* Returns the key type.
* @return false if key is integer (array index), true if key is string (hash key)
*/
public boolean getType() {
return keyType;
}
/**
* Returns a PhpMap for a given value.
* @param value The value, must be an array or implement Map or Collection
* @param bridge The bridge instance
* @return The PhpMap
*/
public static PhpMap getPhpMap(Object value, JavaBridge bridge) {
if(bridge.logLevel>3) bridge.logDebug("returning map for "+ value.getClass());
if(value.getClass().isArray()) {
return
new PhpMap(bridge, value, false) {
boolean valid;
int i;
int length;
protected void init() {
i=0;
length = Array.getLength(this.value);
valid=length>0;
}
public Object currentData() {
if(!valid) return null;
return Array.get(this.value, i);
}
public Object currentKey() {
if(!valid) return null;
return _bridge.castToExact(new Integer(i));
}
public boolean moveForward() {
valid=++i<length;
return valid?true:false;
}
public boolean hasMore() {
return valid?true:false;
}
};
}
if(value instanceof Collection) {
return
new PhpMap(bridge, value, false) {
int i;
boolean valid;
Iterator iter;
protected void init() {
iter = ((Collection)(this.value)).iterator();
i = 0;
valid=false;
if(iter.hasNext()) {
valid=true;
this.value=iter.next();
}
}
public Object currentData() {
return this.value;
}
public Object currentKey() {
return _bridge.castToExact(new Integer(i));
}
public boolean moveForward() {
if(iter.hasNext()) {
i++;
this.value = iter.next();
return valid=true;
} else {
return valid=false;
}
}
public boolean hasMore() {
return valid;
}
};
}
if(value instanceof Map) {
return
new PhpMap(bridge, value, true){
Object currentKey;
Iterator iter;
protected void init() {
iter = ((Map)(this.value)).keySet().iterator();
currentKey=null;
if(iter.hasNext()) {
currentKey=iter.next();
}
}
public Object currentData() {
if(currentKey==null) return null;
return ((Map)(this.value)).get(currentKey);
}
public Object currentKey() {
return new SimplePhpString(_bridge, String.valueOf(currentKey));
}
public boolean moveForward() {
currentKey = iter.hasNext() ? iter.next() : null;
return currentKey==null?false:true;
}
public boolean hasMore() {
return currentKey==null?false:true;
}
};
}
return null;
}
}