Working Java v.1.0 for arbitrary calls of callback objects
[iot2.git] / iotjava / iotrmi / Java / basics / TestClass.java
1 import java.util.Set;
2 import java.util.List;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 public class TestClass implements TestClassInterface {
7
8         /**
9          * Class Properties
10          */
11         private int intA;
12         private float floatB;
13         private String stringC;
14         private List<CallBackInterfaceWithCallBack> cblist;
15
16         /**
17          * Constructors
18          */
19         public TestClass() {
20
21                 intA = 1;
22                 floatB = 2;
23                 stringC = "345";
24                 cblist = new ArrayList<CallBackInterfaceWithCallBack>();
25         }
26
27
28         public TestClass(int _int, float _float, String _string) {
29
30                 intA = _int;
31                 floatB = _float;
32                 stringC = _string;
33                 cblist = new ArrayList<CallBackInterfaceWithCallBack>();
34         }
35
36         
37         public int callBack() {
38                 
39                 int sum = 0;
40                 System.out.println("Callback called! cblist: " + cblist.size());
41                 for (CallBackInterfaceWithCallBack cb : cblist) {
42                         sum = sum + cb.printInt();
43                         //TestClass tci = new TestClass();
44                         cb.needCallback(this);
45                         //cb.needCallback(this);
46                         //cb.needCallback(tci);
47                         System.out.println("Inside the loop!");
48                 }
49                 System.out.println("Executed callback of callback! Returning value: " + sum + "\n\n");
50                 return sum;
51         }
52
53         // Callback
54         //public void registerCallback(CallBackInterface _cb) {
55         public void registerCallback(CallBackInterfaceWithCallBack _cb) {
56
57                 cblist.add(_cb);
58                 System.out.println("Registering callback object! Items: " + cblist.size());
59         }
60
61
62         public void registerCallbackArray(CallBackInterfaceWithCallBack _cb[]) {
63
64                 for (CallBackInterfaceWithCallBack cb : _cb) {
65                         cblist.add(cb);
66                         System.out.println("Registering callback objects in array!");
67                 }
68         }
69
70
71         public void registerCallbackList(List<CallBackInterfaceWithCallBack> _cb) {
72
73                 for (CallBackInterfaceWithCallBack cb : _cb) {
74                         cblist.add(cb);
75                         System.out.println("Registering callback objects in list!");
76                 }
77         }
78
79
80         public void registerCallbackComplex(int in, List<CallBackInterfaceWithCallBack> _cb, double db) {
81
82                 for (CallBackInterfaceWithCallBack cb : _cb) {
83                         cblist.add(cb);
84                         System.out.println("Registering callback objects in list!");
85                 }
86
87                 System.out.println("Integer: " + in);
88                 System.out.println("Double: " + db);
89         }
90
91
92         // Single variables
93         public byte getByte(byte in) {
94
95                 return in;
96         }
97
98
99         public short getShort(short in) {
100
101                 return in;
102         }
103
104
105         public long getLong(long in) {
106
107                 return in;
108         }
109
110
111         public float getFloat(float in) {
112
113                 return in;
114         }
115
116
117         public double getDouble(double in) {
118
119                 return in;
120         }
121
122
123         public boolean getBoolean(boolean in) {
124
125                 return in;
126         }
127
128
129         public char getChar(char in) {
130
131                 return in;
132         }
133
134
135         // Arrays
136         public byte[] getByteArray(byte[] in) {
137
138                 return in;
139         }
140
141
142         public short[] getShortArray(short[] in) {
143
144                 return in;
145         }
146
147
148         public long[] getLongArray(long[] in) {
149
150                 return in;
151         }
152
153
154         public float[] getFloatArray(float[] in) {
155
156                 return in;
157         }
158
159
160         public double[] getDoubleArray(double[] in) {
161
162                 return in;
163         }
164
165
166         public boolean[] getBooleanArray(boolean[] in) {
167
168                 return in;
169         }
170
171
172         public char[] getCharArray(char[] in) {
173
174                 return in;
175         }
176
177
178         // Lists
179         public List<Byte> getByteList(List<Byte> in) {
180
181                 return in;
182         }
183
184
185         public List<Short> getShortList(List<Short> in) {
186
187                 return in;
188         }
189
190
191         public List<Long> getLongList(List<Long> in) {
192
193                 return in;
194         }
195
196
197         public List<Float> getFloatList(List<Float> in) {
198
199                 return in;
200         }
201
202
203         public List<Double> getDoubleList(List<Double> in) {
204
205                 return in;
206         }
207
208
209         public List<Boolean> getBooleanList(List<Boolean> in) {
210
211                 return in;
212         }
213
214
215         public List<Character> getCharList(List<Character> in) {
216
217                 return in;
218         }
219
220
221         // Other functions
222         public int getA() {
223
224                 return intA;
225         }
226
227
228         public void setA(int _int) {
229
230                 intA = _int;
231         }
232
233
234         public void setB(float _float) {
235
236                 floatB = _float;
237         }
238
239
240         public void setC(String _string) {
241
242                 stringC = _string;
243         }
244
245
246         // Enum
247         public Enum handleEnum(Enum en) {
248
249                 System.out.println("Enum: " + en);
250                                 
251                 return en;
252         }
253
254
255         public Enum[] handleEnumArray(Enum[] en) {
256
257                 for (Enum e : en) {
258                         System.out.println("Enum: " + e);
259                 }
260                 
261                 return en;
262         }
263
264
265         public List<Enum> handleEnumList(List<Enum> en) {
266
267                 for (Enum e : en) {
268                         System.out.println("Enum: " + e);
269                 }
270                 
271                 return en;
272         }
273
274
275         public Enum handleEnumComplex(Enum en, int i, char c) {
276
277                 System.out.println("Enum: " + en);
278                 System.out.println("Integer: " + i);
279                 System.out.println("Char: " + c);
280                 
281                 return en;
282         }
283
284
285         public Enum[] handleEnumComplex2(Enum[] en, int in, char c) {
286
287                 for (Enum e : en) {
288                         System.out.println("Enum: " + e);
289                 }
290                 System.out.println("Integer: " + in);
291                 System.out.println("Char: " + c);
292                 
293                 return en;
294         }
295
296
297         public Enum[] handleEnumTwo(Enum en1[], Enum en2[]) {
298
299                 for (Enum e : en1) {
300                         System.out.println("Enum1: " + e);
301                 }
302                 for (Enum e : en2) {
303                         System.out.println("Enum2: " + e);
304                 }
305                 
306                 return en1;
307         }
308
309
310         public Enum[] handleEnumThree(Enum en1[], Enum en2[], List<Struct> str1, List<Struct> str2) {
311
312                 for (Enum e : en1) {
313                         System.out.println("Enum1: " + e);
314                 }
315                 for (Enum e : en2) {
316                         System.out.println("Enum2: " + e);
317                 }
318                 
319                 return en1;
320         }
321
322
323         // Struct
324         public Struct handleStruct(Struct str) {
325
326                 System.out.println("Name: " + str.name);
327                 System.out.println("Value: " + str.value);
328                 System.out.println("Year: " + str.year);
329
330
331                 Struct test = new Struct();
332                 test.name = "Anonymous";
333                 test.value = 1.33f;
334                 test.year = 2016;
335
336                 str = test;
337
338                 return str;
339         }
340
341
342         public Struct[] handleStructArray(Struct str[]) {
343
344                 for (Struct st : str) {
345                         System.out.println("Name: " + st.name);
346                         System.out.println("Value: " + st.value);
347                         System.out.println("Year: " + st.year);
348                 }
349
350                 Struct test = new Struct();
351                 test.name = "Anonymous";
352                 test.value = 1.33f;
353                 test.year = 2016;
354
355                 str[0] = test;
356
357                 return str;
358         }
359
360
361         public List<Struct> handleStructList(List<Struct> str) {
362
363                 for (Struct st : str) {
364                         System.out.println("Name: " + st.name);
365                         System.out.println("Value: " + st.value);
366                         System.out.println("Year: " + st.year);
367                 }
368
369                 Struct test = new Struct();
370                 test.name = "Tests";
371                 test.value = 1.34f;
372                 test.year = 2017;
373
374                 str.add(test);
375
376                 return str;
377         }
378
379
380         public Struct handleStructComplex(int in, char c, Struct str) {
381
382                 System.out.println("Name: " + str.name);
383                 System.out.println("Value: " + str.value);
384                 System.out.println("Year: " + str.year);
385
386                 System.out.println("Integer: " + in);
387                 System.out.println("Char: " + c);
388
389                 Struct test = new Struct();
390                 test.name = "Anonymous";
391                 test.value = 1.33f;
392                 test.year = 2016;
393
394                 str = test;
395
396                 return str;
397         }
398
399
400         public List<Struct> handleStructComplex2(int in, char c, Struct str[]) {
401
402                 for (Struct st : str) {
403                         System.out.println("Name: " + st.name);
404                         System.out.println("Value: " + st.value);
405                         System.out.println("Year: " + st.year);
406                 }
407
408                 System.out.println("Integer: " + in);
409                 System.out.println("Char: " + c);
410
411                 return new ArrayList<Struct>(Arrays.asList(str));
412         }
413
414
415         public Enum[] handleEnumStruct(Enum en[], List<Struct> str, char c) {
416
417                 for (Struct st : str) {
418                         System.out.println("Name: " + st.name);
419                         System.out.println("Value: " + st.value);
420                         System.out.println("Year: " + st.year);
421                 }
422
423                 System.out.println("Char: " + c);
424
425                 return en;
426         }
427
428
429         public List<Struct> handleStructTwo(List<Struct> str1, List<Struct> str2) {
430
431                 for (Struct st : str1) {
432                         System.out.println("Name: " + st.name);
433                         System.out.println("Value: " + st.value);
434                         System.out.println("Year: " + st.year);
435                 }
436
437                 return str1;
438         }
439
440
441         public List<Struct> handleStructThree(List<Struct> str1, List<Struct> str2, List<Struct> str3) {
442
443                 for (Struct st : str1) {
444                         System.out.println("Name: " + st.name);
445                         System.out.println("Value: " + st.value);
446                         System.out.println("Year: " + st.year);
447                 }
448
449                 return str2;
450         }
451
452
453         public Enum[] handleAll(Enum en[], List<Struct> str, char c, List<CallBackInterfaceWithCallBack> _cb) {
454
455                 for (CallBackInterfaceWithCallBack cb : _cb) {
456                         cblist.add(cb);
457                         System.out.println("Registering callback objects in list!");
458                 }
459
460                 for (Struct st : str) {
461                         System.out.println("Name: " + st.name);
462                         System.out.println("Value: " + st.value);
463                         System.out.println("Year: " + st.year);
464                 }
465
466                 System.out.println("Char: " + c);
467
468                 return en;
469         }
470
471
472         public Enum[] handleCallbackEnum(Enum en[], char c, List<CallBackInterfaceWithCallBack> _cb) {
473
474                 for (CallBackInterfaceWithCallBack cb : _cb) {
475                         cblist.add(cb);
476                         System.out.println("Registering callback objects in list!");
477                 }
478
479                 System.out.println("Char: " + c);
480
481                 return en;
482         }
483
484
485         public Enum[] handleAllTwo(Enum en1[], Enum en2[], List<Struct> str1, List<Struct> str2, char c, List<CallBackInterfaceWithCallBack> _cb1, List<CallBackInterfaceWithCallBack> _cb2) {
486
487                 for (CallBackInterfaceWithCallBack cb : _cb1) {
488                         cblist.add(cb);
489                         System.out.println("Registering callback objects in list!");
490                 }
491
492                 for (Struct st : str1) {
493                         System.out.println("Name: " + st.name);
494                         System.out.println("Value: " + st.value);
495                         System.out.println("Year: " + st.year);
496                 }
497
498                 System.out.println("Char: " + c);
499
500                 return en1;
501         }
502
503
504         // Getters
505         public String sumArray(String[] newA) {
506
507                 String sum = "";
508                 for (String i : newA) 
509                         sum = sum + i;
510                 return sum;
511         }
512
513
514         public int setAndGetA(int newA) {
515
516                 intA = newA;
517                 return intA;
518         }
519
520
521         public int setACAndGetA(String newC, int newA) {
522
523                 stringC = newC;
524                 intA = newA;
525                 return intA;
526         }
527
528         public static void main(String[] args) {
529
530                 TestClass tc = new TestClass();
531                 System.out.println("Get short: " + tc.getShort((short) 1234));
532         }
533 }