Skip to content

Commit aa12288

Browse files
committed
Avoid collection lookups in StompCommand
Issue: SPR-14636 (cherry picked from commit 8e98177)
1 parent cc33bfa commit aa12288

File tree

2 files changed

+141
-44
lines changed

2 files changed

+141
-44
lines changed
Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,79 +16,76 @@
1616

1717
package org.springframework.messaging.simp.stomp;
1818

19-
import java.util.Arrays;
20-
import java.util.Collection;
21-
import java.util.HashMap;
22-
import java.util.Map;
23-
2419
import org.springframework.messaging.simp.SimpMessageType;
2520

2621
/**
2722
* Represents a STOMP command.
2823
*
2924
* @author Rossen Stoyanchev
25+
* @author Juergen Hoeller
3026
* @since 4.0
3127
*/
3228
public enum StompCommand {
3329

3430
// client
35-
CONNECT,
36-
STOMP,
37-
DISCONNECT,
38-
SUBSCRIBE,
39-
UNSUBSCRIBE,
40-
SEND,
41-
ACK,
42-
NACK,
43-
BEGIN,
44-
COMMIT,
45-
ABORT,
31+
STOMP(SimpMessageType.CONNECT),
32+
CONNECT(SimpMessageType.CONNECT),
33+
DISCONNECT(SimpMessageType.DISCONNECT),
34+
SUBSCRIBE(SimpMessageType.SUBSCRIBE, true, true, false),
35+
UNSUBSCRIBE(SimpMessageType.UNSUBSCRIBE, false, true, false),
36+
SEND(SimpMessageType.MESSAGE, true, false, true),
37+
ACK(SimpMessageType.OTHER),
38+
NACK(SimpMessageType.OTHER),
39+
BEGIN(SimpMessageType.OTHER),
40+
COMMIT(SimpMessageType.OTHER),
41+
ABORT(SimpMessageType.OTHER),
4642

4743
// server
48-
CONNECTED,
49-
MESSAGE,
50-
RECEIPT,
51-
ERROR;
52-
53-
54-
private static Map<StompCommand, SimpMessageType> messageTypes = new HashMap<StompCommand, SimpMessageType>();
55-
static {
56-
messageTypes.put(StompCommand.CONNECT, SimpMessageType.CONNECT);
57-
messageTypes.put(StompCommand.STOMP, SimpMessageType.CONNECT);
58-
messageTypes.put(StompCommand.SEND, SimpMessageType.MESSAGE);
59-
messageTypes.put(StompCommand.MESSAGE, SimpMessageType.MESSAGE);
60-
messageTypes.put(StompCommand.SUBSCRIBE, SimpMessageType.SUBSCRIBE);
61-
messageTypes.put(StompCommand.UNSUBSCRIBE, SimpMessageType.UNSUBSCRIBE);
62-
messageTypes.put(StompCommand.DISCONNECT, SimpMessageType.DISCONNECT);
63-
}
44+
CONNECTED(SimpMessageType.OTHER),
45+
RECEIPT(SimpMessageType.OTHER),
46+
MESSAGE(SimpMessageType.MESSAGE, true, true, true),
47+
ERROR(SimpMessageType.OTHER, false, false, true);
48+
6449

65-
private static Collection<StompCommand> destinationRequired = Arrays.asList(SEND, SUBSCRIBE, MESSAGE);
66-
private static Collection<StompCommand> subscriptionIdRequired = Arrays.asList(SUBSCRIBE, UNSUBSCRIBE, MESSAGE);
67-
private static Collection<StompCommand> contentLengthRequired = Arrays.asList(SEND, MESSAGE, ERROR);
68-
private static Collection<StompCommand> bodyAllowed = Arrays.asList(SEND, MESSAGE, ERROR);
50+
private final SimpMessageType messageType;
6951

52+
private final boolean destination;
53+
54+
private final boolean subscriptionId;
55+
56+
private final boolean body;
57+
58+
59+
StompCommand(SimpMessageType messageType) {
60+
this(messageType, false, false, false);
61+
}
62+
63+
StompCommand(SimpMessageType messageType, boolean destination, boolean subscriptionId, boolean body) {
64+
this.messageType = messageType;
65+
this.destination = destination;
66+
this.subscriptionId = subscriptionId;
67+
this.body = body;
68+
}
7069

7170

7271
public SimpMessageType getMessageType() {
73-
SimpMessageType type = messageTypes.get(this);
74-
return (type != null) ? type : SimpMessageType.OTHER;
72+
return this.messageType;
7573
}
7674

7775
public boolean requiresDestination() {
78-
return destinationRequired.contains(this);
76+
return this.destination;
7977
}
8078

8179
public boolean requiresSubscriptionId() {
82-
return subscriptionIdRequired.contains(this);
80+
return this.subscriptionId;
8381
}
8482

8583
public boolean requiresContentLength() {
86-
return contentLengthRequired.contains(this);
84+
return this.body;
8785
}
8886

8987
public boolean isBodyAllowed() {
90-
return bodyAllowed.contains(this);
88+
return this.body;
9189
}
9290

9391
}
94-
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.messaging.simp.stomp;
18+
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.EnumMap;
22+
import java.util.Map;
23+
24+
import org.junit.Test;
25+
26+
import org.springframework.messaging.simp.SimpMessageType;
27+
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
* @author Juergen Hoeller
32+
*/
33+
public class StompCommandTests {
34+
35+
private static final Collection<StompCommand> destinationRequired =
36+
Arrays.asList(StompCommand.SEND, StompCommand.SUBSCRIBE, StompCommand.MESSAGE);
37+
38+
private static final Collection<StompCommand> subscriptionIdRequired =
39+
Arrays.asList(StompCommand.SUBSCRIBE, StompCommand.UNSUBSCRIBE, StompCommand.MESSAGE);
40+
41+
private static final Collection<StompCommand> contentLengthRequired =
42+
Arrays.asList(StompCommand.SEND, StompCommand.MESSAGE, StompCommand.ERROR);
43+
44+
private static final Collection<StompCommand> bodyAllowed =
45+
Arrays.asList(StompCommand.SEND, StompCommand.MESSAGE, StompCommand.ERROR);
46+
47+
private static final Map<StompCommand, SimpMessageType> messageTypes =
48+
new EnumMap<>(StompCommand.class);
49+
50+
static {
51+
messageTypes.put(StompCommand.STOMP, SimpMessageType.CONNECT);
52+
messageTypes.put(StompCommand.CONNECT, SimpMessageType.CONNECT);
53+
messageTypes.put(StompCommand.DISCONNECT, SimpMessageType.DISCONNECT);
54+
messageTypes.put(StompCommand.SUBSCRIBE, SimpMessageType.SUBSCRIBE);
55+
messageTypes.put(StompCommand.UNSUBSCRIBE, SimpMessageType.UNSUBSCRIBE);
56+
messageTypes.put(StompCommand.SEND, SimpMessageType.MESSAGE);
57+
messageTypes.put(StompCommand.MESSAGE, SimpMessageType.MESSAGE);
58+
}
59+
60+
61+
@Test
62+
public void getMessageType() throws Exception {
63+
for (StompCommand stompCommand : StompCommand.values()) {
64+
SimpMessageType simp = messageTypes.get(stompCommand);
65+
if (simp == null) {
66+
simp = SimpMessageType.OTHER;
67+
}
68+
assertSame(simp, stompCommand.getMessageType());
69+
}
70+
}
71+
72+
@Test
73+
public void requiresDestination() throws Exception {
74+
for (StompCommand stompCommand : StompCommand.values()) {
75+
assertEquals(destinationRequired.contains(stompCommand), stompCommand.requiresDestination());
76+
}
77+
}
78+
79+
@Test
80+
public void requiresSubscriptionId() throws Exception {
81+
for (StompCommand stompCommand : StompCommand.values()) {
82+
assertEquals(subscriptionIdRequired.contains(stompCommand), stompCommand.requiresSubscriptionId());
83+
}
84+
}
85+
86+
@Test
87+
public void requiresContentLength() throws Exception {
88+
for (StompCommand stompCommand : StompCommand.values()) {
89+
assertEquals(contentLengthRequired.contains(stompCommand), stompCommand.requiresContentLength());
90+
}
91+
}
92+
93+
@Test
94+
public void isBodyAllowed() throws Exception {
95+
for (StompCommand stompCommand : StompCommand.values()) {
96+
assertEquals(bodyAllowed.contains(stompCommand), stompCommand.isBodyAllowed());
97+
}
98+
}
99+
100+
}

0 commit comments

Comments
 (0)