Mercurial > projects > dwt2
comparison java/src/java/lang/wrappers.d @ 23:aaa0a4e20edf
Added missing files to repository
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 20 Mar 2009 13:17:52 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
22:4642ab680468 | 23:aaa0a4e20edf |
---|---|
1 module java.lang.wrappers; | |
2 | |
3 import java.lang.String; | |
4 | |
5 abstract class ArrayWrapper{ | |
6 } | |
7 abstract class ValueWrapper{ | |
8 } | |
9 | |
10 class ArrayWrapperT(T) : ArrayWrapper { | |
11 public T[] array; | |
12 public this( T[] data ){ | |
13 array = data; | |
14 } | |
15 public override equals_t opEquals( Object o ){ | |
16 if( auto other = cast(ArrayWrapperT!(T))o){ | |
17 return array == other.array; | |
18 } | |
19 return false; | |
20 } | |
21 public override hash_t toHash(){ | |
22 return (typeid(T[])).getHash(&array); | |
23 } | |
24 static if( is( T == char )){ | |
25 public override String toString(){ | |
26 return cast(String)array; | |
27 } | |
28 } | |
29 } | |
30 | |
31 class ValueWrapperT(T) : ValueWrapper { | |
32 public T value; | |
33 public this( T data ){ | |
34 value = data; | |
35 } | |
36 static if( is(T==class) || is(T==interface)){ | |
37 public equals_t opEquals( Object other ){ | |
38 if( auto o = cast(ValueWrapperT!(T))other ){ | |
39 return value == o.value; | |
40 } | |
41 if( auto o = cast(T)other ){ | |
42 if( value is o ){ | |
43 return true; | |
44 } | |
45 if( value is null || o is null ){ | |
46 return false; | |
47 } | |
48 return value == o; | |
49 } | |
50 return false; | |
51 } | |
52 } | |
53 else{ | |
54 public equals_t opEquals( Object other ){ | |
55 if( auto o = cast(ValueWrapperT!(T))other ){ | |
56 return value == o.value; | |
57 } | |
58 return false; | |
59 } | |
60 public equals_t opEquals( T other ){ | |
61 return value == other; | |
62 } | |
63 } | |
64 public override hash_t toHash(){ | |
65 return (typeid(T)).getHash(&value); | |
66 } | |
67 } | |
68 | |
69 alias ArrayWrapperT!(byte) ArrayWrapperByte; | |
70 alias ArrayWrapperT!(int) ArrayWrapperInt; | |
71 alias ArrayWrapperT!(Object) ArrayWrapperObject; | |
72 alias ArrayWrapperT!(char) ArrayWrapperString; | |
73 alias ArrayWrapperT!(String) ArrayWrapperString2; | |
74 | |
75 Object[] StringArrayToObjectArray( String[] strs ){ | |
76 Object[] res = new Object[strs.length]; | |
77 foreach( idx, str; strs ){ | |
78 res[idx] = new ArrayWrapperString(cast(char[])str); | |
79 } | |
80 return res; | |
81 } | |
82 | |
83 String stringcast( Object o ){ | |
84 if( auto str = cast(ArrayWrapperString) o ){ | |
85 return cast(String)str.array; | |
86 } | |
87 return null; | |
88 } | |
89 String[] stringcast( Object[] objs ){ | |
90 String[] res = new String[](objs.length); | |
91 foreach( idx, obj; objs ){ | |
92 res[idx] = stringcast(obj); | |
93 } | |
94 return res; | |
95 } | |
96 ArrayWrapperString stringcast( String str ){ | |
97 return new ArrayWrapperString( cast(char[])str ); | |
98 } | |
99 ArrayWrapperString[] stringcast( String[] strs ){ | |
100 ArrayWrapperString[] res = new ArrayWrapperString[ strs.length ]; | |
101 foreach( idx, str; strs ){ | |
102 res[idx] = stringcast(str); | |
103 } | |
104 return res; | |
105 } | |
106 | |
107 String[] stringArrayFromObject( Object obj ){ | |
108 if( auto wrapper = cast(ArrayWrapperString2)obj ){ | |
109 return wrapper.array; | |
110 } | |
111 if( auto wrapper = cast(ArrayWrapperObject)obj ){ | |
112 String[] res = new String[ wrapper.array.length ]; | |
113 foreach( idx, o; wrapper.array ){ | |
114 if( auto swrapper = cast(ArrayWrapperString) o ){ | |
115 res[idx] = cast(String)swrapper.array; | |
116 } | |
117 } | |
118 return res; | |
119 } | |
120 assert( obj is null ); // if not null, it was the wrong type | |
121 return null; | |
122 } | |
123 | |
124 T[] arrayFromObject(T)( Object obj ){ | |
125 if( auto wrapper = cast(ArrayWrapperObject)obj ){ | |
126 T[] res = new T[ wrapper.array.length ]; | |
127 foreach( idx, o; wrapper.array ){ | |
128 res[idx] = cast(T)o; | |
129 } | |
130 return res; | |
131 } | |
132 assert( obj is null ); // if not null, it was the wrong type | |
133 return null; | |
134 } | |
135 |