11package com .pubnub .api .endpoints .presence ;
22
33
4+ import com .fasterxml .jackson .databind .JsonNode ;
45import com .pubnub .api .PubNub ;
56import com .pubnub .api .PubNubException ;
67import com .pubnub .api .PubNubUtil ;
1920
2021import java .util .ArrayList ;
2122import java .util .HashMap ;
23+ import java .util .Iterator ;
2224import java .util .List ;
2325import java .util .Map ;
2426
2527@ Accessors (chain = true , fluent = true )
26- public class HereNow extends Endpoint <Envelope <Object >, PNHereNowResult > {
28+ public class HereNow extends Endpoint <Envelope <JsonNode >, PNHereNowResult > {
2729 @ Setter
2830 private List <String > channels ;
2931 @ Setter
@@ -48,7 +50,7 @@ protected void validateParams() throws PubNubException {
4850 }
4951
5052 @ Override
51- protected Call <Envelope <Object >> doWork (Map <String , String > params ) {
53+ protected Call <Envelope <JsonNode >> doWork (Map <String , String > params ) {
5254
5355 if (includeState == null ) {
5456 includeState = false ;
@@ -86,7 +88,7 @@ protected Call<Envelope<Object>> doWork(Map<String, String> params) {
8688 }
8789
8890 @ Override
89- protected PNHereNowResult createResponse (Response <Envelope <Object >> input ) {
91+ protected PNHereNowResult createResponse (Response <Envelope <JsonNode >> input ) {
9092 PNHereNowResult herenowData ;
9193
9294 if (channels .size () > 1 || channelGroups .size () > 0 ) {
@@ -98,7 +100,7 @@ protected PNHereNowResult createResponse(Response<Envelope<Object>> input) {
98100 return herenowData ;
99101 }
100102
101- private PNHereNowResult parseSingleChannelResponse (Envelope <Object > input ) {
103+ private PNHereNowResult parseSingleChannelResponse (Envelope <JsonNode > input ) {
102104 PNHereNowResult hereNowData = PNHereNowResult .builder ()
103105 .totalChannels (1 )
104106 .channels (new HashMap <String , PNHereNowChannelData >())
@@ -117,72 +119,55 @@ private PNHereNowResult parseSingleChannelResponse(Envelope<Object> input) {
117119 return hereNowData ;
118120 }
119121
120- private PNHereNowResult parseMultipleChannelResponse (Object input ) {
121- Map <String , Object > parsedInput = (Map <String , Object >) input ;
122-
122+ private PNHereNowResult parseMultipleChannelResponse (JsonNode input ) {
123123 PNHereNowResult hereNowData = PNHereNowResult .builder ()
124124 .channels (new HashMap <String , PNHereNowChannelData >())
125- .totalChannels (( Integer ) parsedInput .get ("total_channels" ))
126- .totalOccupancy (( Integer ) parsedInput .get ("total_occupancy" ))
125+ .totalChannels (input .get ("total_channels" ). asInt ( ))
126+ .totalOccupancy (input .get ("total_occupancy" ). asInt ( ))
127127 .build ();
128128
129- Map <String , Object > channelsParam = (HashMap <String , Object >) parsedInput .get ("channels" );
130-
131- for (Map .Entry <String , Object > entry : channelsParam .entrySet ()) {
132- Map <String , Object > channel = (Map <String , Object >) channelsParam .get (entry .getKey ());
129+ for (Iterator <Map .Entry <String , JsonNode >> it = input .get ("channels" ).fields (); it .hasNext ();) {
130+ Map .Entry <String , JsonNode > entry = it .next ();
133131
134132 PNHereNowChannelData .PNHereNowChannelDataBuilder hereNowChannelData = PNHereNowChannelData .builder ()
135133 .channelName (entry .getKey ())
136- .occupancy (( Integer ) channel .get ("occupancy" ));
134+ .occupancy (entry . getValue () .get ("occupancy" ). asInt ( ));
137135
138136 if (includeUUIDs ) {
139- hereNowChannelData .occupants (prepareOccupantData (channel .get ("uuids" )));
137+ hereNowChannelData .occupants (prepareOccupantData (entry . getValue () .get ("uuids" )));
140138 } else {
141139 hereNowChannelData .occupants (null );
142140 }
143141
144142 hereNowData .getChannels ().put (entry .getKey (), hereNowChannelData .build ());
145143 }
146144
147- // for (String channelName : channelsParam.keySet()) {
148- // Map<String, Object> channel = (Map<String, Object>) channelsParam.get(channelName);
149- //
150- // PNHereNowChannelData.PNHereNowChannelDataBuilder hereNowChannelData = PNHereNowChannelData.builder()
151- // .channelName(channelName)
152- // .occupancy((Integer) channel.get("occupancy"));
153- //
154- // if (includeUUIDs) {
155- // hereNowChannelData.occupants(prepareOccupantData(channel.get("uuids")));
156- // } else {
157- // hereNowChannelData.occupants(null);
158- // }
159- //
160- // hereNowData.getChannels().put(channelName, hereNowChannelData.build());
161- // }
162-
163145 return hereNowData ;
164146 }
165147
166- private List <PNHereNowOccupantData > prepareOccupantData (Object input ) {
167- List <PNHereNowOccupantData > occupantsResults = new ArrayList <PNHereNowOccupantData >();
148+ private List <PNHereNowOccupantData > prepareOccupantData (JsonNode input ) {
149+ List <PNHereNowOccupantData > occupantsResults = new ArrayList <>();
168150
169151 if (includeState != null && includeState ) {
170- for (Map <String , Object > occupant : (List <Map <String , Object >>) input ) {
152+ for (Iterator <JsonNode > it = input .elements (); it .hasNext ();) {
153+ JsonNode occupant = it .next ();
171154 PNHereNowOccupantData .PNHereNowOccupantDataBuilder hereNowOccupantData = PNHereNowOccupantData .builder ();
172- hereNowOccupantData .uuid (( String ) occupant .get ("uuid" ));
155+ hereNowOccupantData .uuid (occupant .get ("uuid" ). asText ( ));
173156 hereNowOccupantData .state (occupant .get ("state" ));
174157
175158 occupantsResults .add (hereNowOccupantData .build ());
176159 }
177160 } else {
178- for (String occupant : (List <String >) input ) {
161+ for (Iterator <JsonNode > it = input .elements (); it .hasNext ();) {
162+ JsonNode occupant = it .next ();
179163 PNHereNowOccupantData .PNHereNowOccupantDataBuilder hereNowOccupantData = PNHereNowOccupantData .builder ();
180- hereNowOccupantData .uuid (occupant );
164+ hereNowOccupantData .uuid (occupant . asText () );
181165 hereNowOccupantData .state (null );
182166
183167 occupantsResults .add (hereNowOccupantData .build ());
184168 }
185169 }
170+
186171 return occupantsResults ;
187172 }
188173
0 commit comments