-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrift-gwt-gwtready-r1064300.patch
2559 lines (2494 loc) · 87.7 KB
/
thrift-gwt-gwtready-r1064300.patch
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Index: test/GWTTest2.thrift
===================================================================
--- test/GWTTest2.thrift (revision 0)
+++ test/GWTTest2.thrift (revision 0)
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+namespace java thrift.test
+
+enum GWTTestEnum2 {
+ TWO = 2,
+}
+
+struct GWTTestStruct2 {
+ 1: bool bool_field,
+ 2: byte byte_field,
+ 3: i16 i16_field,
+ 4: i32 i32_field,
+ 5: i64 i64_field,
+ 6: binary binary_field,
+ 7: string string_field,
+ 8: double double_field,
+ 9: map<string,string> map_field,
+ 10: set<string> set_field,
+ 11: list<string> list_field,
+ 12: GWTTestEnum2 enum_field,
+ 13: list<binary> binary_list_field,
+ 14: set<binary> binary_set_field,
+ 15: map<binary,binary> binary_map_field,
+}
Index: test/GWTTest.thrift
===================================================================
--- test/GWTTest.thrift (revision 0)
+++ test/GWTTest.thrift (revision 0)
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+namespace java thrift.test
+
+enum GWTTestEnum {
+ ONE = 1,
+}
+
+struct GWTTestStruct {
+ 1: bool bool_field,
+ 2: byte byte_field,
+ 3: i16 i16_field,
+ 4: i32 i32_field,
+ 5: i64 i64_field,
+ 6: binary binary_field,
+ 7: string string_field,
+ 8: double double_field,
+ 9: map<string,string> map_field,
+ 10: set<string> set_field,
+ 11: list<string> list_field,
+ 12: GWTTestEnum enum_field,
+ 13: list<binary> binary_list_field,
+ 14: set<binary> binary_set_field,
+ 15: map<binary,binary> binary_map_field,
+}
Index: lib/java/test/org/apache/thrift/test/GWTHelperTest.java
===================================================================
--- lib/java/test/org/apache/thrift/test/GWTHelperTest.java (revision 0)
+++ lib/java/test/org/apache/thrift/test/GWTHelperTest.java (revision 0)
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.test;
+
+import java.lang.Math;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import java.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+import org.apache.thrift.GWTHelper;
+import org.apache.thrift.TException;
+import org.apache.thrift.TBaseHelper;
+
+import org.junit.Assert;
+
+public class GWTHelperTest extends TestCase {
+
+ public static void testGWT2Thrift_bean() {
+ thrift.test.gwt.GWTTestStruct gwt = new thrift.test.gwt.GWTTestStruct();
+
+ gwt.setBool_field(true);
+ gwt.setByte_field((byte) (Math.random() * 256));
+ gwt.setI16_field((short) (Math.random() * Short.MAX_VALUE));
+ gwt.setI32_field((int) (Math.random() * Integer.MAX_VALUE));
+ gwt.setI64_field((long) (Math.random() * Long.MAX_VALUE));
+ gwt.setDouble_field(Math.random() * Double.MAX_VALUE);
+ byte[] a = "foobar".getBytes();
+ gwt.setBinary_field(a);
+ gwt.setString_field("thrift");
+ Map<String,String> m = new HashMap<String,String>();
+ m.put("foo","bar");
+ m.put("thrift","rocks");
+ gwt.setMap_field(m);
+ Set<String> s = new HashSet<String>();
+ s.add("foo");
+ s.add("bar");
+ s.add("thrift");
+ s.add("rocks");
+ gwt.setSet_field(s);
+ List<String> l = new ArrayList<String>();
+ l.add("foo");
+ l.add("bar");
+ l.add("thrift");
+ l.add("rocks");
+ gwt.setList_field(l);
+ gwt.setEnum_field(thrift.test.gwt.GWTTestEnum.ONE);
+
+ gwt.addToBinary_list_field("listfoo".getBytes());
+ gwt.addToBinary_list_field("listbar".getBytes());
+
+ gwt.addToBinary_set_field("setfoo".getBytes());
+
+ gwt.putToBinary_map_field("mapfoo".getBytes(), "mapbar".getBytes());
+
+ try {
+ thrift.test.GWTTestStruct raw = (thrift.test.GWTTestStruct) GWTHelper.convert(gwt);
+
+ assertTrue(raw.isBool_field());
+ assertEquals(gwt.getByte_field(), raw.getByte_field());
+ assertEquals(gwt.getI16_field(), raw.getI16_field());
+ assertEquals(gwt.getI32_field(), raw.getI32_field());
+ assertEquals(gwt.getI64_field(), raw.getI64_field());
+ assertEquals(gwt.getDouble_field(), raw.getDouble_field());
+
+ assertEquals(a.length, raw.getBinary_field().length);
+ for (int i = 0; i < a.length; i++) {
+ assertEquals(gwt.getBinary_field()[i], raw.getBinary_field()[i]);
+ }
+ assertEquals(gwt.getString_field(), raw.getString_field());
+ assertEquals(gwt.getMap_field(), raw.getMap_field());
+ assertEquals(gwt.getSet_field(), raw.getSet_field());
+ assertEquals(gwt.getList_field(), raw.getList_field());
+ assertEquals(gwt.getEnum_field().getValue(), raw.getEnum_field().getValue());
+
+ assertEquals(gwt.getBinary_list_field().size(), raw.getBinary_list_field().size());
+ assertTrue(raw.getBinary_list_field().get(0) instanceof ByteBuffer);
+ Assert.assertArrayEquals(gwt.getBinary_list_field().get(0), TBaseHelper.byteBufferToByteArray(raw.getBinary_list_field().get(0)));
+ Assert.assertArrayEquals(gwt.getBinary_list_field().get(1), TBaseHelper.byteBufferToByteArray(raw.getBinary_list_field().get(1)));
+
+ assertEquals(gwt.getBinary_set_field().size(), raw.getBinary_set_field().size());
+ Iterator iter = raw.getBinary_set_field().iterator();
+ Object o = iter.next();
+ assertTrue(o instanceof ByteBuffer);
+ iter = gwt.getBinary_set_field().iterator();
+ Object oo = iter.next();
+ Assert.assertArrayEquals((byte[]) oo, TBaseHelper.byteBufferToByteArray((ByteBuffer) o));
+
+ assertEquals(gwt.getBinary_map_field().size(), raw.getBinary_map_field().size());
+ for (Object ooo: raw.getBinary_map_field().keySet()) {
+ assertTrue(ooo instanceof ByteBuffer);
+ assertTrue(raw.getBinary_map_field().get(ooo) instanceof ByteBuffer);
+ }
+ for(byte[] b: gwt.getBinary_map_field().keySet()) {
+ Assert.assertArrayEquals(gwt.getBinary_map_field().get(b), TBaseHelper.byteBufferToByteArray(raw.getBinary_map_field().get(ByteBuffer.wrap(b))));
+ }
+ } catch (TException te) {
+ te.printStackTrace();
+ throw new RuntimeException("GWT2Thrift conversion failed with exception " + te);
+ }
+ }
+
+ public static void testThrift2GWT_bean() {
+ thrift.test.GWTTestStruct raw = new thrift.test.GWTTestStruct();
+
+ raw.setBool_field(true);
+ raw.setByte_field((byte) (Math.random() * 256));
+ raw.setI16_field((short) (Math.random() * Short.MAX_VALUE));
+ raw.setI32_field((int) (Math.random() * Integer.MAX_VALUE));
+ raw.setI64_field((long) (Math.random() * Long.MAX_VALUE));
+ raw.setDouble_field(Math.random() * Double.MAX_VALUE);
+ byte[] a = "foobar".getBytes();
+ raw.setBinary_field(a);
+ raw.setString_field("thrift");
+ Map<String,String> m = new HashMap<String,String>();
+ m.put("foo","bar");
+ m.put("thrift","rocks");
+ raw.setMap_field(m);
+ Set<String> s = new HashSet<String>();
+ s.add("foo");
+ s.add("bar");
+ s.add("thrift");
+ s.add("rocks");
+ raw.setSet_field(s);
+ List<String> l = new ArrayList<String>();
+ l.add("foo");
+ l.add("bar");
+ l.add("thrift");
+ l.add("rocks");
+ raw.setList_field(l);
+ raw.setEnum_field(thrift.test.GWTTestEnum.ONE);
+
+ raw.addToBinary_list_field(ByteBuffer.wrap("listfoo".getBytes()));
+ raw.addToBinary_list_field(ByteBuffer.wrap("listbar".getBytes()));
+
+ raw.addToBinary_set_field(ByteBuffer.wrap("setfoo".getBytes()));
+
+ raw.putToBinary_map_field(ByteBuffer.wrap("mapfoo".getBytes()), ByteBuffer.wrap("mapbar".getBytes()));
+
+ try {
+ thrift.test.gwt.GWTTestStruct gwt = (thrift.test.gwt.GWTTestStruct) GWTHelper.convert(raw);
+
+ assertTrue(gwt.isBool_field());
+ assertEquals(gwt.getByte_field(), raw.getByte_field());
+ assertEquals(gwt.getI16_field(), raw.getI16_field());
+ assertEquals(gwt.getI32_field(), raw.getI32_field());
+ assertEquals(gwt.getI64_field(), raw.getI64_field());
+ assertEquals(gwt.getDouble_field(), raw.getDouble_field());
+
+ assertEquals(a.length, raw.getBinary_field().length);
+ for (int i = 0; i < a.length; i++) {
+ assertEquals(gwt.getBinary_field()[i], raw.getBinary_field()[i]);
+ }
+ assertEquals(gwt.getString_field(), raw.getString_field());
+ assertEquals(gwt.getMap_field(), raw.getMap_field());
+ assertEquals(gwt.getSet_field(), raw.getSet_field());
+ assertEquals(gwt.getList_field(), raw.getList_field());
+ assertEquals(gwt.getEnum_field().getValue(), raw.getEnum_field().getValue());
+
+ assertEquals(gwt.getBinary_list_field().size(), raw.getBinary_list_field().size());
+ assertTrue(gwt.getBinary_list_field().get(0) instanceof byte[]);
+ Assert.assertArrayEquals(gwt.getBinary_list_field().get(0), TBaseHelper.byteBufferToByteArray(raw.getBinary_list_field().get(0)));
+ Assert.assertArrayEquals(gwt.getBinary_list_field().get(1), TBaseHelper.byteBufferToByteArray(raw.getBinary_list_field().get(1)));
+
+ assertEquals(gwt.getBinary_set_field().size(), raw.getBinary_set_field().size());
+ Iterator iter = raw.getBinary_set_field().iterator();
+ Object o = iter.next();
+ assertTrue(o instanceof ByteBuffer);
+ iter = gwt.getBinary_set_field().iterator();
+ Object oo = iter.next();
+ assertTrue(oo instanceof byte[]);
+ Assert.assertArrayEquals((byte[]) oo, TBaseHelper.byteBufferToByteArray((ByteBuffer) o));
+
+ assertEquals(gwt.getBinary_map_field().size(), raw.getBinary_map_field().size());
+ for (Object ooo: gwt.getBinary_map_field().keySet()) {
+ assertTrue(ooo instanceof byte[]);
+ assertTrue(gwt.getBinary_map_field().get(ooo) instanceof byte[]);
+ }
+ for(byte[] b: gwt.getBinary_map_field().keySet()) {
+ Assert.assertArrayEquals(gwt.getBinary_map_field().get(b), TBaseHelper.byteBufferToByteArray(raw.getBinary_map_field().get(ByteBuffer.wrap(b))));
+ }
+ } catch (TException te) {
+ throw new RuntimeException("Thrift2GWT conversion failed with exception " + te);
+ }
+ }
+
+ public static void testGWT2Thrift() {
+ thrift.test.gwt.GWTTestStruct2 gwt = new thrift.test.gwt.GWTTestStruct2();
+
+ gwt.setBool_field(true);
+ gwt.setByte_field((byte) (Math.random() * 256));
+ gwt.setI16_field((short) (Math.random() * Short.MAX_VALUE));
+ gwt.setI32_field((int) (Math.random() * Integer.MAX_VALUE));
+ gwt.setI64_field((long) (Math.random() * Long.MAX_VALUE));
+ gwt.setDouble_field(Math.random() * Double.MAX_VALUE);
+ byte[] a = "foobar".getBytes();
+ gwt.setBinary_field(a);
+ gwt.setString_field("thrift");
+ Map<String,String> m = new HashMap<String,String>();
+ m.put("foo","bar");
+ m.put("thrift","rocks");
+ gwt.setMap_field(m);
+ Set<String> s = new HashSet<String>();
+ s.add("foo");
+ s.add("bar");
+ s.add("thrift");
+ s.add("rocks");
+ gwt.setSet_field(s);
+ List<String> l = new ArrayList<String>();
+ l.add("foo");
+ l.add("bar");
+ l.add("thrift");
+ l.add("rocks");
+ gwt.setList_field(l);
+ gwt.setEnum_field(thrift.test.gwt.GWTTestEnum2.TWO);
+
+ try {
+ thrift.test.GWTTestStruct2 raw = (thrift.test.GWTTestStruct2) GWTHelper.convert(gwt);
+
+ assertTrue(raw.isBool_field());
+ assertEquals(gwt.getByte_field(), raw.getByte_field());
+ assertEquals(gwt.getI16_field(), raw.getI16_field());
+ assertEquals(gwt.getI32_field(), raw.getI32_field());
+ assertEquals(gwt.getI64_field(), raw.getI64_field());
+ assertEquals(gwt.getDouble_field(), raw.getDouble_field());
+
+ assertEquals(a.length, raw.getBinary_field().length);
+ Assert.assertArrayEquals(gwt.binary_field, TBaseHelper.byteBufferToByteArray(raw.binary_field));
+
+ assertEquals(gwt.getString_field(), raw.getString_field());
+ assertEquals(gwt.getMap_field(), raw.getMap_field());
+ assertEquals(gwt.getSet_field(), raw.getSet_field());
+ assertEquals(gwt.getList_field(), raw.getList_field());
+ assertEquals(gwt.getEnum_field().getValue(), raw.getEnum_field().getValue());
+
+ } catch (TException te) {
+ throw new RuntimeException("GWT2Thrift conversion failed with exception " + te);
+ }
+ }
+
+ public static void testThrift2GWT() {
+ thrift.test.GWTTestStruct2 raw = new thrift.test.GWTTestStruct2();
+
+ raw.setBool_field(true);
+ raw.setByte_field((byte) (Math.random() * 256));
+ raw.setI16_field((short) (Math.random() * Short.MAX_VALUE));
+ raw.setI32_field((int) (Math.random() * Integer.MAX_VALUE));
+ raw.setI64_field((long) (Math.random() * Long.MAX_VALUE));
+ raw.setDouble_field(Math.random() * Double.MAX_VALUE);
+ byte[] a = "foobar".getBytes();
+ raw.setBinary_field(a);
+ raw.setString_field("thrift");
+ Map<String,String> m = new HashMap<String,String>();
+ m.put("foo","bar");
+ m.put("thrift","rocks");
+ raw.setMap_field(m);
+ Set<String> s = new HashSet<String>();
+ s.add("foo");
+ s.add("bar");
+ s.add("thrift");
+ s.add("rocks");
+ raw.setSet_field(s);
+ List<String> l = new ArrayList<String>();
+ l.add("foo");
+ l.add("bar");
+ l.add("thrift");
+ l.add("rocks");
+ raw.setList_field(l);
+ raw.setEnum_field(thrift.test.GWTTestEnum2.TWO);
+
+ try {
+ thrift.test.gwt.GWTTestStruct2 gwt = (thrift.test.gwt.GWTTestStruct2) GWTHelper.convert(raw);
+
+ assertTrue(gwt.isBool_field());
+ assertEquals(gwt.getByte_field(), raw.getByte_field());
+ assertEquals(gwt.getI16_field(), raw.getI16_field());
+ assertEquals(gwt.getI32_field(), raw.getI32_field());
+ assertEquals(gwt.getI64_field(), raw.getI64_field());
+ assertEquals(gwt.getDouble_field(), raw.getDouble_field());
+
+ assertEquals(a.length, raw.getBinary_field().length);
+ Assert.assertArrayEquals(gwt.binary_field, TBaseHelper.byteBufferToByteArray(raw.binary_field));
+
+ assertEquals(gwt.getString_field(), raw.getString_field());
+ assertEquals(gwt.getMap_field(), raw.getMap_field());
+ assertEquals(gwt.getSet_field(), raw.getSet_field());
+ assertEquals(gwt.getList_field(), raw.getList_field());
+ assertEquals(gwt.getEnum_field().getValue(), raw.getEnum_field().getValue());
+
+ } catch (TException te) {
+ throw new RuntimeException("Thrift2GWT conversion failed with exception " + te);
+ }
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ testGWT2Thrift_bean();
+ testThrift2GWT_bean();
+ testGWT2Thrift();
+ testThrift2GWT();
+ }
+}
Index: lib/java/test/org/apache/thrift/test/GWTBitSetTest.java
===================================================================
--- lib/java/test/org/apache/thrift/test/GWTBitSetTest.java (revision 0)
+++ lib/java/test/org/apache/thrift/test/GWTBitSetTest.java (revision 0)
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.test;
+
+import junit.framework.TestCase;
+
+import org.apache.thrift.gwt.GWTBitSet;
+
+public class GWTBitSetTest extends TestCase {
+ /**
+ * Test a single set operation on a random bit.
+ */
+ public static void testSet_single() {
+ GWTBitSet tbs = new GWTBitSet();
+ int bit = (int) Math.round(Math.random() * 256);
+ tbs.set(bit);
+ assertTrue(tbs.get(bit));
+ }
+
+ /**
+ * Test multiple set operations on first 8 bits of
+ * bit set.
+ */
+ public static void testSet_multiple() {
+ GWTBitSet tbs = new GWTBitSet();
+ for (int i = 0; i < 8; i++) {
+ tbs.set(i);
+ }
+ for (int i = 0; i < 8; i++) {
+ assertTrue(tbs.get(i));
+ }
+ }
+
+ /**
+ * Test random retrieval on empty set
+ */
+ public static void testGet_multiple() {
+ GWTBitSet tbs = new GWTBitSet();
+ for (int i = 0; i < 16; i++) {
+ assertTrue(!tbs.get(i));
+ }
+ }
+
+ /**
+ * Test size of bit set
+ */
+ public static void testSize() {
+ GWTBitSet tbs = new GWTBitSet();
+ int bit = (int) Math.round(Math.random() * 256);
+ tbs.clear(bit);
+ assertEquals(bit + 1, tbs.size());
+ }
+
+ /**
+ * Test String representation of set
+ */
+ public static void testToString() {
+ GWTBitSet tbs = new GWTBitSet();
+ int bit = (int) Math.round(Math.random() * 256);
+ tbs.set(bit);
+ String str = tbs.toString();
+ assertEquals(bit + 1, str.length());
+ assertEquals(bit, str.indexOf("1"));
+ }
+
+ /**
+ * Test constructor with size
+ */
+ public static void testConstructor() {
+ int bit = (int) Math.round(Math.random() * 256) + 1;
+ GWTBitSet tbs = new GWTBitSet(bit);
+ assertEquals(bit, tbs.size());
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ testSet_single();
+ testSet_multiple();
+ testGet_multiple();
+ testSize();
+ testToString();
+ testConstructor();
+ }
+
+}
Index: lib/java/src/org/apache/thrift/GWTHelper.java
===================================================================
--- lib/java/src/org/apache/thrift/GWTHelper.java (revision 0)
+++ lib/java/src/org/apache/thrift/GWTHelper.java (revision 0)
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.TException;
+
+public class GWTHelper {
+ public static Object convert(Object o) throws TException {
+
+ try {
+ if (o instanceof GWTReady) {
+ return ((GWTReady) o).Thrift2GWT(o);
+ } else if (o.getClass().getName().contains(".gwt.") || o.getClass().getName().startsWith("gwt.")) {
+ Class c = Class.forName(o.getClass().getName().replace("gwt.",""));
+ if (o instanceof org.apache.thrift.gwt.TEnum) {
+ java.lang.reflect.Method m = c.getDeclaredMethod("findByValue", int.class);
+ return m.invoke(null, ((org.apache.thrift.gwt.TEnum) o).getValue());
+ } else {
+ Object obj = c.newInstance();
+ java.lang.reflect.Method m = c.getDeclaredMethod("GWT2Thrift", Object.class);
+ return m.invoke(obj, o);
+ }
+ } else {
+ throw new TException("Cannot convert class " + o.getClass().getName());
+ }
+ } catch (TException te) {
+ throw te;
+ } catch (Exception e) {
+ throw new TException(e.getMessage(), e.getCause());
+ }
+ }
+}
Index: lib/java/src/org/apache/thrift/gwt/GWT.gwt.xml
===================================================================
--- lib/java/src/org/apache/thrift/gwt/GWT.gwt.xml (revision 0)
+++ lib/java/src/org/apache/thrift/gwt/GWT.gwt.xml (revision 0)
@@ -0,0 +1,3 @@
+<module>
+ <source path="" />
+</module>
Index: lib/java/src/org/apache/thrift/gwt/HashCodeBuilder.java
===================================================================
--- lib/java/src/org/apache/thrift/gwt/HashCodeBuilder.java (revision 0)
+++ lib/java/src/org/apache/thrift/gwt/HashCodeBuilder.java (revision 0)
@@ -0,0 +1,622 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.thrift.gwt;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * <p>
+ * Assists in implementing {@link Object#hashCode()} methods.
+ * </p>
+ *
+ * <p>
+ * This class enables a good <code>hashCode</code> method to be built for any class. It follows the rules laid out in
+ * the book <a href="http://java.sun.com/docs/books/effective/index.html">Effective Java</a> by Joshua Bloch. Writing a
+ * good <code>hashCode</code> method is actually quite difficult. This class aims to simplify the process.
+ * </p>
+ *
+ * <p>
+ * All relevant fields from the object should be included in the <code>hashCode</code> method. Derived fields may be
+ * excluded. In general, any field used in the <code>equals</code> method must be used in the <code>hashCode</code>
+ * method.
+ * </p>
+ *
+ * <p>
+ * To use this class write code as follows:
+ * </p>
+ *
+ * <pre>
+ * public class Person {
+ * String name;
+ * int age;
+ * boolean smoker;
+ * ...
+ *
+ * public int hashCode() {
+ * // you pick a hard-coded, randomly chosen, non-zero, odd number
+ * // ideally different for each class
+ * return new HashCodeBuilder(17, 37).
+ * append(name).
+ * append(age).
+ * append(smoker).
+ * toHashCode();
+ * }
+ * }
+ * </pre>
+ *
+ * <p>
+ * If required, the superclass <code>hashCode()</code> can be added using {@link #appendSuper}.
+ * </p>
+ *
+ * <p>
+ * Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are
+ * usually private, the method, <code>reflectionHashCode</code>, uses <code>AccessibleObject.setAccessible</code>
+ * to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions
+ * are set up correctly. It is also slower than testing explicitly.
+ * </p>
+ *
+ * <p>
+ * A typical invocation for this method would look like:
+ * </p>
+ *
+ * <pre>
+ * public int hashCode() {
+ * return HashCodeBuilder.reflectionHashCode(this);
+ * }
+ * </pre>
+ *
+ * @author Stephen Colebourne
+ * @author Gary Gregory
+ * @author Pete Gieser
+ * @since 1.0
+ * @version $Id: HashCodeBuilder.java 564070 2007-08-09 01:58:11Z bayard $
+ */
+public class HashCodeBuilder {
+
+ /**
+ * Constant to use in building the hashCode.
+ */
+ private final int iConstant;
+
+ /**
+ * Running total of the hashCode.
+ */
+ private int iTotal = 0;
+
+ /**
+ * <p>
+ * Uses two hard coded choices for the constants needed to build a <code>hashCode</code>.
+ * </p>
+ */
+ public HashCodeBuilder() {
+ iConstant = 37;
+ iTotal = 17;
+ }
+
+ /**
+ * <p>
+ * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class,
+ * however this is not vital.
+ * </p>
+ *
+ * <p>
+ * Prime numbers are preferred, especially for the multiplier.
+ * </p>
+ *
+ * @param initialNonZeroOddNumber
+ * a non-zero, odd number used as the initial value
+ * @param multiplierNonZeroOddNumber
+ * a non-zero, odd number used as the multiplier
+ * @throws IllegalArgumentException
+ * if the number is zero or even
+ */
+ public HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) {
+ if (initialNonZeroOddNumber == 0) {
+ throw new IllegalArgumentException("HashCodeBuilder requires a non zero initial value");
+ }
+ if (initialNonZeroOddNumber % 2 == 0) {
+ throw new IllegalArgumentException("HashCodeBuilder requires an odd initial value");
+ }
+ if (multiplierNonZeroOddNumber == 0) {
+ throw new IllegalArgumentException("HashCodeBuilder requires a non zero multiplier");
+ }
+ if (multiplierNonZeroOddNumber % 2 == 0) {
+ throw new IllegalArgumentException("HashCodeBuilder requires an odd multiplier");
+ }
+ iConstant = multiplierNonZeroOddNumber;
+ iTotal = initialNonZeroOddNumber;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>boolean</code>.
+ * </p>
+ * <p>
+ * This adds <code>iConstant * 1</code> to the <code>hashCode</code> and not a <code>1231</code> or
+ * <code>1237</code> as done in java.lang.Boolean. This is in accordance with the <quote>Effective Java</quote>
+ * design.
+ * </p>
+ *
+ * @param value
+ * the boolean to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(boolean value) {
+ iTotal = iTotal * iConstant + (value ? 0 : 1);
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>boolean</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(boolean[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+
+ // -------------------------------------------------------------------------
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>byte</code>.
+ * </p>
+ *
+ * @param value
+ * the byte to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(byte value) {
+ iTotal = iTotal * iConstant + value;
+ return this;
+ }
+
+ // -------------------------------------------------------------------------
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>byte</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(byte[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>char</code>.
+ * </p>
+ *
+ * @param value
+ * the char to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(char value) {
+ iTotal = iTotal * iConstant + value;
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>char</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(char[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>double</code>.
+ * </p>
+ *
+ * @param value
+ * the double to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(double value) {
+ return append(doubleToLongBits(value));
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>double</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(double[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>float</code>.
+ * </p>
+ *
+ * @param value
+ * the float to add to the <code>hashCode</code>
+ * @return this
+ public HashCodeBuilder append(float value) {
+ iTotal = iTotal * iConstant + floatToIntBits(value);
+ return this;
+ }
+ */
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>float</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(float[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for an <code>int</code>.
+ * </p>
+ *
+ * @param value
+ * the int to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(int value) {
+ iTotal = iTotal * iConstant + value;
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for an <code>int</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(int[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>long</code>.
+ * </p>
+ * <p>
+ *
+ * @param value
+ * the long to add to the <code>hashCode</code>
+ * @return this
+ */
+ // NOTE: This method uses >> and not >>> as Effective Java and
+ // Long.hashCode do. Ideally we should switch to >>> at
+ // some stage. There are backwards compat issues, so
+ // that will have to wait for the time being. cf LANG-342.
+ public HashCodeBuilder append(long value) {
+ iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32)));
+ return this;
+ }
+
+ /**
+ * <p>
+ * Append a <code>hashCode</code> for a <code>long</code> array.
+ * </p>
+ *
+ * @param array
+ * the array to add to the <code>hashCode</code>
+ * @return this
+ */
+ public HashCodeBuilder append(long[] array) {
+ if (array == null) {
+ iTotal = iTotal * iConstant;
+ } else {
+ for (int i = 0; i < array.length; i++) {
+ append(array[i]);
+ }
+ }
+ return this;
+ }
+