From 7106667793427d7075ef29ab67912e1d2a91b9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrejs=20Mivre=C5=86iks?= Date: Sat, 8 Oct 2016 17:15:00 +0300 Subject: [PATCH] Format code according to Java conventions --- .../java/eu/mivrenik/stomp/StompClient.java | 324 +++++++++--------- .../java/eu/mivrenik/stomp/StompCommand.java | 46 +-- .../java/eu/mivrenik/stomp/StompFrame.java | 144 ++++---- .../java/eu/mivrenik/stomp/StompHeader.java | 38 +- .../mivrenik/stomp/StompMessageListener.java | 2 +- .../eu/mivrenik/stomp/StompSubscription.java | 34 +- 6 files changed, 294 insertions(+), 294 deletions(-) diff --git a/src/main/java/eu/mivrenik/stomp/StompClient.java b/src/main/java/eu/mivrenik/stomp/StompClient.java index ee48bfb..dd51540 100644 --- a/src/main/java/eu/mivrenik/stomp/StompClient.java +++ b/src/main/java/eu/mivrenik/stomp/StompClient.java @@ -43,208 +43,208 @@ import com.neovisionaries.ws.client.WebSocketState; * */ public class StompClient { - private static final String STOMP_VERSION = "1.2"; - private final Map subscriptions = new HashMap<>(); - private WebSocket webSocket; - private boolean stompConnected; + private static final String STOMP_VERSION = "1.2"; + private final Map subscriptions = new HashMap<>(); + private WebSocket webSocket; + private boolean stompConnected; - public StompClient(String url) throws IOException { - this.webSocket = new WebSocketFactory().createSocket(url); - this.webSocket.addListener(webSocketListener); - } + public StompClient(String url) throws IOException { + this.webSocket = new WebSocketFactory().createSocket(url); + this.webSocket.addListener(webSocketListener); + } - public void connect() throws WebSocketException, IOException { - if (webSocket.getState() != WebSocketState.CREATED) { - webSocket = webSocket.recreate(); - } - webSocket.connect(); - connectStomp(); - } + public void connect() throws WebSocketException, IOException { + if (webSocket.getState() != WebSocketState.CREATED) { + webSocket = webSocket.recreate(); + } + webSocket.connect(); + connectStomp(); + } - protected void connectStomp() { - if (stompConnected) - return; - Map headers = new HashMap<>(); - headers.put(StompHeader.ACCEPT_VERSION.toString(), STOMP_VERSION); - headers.put(StompHeader.HOST.toString(), webSocket.getURI().getHost()); - webSocket.sendText(new StompFrame(StompCommand.CONNECT, headers).toString()); - } + protected void connectStomp() { + if (stompConnected) + return; + Map headers = new HashMap<>(); + headers.put(StompHeader.ACCEPT_VERSION.toString(), STOMP_VERSION); + headers.put(StompHeader.HOST.toString(), webSocket.getURI().getHost()); + webSocket.sendText(new StompFrame(StompCommand.CONNECT, headers).toString()); + } - public void disconnect() { - disconnectStomp(); - webSocket.disconnect(); - } + public void disconnect() { + disconnectStomp(); + webSocket.disconnect(); + } - protected void disconnectStomp() { - if (!stompConnected) - return; + protected void disconnectStomp() { + if (!stompConnected) + return; - webSocket.sendText(new StompFrame(StompCommand.DISCONNECT).toString()); - } + webSocket.sendText(new StompFrame(StompCommand.DISCONNECT).toString()); + } - public boolean isConnected() { - return stompConnected; - } + public boolean isConnected() { + return stompConnected; + } - public void send(String destination, String message) { - send(destination, message, null); - } + public void send(String destination, String message) { + send(destination, message, null); + } - public void send(String destination, String message, Map headers) { - if (headers == null) - headers = new HashMap<>(); + public void send(String destination, String message, Map headers) { + if (headers == null) + headers = new HashMap<>(); - headers.put(StompHeader.DESTINATION.toString(), destination); - webSocket.sendText(new StompFrame(StompCommand.SEND, headers, message).toString()); - } + headers.put(StompHeader.DESTINATION.toString(), destination); + webSocket.sendText(new StompFrame(StompCommand.SEND, headers, message).toString()); + } - public StompSubscription subscribe(String destination, StompMessageListener listener) { - StompSubscription subscription = new StompSubscription(UUID.randomUUID().hashCode(), destination, listener); - Map headers = new HashMap<>(); - headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId())); - headers.put(StompHeader.DESTINATION.toString(), subscription.getDestination()); + public StompSubscription subscribe(String destination, StompMessageListener listener) { + StompSubscription subscription = new StompSubscription(UUID.randomUUID().hashCode(), destination, listener); + Map headers = new HashMap<>(); + headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId())); + headers.put(StompHeader.DESTINATION.toString(), subscription.getDestination()); - webSocket.sendText(new StompFrame(StompCommand.SUBSCRIBE, headers).toString()); - subscriptions.put(subscription.getId(), subscription); - return subscription; - } + webSocket.sendText(new StompFrame(StompCommand.SUBSCRIBE, headers).toString()); + subscriptions.put(subscription.getId(), subscription); + return subscription; + } - public void unsubscribe(StompSubscription subscription) { - Map headers = new HashMap<>(); - headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId())); - webSocket.sendText(new StompFrame(StompCommand.UNSUBSCRIBE, headers).toString()); - subscriptions.remove(subscription.getId()); - } + public void unsubscribe(StompSubscription subscription) { + Map headers = new HashMap<>(); + headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId())); + webSocket.sendText(new StompFrame(StompCommand.UNSUBSCRIBE, headers).toString()); + subscriptions.remove(subscription.getId()); + } - public void unsubscribeAll(String destination) { - for (StompSubscription s : subscriptions.values()) - if (s.getDestination().equals(destination)) - unsubscribe(s); - } + public void unsubscribeAll(String destination) { + for (StompSubscription s : subscriptions.values()) + if (s.getDestination().equals(destination)) + unsubscribe(s); + } - private final WebSocketListener webSocketListener = new WebSocketListener() { + private final WebSocketListener webSocketListener = new WebSocketListener() { - @Override - public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { - } + @Override + public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { + } - @Override - public void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception { - } + @Override + public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception { + } - @Override - public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onConnected(WebSocket websocket, Map> headers) throws Exception { - } + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + } - @Override - public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception { - } + @Override + public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception { + } - @Override - public void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, - WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { - stompConnected = false; - } + @Override + public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, + WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { + stompConnected = false; + } - @Override - public void onError(WebSocket websocket, WebSocketException cause) throws Exception { - } + @Override + public void onError(WebSocket websocket, WebSocketException cause) throws Exception { + } - @Override - public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) - throws Exception { - } + @Override + public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) + throws Exception { + } - @Override - public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) - throws Exception { - } + @Override + public void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) + throws Exception { + } - @Override - public void onMessageError(WebSocket websocket, WebSocketException cause, List frames) - throws Exception { - } + @Override + public void onMessageError(WebSocket websocket, WebSocketException cause, List frames) + throws Exception { + } - @Override - public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) - throws Exception { - } + @Override + public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) + throws Exception { + } - @Override - public void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - } + @Override + public void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + } - @Override - public void onSendingHandshake(WebSocket websocket, String requestLine, List headers) - throws Exception { - } + @Override + public void onSendingHandshake(WebSocket websocket, String requestLine, List headers) + throws Exception { + } - @Override - public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception { - } + @Override + public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception { + } - @Override - public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { - StompFrame stompFrame = StompFrame.fromString(frame.getPayloadText()); + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + StompFrame stompFrame = StompFrame.fromString(frame.getPayloadText()); - switch (stompFrame.getCommand()) { - case CONNECTED: - stompConnected = true; - break; - case MESSAGE: - Integer subscriptionId = Integer - .valueOf(stompFrame.getHeaders().get(StompHeader.SUBSCRIPTION.toString())); + switch (stompFrame.getCommand()) { + case CONNECTED: + stompConnected = true; + break; + case MESSAGE: + Integer subscriptionId = Integer + .valueOf(stompFrame.getHeaders().get(StompHeader.SUBSCRIPTION.toString())); - subscriptions.get(subscriptionId).getListener().onMessage(stompFrame.getBody()); - break; - default: - break; - } - } + subscriptions.get(subscriptionId).getListener().onMessage(stompFrame.getBody()); + break; + default: + break; + } + } - @Override - public void onTextMessage(WebSocket websocket, String text) throws Exception { - } + @Override + public void onTextMessage(WebSocket websocket, String text) throws Exception { + } - @Override - public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) - throws Exception { - } + @Override + public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) + throws Exception { + } - @Override - public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception { - } - }; + @Override + public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception { + } + }; } diff --git a/src/main/java/eu/mivrenik/stomp/StompCommand.java b/src/main/java/eu/mivrenik/stomp/StompCommand.java index 3d7f5f2..8b7a5f2 100644 --- a/src/main/java/eu/mivrenik/stomp/StompCommand.java +++ b/src/main/java/eu/mivrenik/stomp/StompCommand.java @@ -26,32 +26,32 @@ package eu.mivrenik.stomp; * */ enum StompCommand { - CONNECT("CONNECT"), - CONNECTED("CONNECTED"), - DISCONNECT("DISCONNECT"), - ERROR("ERROR"), - MESSAGE("MESSAGE"), - RECEIPT("RECEIPT"), - SEND("SEND"), - SUBSCRIBE("SUBSCRIBE"), - UNSUBSCRIBE("UNSUBSCRIBE"); + CONNECT("CONNECT"), + CONNECTED("CONNECTED"), + DISCONNECT("DISCONNECT"), + ERROR("ERROR"), + MESSAGE("MESSAGE"), + RECEIPT("RECEIPT"), + SEND("SEND"), + SUBSCRIBE("SUBSCRIBE"), + UNSUBSCRIBE("UNSUBSCRIBE"); - private final String value; + private final String value; - public static StompCommand fromValue(String value) { - for (StompCommand c : StompCommand.values()) - if (c.value.equals(value)) - return c; + public static StompCommand fromValue(String value) { + for (StompCommand c : StompCommand.values()) + if (c.value.equals(value)) + return c; - throw new IllegalArgumentException("Unknown STOMP command:" + value); - } + throw new IllegalArgumentException("Unknown STOMP command:" + value); + } - private StompCommand(String value) { - this.value = value; - } + private StompCommand(String value) { + this.value = value; + } - @Override - public String toString() { - return this.value; - } + @Override + public String toString() { + return this.value; + } } diff --git a/src/main/java/eu/mivrenik/stomp/StompFrame.java b/src/main/java/eu/mivrenik/stomp/StompFrame.java index 4e7e213..f90891d 100644 --- a/src/main/java/eu/mivrenik/stomp/StompFrame.java +++ b/src/main/java/eu/mivrenik/stomp/StompFrame.java @@ -31,88 +31,88 @@ import java.util.Map; * */ class StompFrame { - private StompCommand command; - private Map headers = new HashMap<>(); - private String body; + private StompCommand command; + private Map headers = new HashMap<>(); + private String body; - public StompFrame(StompCommand command) { - this(command, null, null); - } + public StompFrame(StompCommand command) { + this(command, null, null); + } - public StompFrame(StompCommand command, Map headers) { - this(command, headers, null); - } + public StompFrame(StompCommand command, Map headers) { + this(command, headers, null); + } - public StompFrame(StompCommand command, Map headers, String body) { - this.command = command; - this.body = body; + public StompFrame(StompCommand command, Map headers, String body) { + this.command = command; + this.body = body; - if (headers != null) - for (String k : headers.keySet()) - this.headers.put(k, headers.get(k)); - } + if (headers != null) + for (String k : headers.keySet()) + this.headers.put(k, headers.get(k)); + } - public StompCommand getCommand() { - return command; - } + public StompCommand getCommand() { + return command; + } - public Map getHeaders() { - return headers; - } + public Map getHeaders() { + return headers; + } - public String getBody() { - return body; - } + public String getBody() { + return body; + } - public static StompFrame fromString(String data) { - String[] lines = data.split("\n"); - int pos = 0; - // Ignore empty lines - while (pos < lines.length && lines[pos].isEmpty()) - pos++; - // Command - StompCommand command = StompCommand.fromValue(lines[pos++]); - // Headers - Map headers = new HashMap<>(); - while (!lines[pos].isEmpty()) { - String[] keyValue = lines[pos].split(":"); - headers.put(keyValue[0], keyValue[1]); + public static StompFrame fromString(String data) { + String[] lines = data.split("\n"); + int pos = 0; + // Ignore empty lines + while (pos < lines.length && lines[pos].isEmpty()) + pos++; + // Command + StompCommand command = StompCommand.fromValue(lines[pos++]); + // Headers + Map headers = new HashMap<>(); + while (!lines[pos].isEmpty()) { + String[] keyValue = lines[pos].split(":"); + headers.put(keyValue[0], keyValue[1]); - pos++; - } - pos++; - // Body - StringBuilder body = new StringBuilder(); - for (; pos < lines.length; pos++) { - body.append(lines[pos]); - if (body.charAt(body.length() - 1) == '\0') { - body.deleteCharAt(body.length() - 1); - break; - } - } + pos++; + } + pos++; + // Body + StringBuilder body = new StringBuilder(); + for (; pos < lines.length; pos++) { + body.append(lines[pos]); + if (body.charAt(body.length() - 1) == '\0') { + body.deleteCharAt(body.length() - 1); + break; + } + } - return new StompFrame(command, headers, body.toString()); - } + return new StompFrame(command, headers, body.toString()); + } - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - // Command - sb.append(command); - sb.append('\n'); - // Headers - for (String k : headers.keySet()) { - sb.append(k); - sb.append(':'); - sb.append(headers.get(k)); - sb.append('\n'); - } - sb.append('\n'); - // Body - if (body != null) - sb.append(body); - sb.append('\0'); + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + // Command + sb.append(command); + sb.append('\n'); + // Headers + for (String k : headers.keySet()) { + sb.append(k); + sb.append(':'); + sb.append(headers.get(k)); + sb.append('\n'); + } + sb.append('\n'); + // Body + if (body != null) + sb.append(body); + sb.append('\0'); - return sb.toString(); - } + return sb.toString(); + } } diff --git a/src/main/java/eu/mivrenik/stomp/StompHeader.java b/src/main/java/eu/mivrenik/stomp/StompHeader.java index 2a923ce..c3551c9 100644 --- a/src/main/java/eu/mivrenik/stomp/StompHeader.java +++ b/src/main/java/eu/mivrenik/stomp/StompHeader.java @@ -26,26 +26,26 @@ package eu.mivrenik.stomp; * */ enum StompHeader { - ACCEPT_VERSION("accept-version"), - CONTENT_LENGTH("content-length"), - CONTENT_TYPE("content-type"), - DESTINATION("destination"), - HOST("host"), - ID("id"), - MESSAGE_ID("message-id"), - RECEIPT_ID("receipt-id"), - RECEIPT("receipt"), - SUBSCRIPTION("subscription"), - VERSION("version"); + ACCEPT_VERSION("accept-version"), + CONTENT_LENGTH("content-length"), + CONTENT_TYPE("content-type"), + DESTINATION("destination"), + HOST("host"), + ID("id"), + MESSAGE_ID("message-id"), + RECEIPT_ID("receipt-id"), + RECEIPT("receipt"), + SUBSCRIPTION("subscription"), + VERSION("version"); - private final String value; + private final String value; - private StompHeader(String value) { - this.value = value; - } + private StompHeader(String value) { + this.value = value; + } - @Override - public String toString() { - return this.value; - } + @Override + public String toString() { + return this.value; + } } diff --git a/src/main/java/eu/mivrenik/stomp/StompMessageListener.java b/src/main/java/eu/mivrenik/stomp/StompMessageListener.java index 70d1ee2..bd9205b 100644 --- a/src/main/java/eu/mivrenik/stomp/StompMessageListener.java +++ b/src/main/java/eu/mivrenik/stomp/StompMessageListener.java @@ -26,5 +26,5 @@ package eu.mivrenik.stomp; * */ public interface StompMessageListener { - void onMessage(String message); + void onMessage(String message); } diff --git a/src/main/java/eu/mivrenik/stomp/StompSubscription.java b/src/main/java/eu/mivrenik/stomp/StompSubscription.java index b6c7884..b92d4f2 100644 --- a/src/main/java/eu/mivrenik/stomp/StompSubscription.java +++ b/src/main/java/eu/mivrenik/stomp/StompSubscription.java @@ -26,25 +26,25 @@ package eu.mivrenik.stomp; * */ public class StompSubscription { - private final Integer id; - private final String destination; - private final StompMessageListener listener; + private final Integer id; + private final String destination; + private final StompMessageListener listener; - public StompSubscription(Integer id, String destination, StompMessageListener listener) { - this.id = id; - this.destination = destination; - this.listener = listener; - } + public StompSubscription(Integer id, String destination, StompMessageListener listener) { + this.id = id; + this.destination = destination; + this.listener = listener; + } - public Integer getId() { - return id; - } + public Integer getId() { + return id; + } - public String getDestination() { - return destination; - } + public String getDestination() { + return destination; + } - public StompMessageListener getListener() { - return listener; - } + public StompMessageListener getListener() { + return listener; + } }