1
+ /*
2
+ * Copyright 2014 Red Hat, Inc.
3
+ *
4
+ * Red Hat licenses this file to you under the Apache License, version 2.0
5
+ * (the "License"); you may not use this file except in compliance with the
6
+ * License. You may obtain a copy of the License at:
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ * License for the specific language governing permissions and limitations
14
+ * under the License.
15
+ */
16
+
17
+ package io .vertx .example .reactivex .services .serviceproxy ;
18
+
19
+ import io .vertx .example .reactivex .services .serviceproxy .SomeDatabaseService ;
20
+ import io .vertx .core .eventbus .DeliveryOptions ;
21
+ import io .vertx .core .Vertx ;
22
+ import io .vertx .core .Future ;
23
+ import io .vertx .core .json .JsonObject ;
24
+ import io .vertx .core .json .JsonArray ;
25
+ import java .util .ArrayList ;
26
+ import java .util .HashSet ;
27
+ import java .util .List ;
28
+ import java .util .Map ;
29
+ import java .util .Set ;
30
+ import java .util .stream .Collectors ;
31
+ import java .util .function .Function ;
32
+ import io .vertx .serviceproxy .ProxyHelper ;
33
+ import io .vertx .serviceproxy .ServiceException ;
34
+ import io .vertx .serviceproxy .ServiceExceptionMessageCodec ;
35
+ import io .vertx .example .reactivex .services .serviceproxy .SomeDatabaseService ;
36
+ import io .vertx .core .json .JsonObject ;
37
+ import io .vertx .core .AsyncResult ;
38
+ import io .vertx .core .Handler ;
39
+
40
+ /*
41
+ Generated Proxy code - DO NOT EDIT
42
+ @author Roger the Robot
43
+ */
44
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
45
+ public class SomeDatabaseServiceVertxEBProxy implements SomeDatabaseService {
46
+
47
+ private Vertx _vertx ;
48
+ private String _address ;
49
+ private DeliveryOptions _options ;
50
+ private boolean closed ;
51
+
52
+ public SomeDatabaseServiceVertxEBProxy (Vertx vertx , String address ) {
53
+ this (vertx , address , null );
54
+ }
55
+
56
+ public SomeDatabaseServiceVertxEBProxy (Vertx vertx , String address , DeliveryOptions options ) {
57
+ this ._vertx = vertx ;
58
+ this ._address = address ;
59
+ this ._options = options ;
60
+ try {
61
+ this ._vertx .eventBus ().registerDefaultCodec (ServiceException .class ,
62
+ new ServiceExceptionMessageCodec ());
63
+ } catch (IllegalStateException ex ) {}
64
+ }
65
+
66
+ public SomeDatabaseService getDataById (int id , Handler <AsyncResult <JsonObject >> resultHandler ) {
67
+ if (closed ) {
68
+ resultHandler .handle (Future .failedFuture (new IllegalStateException ("Proxy is closed" )));
69
+ return this ;
70
+ }
71
+ JsonObject _json = new JsonObject ();
72
+ _json .put ("id" , id );
73
+ DeliveryOptions _deliveryOptions = (_options != null ) ? new DeliveryOptions (_options ) : new DeliveryOptions ();
74
+ _deliveryOptions .addHeader ("action" , "getDataById" );
75
+ _vertx .eventBus ().<JsonObject >send (_address , _json , _deliveryOptions , res -> {
76
+ if (res .failed ()) {
77
+ resultHandler .handle (Future .failedFuture (res .cause ()));
78
+ } else {
79
+ resultHandler .handle (Future .succeededFuture (res .result ().body ()));
80
+ }
81
+ });
82
+ return this ;
83
+ }
84
+
85
+
86
+ private List <Character > convertToListChar (JsonArray arr ) {
87
+ List <Character > list = new ArrayList <>();
88
+ for (Object obj : arr ) {
89
+ Integer jobj = (Integer )obj ;
90
+ list .add ((char )(int )jobj );
91
+ }
92
+ return list ;
93
+ }
94
+
95
+ private Set <Character > convertToSetChar (JsonArray arr ) {
96
+ Set <Character > set = new HashSet <>();
97
+ for (Object obj : arr ) {
98
+ Integer jobj = (Integer )obj ;
99
+ set .add ((char )(int )jobj );
100
+ }
101
+ return set ;
102
+ }
103
+
104
+ private <T > Map <String , T > convertMap (Map map ) {
105
+ if (map .isEmpty ()) {
106
+ return (Map <String , T >) map ;
107
+ }
108
+
109
+ Object elem = map .values ().stream ().findFirst ().get ();
110
+ if (!(elem instanceof Map ) && !(elem instanceof List )) {
111
+ return (Map <String , T >) map ;
112
+ } else {
113
+ Function <Object , T > converter ;
114
+ if (elem instanceof List ) {
115
+ converter = object -> (T ) new JsonArray ((List ) object );
116
+ } else {
117
+ converter = object -> (T ) new JsonObject ((Map ) object );
118
+ }
119
+ return ((Map <String , T >) map ).entrySet ()
120
+ .stream ()
121
+ .collect (Collectors .toMap (Map .Entry ::getKey , converter ::apply ));
122
+ }
123
+ }
124
+ private <T > List <T > convertList (List list ) {
125
+ if (list .isEmpty ()) {
126
+ return (List <T >) list ;
127
+ }
128
+
129
+ Object elem = list .get (0 );
130
+ if (!(elem instanceof Map ) && !(elem instanceof List )) {
131
+ return (List <T >) list ;
132
+ } else {
133
+ Function <Object , T > converter ;
134
+ if (elem instanceof List ) {
135
+ converter = object -> (T ) new JsonArray ((List ) object );
136
+ } else {
137
+ converter = object -> (T ) new JsonObject ((Map ) object );
138
+ }
139
+ return (List <T >) list .stream ().map (converter ).collect (Collectors .toList ());
140
+ }
141
+ }
142
+ private <T > Set <T > convertSet (List list ) {
143
+ return new HashSet <T >(convertList (list ));
144
+ }
145
+ }
0 commit comments