Menu

[r205]: / phprpc_3.0 / java / Cast.java  Maximize  Restore  History

Download this file

210 lines (191 with data), 7.5 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| Cast.java |
| |
| Release 3.0.0 beta 5 |
| Copyright (c) 2005-2007 by Team-PHPRPC |
| |
| WebSite: http://www.phprpc.org/ |
| http://www.phprpc.net/ |
| http://www.phprpc.com/ |
| http://sourceforge.net/projects/php-rpc/ |
| |
| Authors: Ma Bingyao <andot@ujn.edu.cn> |
| |
| This file may be distributed and/or modified under the |
| terms of the GNU Lesser General Public License (LGPL) |
| version 2.1 as published by the Free Software Foundation |
| and appearing in the included file LICENSE. |
| |
\**********************************************************/
/* Cast library.
*
* Copyright (C) 2006-2007 Ma Bingyao <andot@ujn.edu.cn>
* Version: 3.0.0 beta 5
* LastModified: Apr 24, 2007
* This library is free. You can redistribute it and/or modify it.
*/
package org.phprpc.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.lang.reflect.Array;
public final class Cast {
private Cast() {};
static public byte[] getBytes(Object obj, String charset) {
try {
return obj.toString().getBytes(charset);
}
catch (Exception e) {
return obj.toString().getBytes();
}
}
static public byte[] getBytes(Object obj) {
return getBytes(obj, "utf-8");
}
static public String toString(Object obj, String charset) {
if (obj instanceof byte[])
{
try {
return new String((byte[]) obj, charset);
}
catch (Exception e) {
return new String((byte[]) obj);
}
}
else {
return obj.toString();
}
}
static public String toString(Object obj) {
return toString(obj, "utf-8");
}
static public Object cast(Number n, Class destClass) {
if (destClass == Byte.class || destClass == Byte.TYPE) {
return new Byte(n.byteValue());
}
if (destClass == Short.class || destClass == Short.TYPE) {
return new Short(n.shortValue());
}
if (destClass == Integer.class || destClass == Integer.TYPE) {
return new Integer(n.intValue());
}
if (destClass == Long.class || destClass == Long.TYPE) {
return new Long(n.longValue());
}
if (destClass == Float.class || destClass == Float.TYPE) {
return new Float(n.floatValue());
}
if (destClass == Double.class || destClass == Double.TYPE) {
return new Double(n.doubleValue());
}
if (destClass == Boolean.class || destClass == Boolean.TYPE) {
return new Boolean(n.byteValue() != 0);
}
return n;
}
static public Object cast(String s, Class destClass, String charset) {
if (destClass == char[].class) {
return s.toCharArray();
}
if (destClass == byte[].class) {
return getBytes(s, charset);
}
if (destClass == StringBuffer.class) {
return new StringBuffer(s);
}
if (destClass == Byte.class || destClass == Byte.TYPE) {
return new Byte(s);
}
if (destClass == Short.class || destClass == Short.TYPE) {
return new Short(s);
}
if (destClass == Integer.class || destClass == Integer.TYPE) {
return new Integer(s);
}
if (destClass == Long.class || destClass == Long.TYPE) {
return new Long(s);
}
if (destClass == Float.class || destClass == Float.TYPE) {
return new Float(s);
}
if (destClass == Double.class || destClass == Double.TYPE) {
return new Double(s);
}
if (destClass == Boolean.class || destClass == Boolean.TYPE) {
return new Boolean(!(s.equals("") || s.equals("0")));
}
return s;
}
static public Object cast(String s, Class destClass) {
return cast(s, destClass, "utf-8");
}
static public Object cast(Object obj, Class destClass, String charset) {
if (obj == null || destClass == null || obj.getClass() == destClass) {
return obj;
}
if (destClass == Void.class || destClass == Void.TYPE) {
return null;
}
if (obj instanceof byte[]) {
return cast(toString(obj, charset), destClass, charset);
}
if (obj instanceof char[]) {
return cast(new String((char[]) obj), destClass, charset);
}
if (obj instanceof StringBuffer) {
return cast(obj.toString(), destClass, charset);
}
if (obj instanceof String) {
return cast((String) obj, destClass, charset);
}
if (destClass == Character.class || destClass == Character.TYPE) {
return new Character(obj.toString().charAt(0));
}
if ((obj instanceof ArrayList) && destClass.isArray()) {
return toArray((ArrayList) obj, destClass.getComponentType(), charset);
}
if ((obj instanceof ArrayList) && destClass == HashMap.class) {
return toHashMap((ArrayList) obj);
}
if ((obj instanceof Boolean) && destClass.isAssignableFrom(Number.class)) {
return cast(new Integer((((Boolean) obj).booleanValue() == true) ? 1 : 0), destClass);
}
if (destClass == String.class) {
return obj.toString();
}
if (destClass == StringBuffer.class) {
return new StringBuffer(obj.toString());
}
if (!obj.getClass().isArray() && destClass == byte[].class) {
return getBytes(obj);
}
if (!obj.getClass().isArray() && destClass == char[].class) {
return obj.toString().toCharArray();
}
if (obj instanceof Number) {
return cast((Number) obj, destClass);
}
return obj;
}
static public Object cast(Object obj, Class destClass) {
return cast(obj, destClass, "utf-8");
}
static HashMap toHashMap(ArrayList a) {
int n = a.size();
HashMap h = new HashMap(n);
for (int i = 0; i < n; i++) {
h.put(new Integer(i), a.get(i));
}
return h;
}
static Object toArray(ArrayList obj, Class componentType, String charset) {
int n = obj.size();
Object a = Array.newInstance(componentType, n);
for (int i = 0; i < n; i++) {
Array.set(a, i, cast(obj.get(i), componentType, charset));
}
return a;
}
};
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.