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