1 | package pl.zankowski.iextrading4j.client.socket.manager; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | import com.google.common.collect.Maps; | |
6 | import io.socket.client.Socket; | |
7 | import pl.zankowski.iextrading4j.client.mapper.IEXTradingMapperContextResolver; | |
8 | ||
9 | import java.io.IOException; | |
10 | import java.util.Arrays; | |
11 | import java.util.Map; | |
12 | import java.util.function.Consumer; | |
13 | ||
14 | public class SocketManager { | |
15 | ||
16 | private final Map<SocketRequest, Socket> socketStore = Maps.newHashMap(); | |
17 | ||
18 | private final SocketWrapper socketWrapper; | |
19 | private final String baseUrl; | |
20 | private final ObjectMapper objectMapper; | |
21 | ||
22 | public SocketManager(final SocketWrapper socketWrapper, final String baseUrl) { | |
23 | this.socketWrapper = socketWrapper; | |
24 | this.baseUrl = baseUrl; | |
25 | this.objectMapper = new IEXTradingMapperContextResolver().getContext(SocketManager.class); | |
26 | } | |
27 | ||
28 | public <T> void subscribe(final SocketRequest<T> request, final Consumer<T> consumer) { | |
29 | final String url = createURL(request); | |
30 | ||
31 | try { | |
32 |
1
1. subscribe : negated conditional → KILLED |
if (socketStore.containsKey(request)) { |
33 | return; | |
34 | } | |
35 | ||
36 | final Socket socket = socketWrapper.socket(url, true).connect(); | |
37 | socket.emit("subscribe", mapParam(request.getParam())) | |
38 |
1
1. lambda$subscribe$0 : removed call to pl/zankowski/iextrading4j/client/socket/manager/SocketManager::processResponse → KILLED |
.on("message", args -> processResponse(args, request, consumer)); |
39 | ||
40 | socketStore.put(request, socket); | |
41 | } catch (Exception e) { | |
42 | throw new IllegalStateException(e.getMessage(), e); | |
43 | } | |
44 | } | |
45 | ||
46 | private String mapParam(final Object param) throws JsonProcessingException { | |
47 |
1
1. mapParam : negated conditional → KILLED |
if (param instanceof String) { |
48 |
1
1. mapParam : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapParam → NO_COVERAGE |
return String.valueOf(param); |
49 | } | |
50 |
1
1. mapParam : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapParam → KILLED |
return objectMapper.writeValueAsString(param); |
51 | } | |
52 | ||
53 | public <T> void unsubscribe(final SocketRequest<T> request) { | |
54 | final Socket socket = socketStore.remove(request); | |
55 |
1
1. unsubscribe : negated conditional → KILLED |
if (socket == null) { |
56 | return; | |
57 | } | |
58 | ||
59 | socket.disconnect(); | |
60 | } | |
61 | ||
62 | private <T> void processResponse(final Object[] args, final SocketRequest<T> request, final Consumer<T> consumer) { | |
63 |
1
1. processResponse : removed call to java/util/stream/Stream::forEach → KILLED |
Arrays.stream(args).forEach(arg -> { |
64 | try { | |
65 |
1
1. lambda$processResponse$1 : removed call to java/util/function/Consumer::accept → KILLED |
consumer.accept(mapObject(arg, request)); |
66 | } catch (IOException e) { | |
67 | throw new IllegalStateException(e.getMessage(), e); | |
68 | } | |
69 | }); | |
70 | } | |
71 | ||
72 | private <T> T mapObject(final Object arg, final SocketRequest<T> request) throws IOException { | |
73 |
1
1. mapObject : replaced return value with null for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapObject → KILLED |
return objectMapper.readValue(String.valueOf(arg), request.getResponseType()); |
74 | } | |
75 | ||
76 | private <R> String createURL(final SocketRequest<R> restRequest) { | |
77 |
1
1. createURL : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::createURL → KILLED |
return new StringBuilder() |
78 | .append(getServicePath()) | |
79 | .append(restRequest.getPath()) | |
80 | .toString(); | |
81 | } | |
82 | ||
83 | private String getServicePath() { | |
84 |
1
1. getServicePath : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::getServicePath → KILLED |
return baseUrl; |
85 | } | |
86 | ||
87 | } | |
Mutations | ||
32 |
1.1 |
|
38 |
1.1 |
|
47 |
1.1 |
|
48 |
1.1 |
|
50 |
1.1 |
|
55 |
1.1 |
|
63 |
1.1 |
|
65 |
1.1 |
|
73 |
1.1 |
|
77 |
1.1 |
|
84 |
1.1 |