Skip to content

Commit ef33c69

Browse files
committed
Merge pull request #76 from albers/protocol-enum
New enum "InternetProtocol" for supported IP protocols replaces "scheme"
2 parents eb5db45 + 68c7fc0 commit ef33c69

File tree

3 files changed

+132
-20
lines changed

3 files changed

+132
-20
lines changed

src/main/java/com/github/dockerjava/api/model/ExposedPort.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.dockerjava.api.model;
22

3+
import static com.github.dockerjava.api.model.InternetProtocol.TCP;
4+
import static com.github.dockerjava.api.model.InternetProtocol.UDP;
5+
36
import java.io.IOException;
47
import java.util.Map.Entry;
58

@@ -22,56 +25,75 @@
2225

2326
/**
2427
* Represents a container port that Docker exposes to external clients.
25-
* The port is defined by its {@link #getPort() port number} and a
26-
* {@link #getScheme() scheme}, e.g. <code>tcp</code>.
28+
* The port is defined by its {@link #getPort() port number} and an
29+
* {@link InternetProtocol}.
2730
* It can be published by Docker by {@link Ports#bind(ExposedPort, Binding) binding}
2831
* it to a host port, represented by a {@link Binding}.
2932
*/
3033
@JsonDeserialize(using = ExposedPort.Deserializer.class)
3134
@JsonSerialize(using = ExposedPort.Serializer.class)
3235
public class ExposedPort {
3336

34-
private final String scheme;
35-
37+
private final InternetProtocol protocol;
3638
private final int port;
3739

40+
/**
41+
* Creates an {@link ExposedPort} for the given parameters.
42+
*
43+
* @param port the {@link #getPort() port number}
44+
* @param protocol the {@link InternetProtocol}
45+
*/
46+
public ExposedPort(int port, InternetProtocol protocol) {
47+
this.port = port;
48+
this.protocol = protocol;
49+
}
50+
3851
/**
3952
* Creates an {@link ExposedPort} for the given parameters.
4053
*
4154
* @param scheme the {@link #getScheme() scheme}, <code>tcp</code> or
4255
* <code>udp</code>
4356
* @param port the {@link #getPort() port number}
57+
* @deprecated use {@link #ExposedPort(int, InternetProtocol)}
4458
*/
59+
@Deprecated
4560
public ExposedPort(String scheme, int port) {
46-
this.scheme = scheme;
47-
this.port = port;
61+
this(port, InternetProtocol.valueOf(scheme));
4862
}
4963

64+
/** @return the {@link InternetProtocol} */
65+
public InternetProtocol getProtocol() {
66+
return protocol;
67+
}
68+
5069
/**
51-
* @return the scheme (IP protocol), <code>tcp</code> or <code>udp</code>
70+
* @return the scheme (internet protocol), <code>tcp</code> or <code>udp</code>
71+
* @deprecated use {@link #getProtocol()}
5272
*/
73+
@Deprecated
5374
public String getScheme() {
54-
return scheme;
75+
return protocol.toString();
5576
}
5677

78+
/** @return the port number */
5779
public int getPort() {
5880
return port;
5981
}
6082

6183
/**
62-
* Creates an {@link ExposedPort} for the TCP scheme.
63-
* This is a shortcut for <code>new ExposedPort("tcp", port)</code>
84+
* Creates an {@link ExposedPort} for {@link InternetProtocol#TCP}.
85+
* This is a shortcut for <code>new ExposedPort(port, {@link InternetProtocol#TCP})</code>
6486
*/
6587
public static ExposedPort tcp(int port) {
66-
return new ExposedPort("tcp", port);
88+
return new ExposedPort(port, TCP);
6789
}
6890

6991
/**
70-
* Creates an {@link ExposedPort} for the UDP scheme.
71-
* This is a shortcut for <code>new ExposedPort("udp", port)</code>
92+
* Creates an {@link ExposedPort} for {@link InternetProtocol#UDP}.
93+
* This is a shortcut for <code>new ExposedPort(port, {@link InternetProtocol#UDP})</code>
7294
*/
7395
public static ExposedPort udp(int port) {
74-
return new ExposedPort("udp", port);
96+
return new ExposedPort(port, UDP);
7597
}
7698

7799
/**
@@ -85,8 +107,7 @@ public static ExposedPort udp(int port) {
85107
public static ExposedPort parse(String serialized) throws IllegalArgumentException {
86108
try {
87109
String[] parts = serialized.split("/");
88-
ExposedPort out = new ExposedPort(parts[1], Integer.valueOf(parts[0]));
89-
return out;
110+
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
90111
} catch (Exception e) {
91112
throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'");
92113
}
@@ -95,28 +116,28 @@ public static ExposedPort parse(String serialized) throws IllegalArgumentExcepti
95116
/**
96117
* Returns a string representation of this {@link ExposedPort} suitable
97118
* for inclusion in a JSON message.
98-
* The format is <code>port/scheme</code>, like the argument in {@link #parse(String)}.
119+
* The format is <code>port/protocol</code>, like the argument in {@link #parse(String)}.
99120
*
100121
* @return a string representation of this {@link ExposedPort}
101122
*/
102123
@Override
103124
public String toString() {
104-
return port + "/" + scheme;
125+
return port + "/" + protocol.toString();
105126
}
106127

107128
@Override
108129
public boolean equals(Object obj) {
109130
if (obj instanceof ExposedPort) {
110131
ExposedPort other = (ExposedPort) obj;
111-
return new EqualsBuilder().append(scheme, other.getScheme())
132+
return new EqualsBuilder().append(protocol, other.getProtocol())
112133
.append(port, other.getPort()).isEquals();
113134
} else
114135
return super.equals(obj);
115136
}
116137

117138
@Override
118139
public int hashCode() {
119-
return new HashCodeBuilder().append(scheme).append(port).toHashCode();
140+
return new HashCodeBuilder().append(protocol).append(port).toHashCode();
120141
}
121142

122143
public static class Deserializer extends JsonDeserializer<ExposedPort> {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.dockerjava.api.model;
2+
3+
4+
/**
5+
* The IP protocols supported by Docker.
6+
*
7+
* @see #TCP
8+
* @see #UDP
9+
*/
10+
public enum InternetProtocol {
11+
/** The <i>Transmission Control Protocol</i> */
12+
TCP,
13+
14+
/** The <i>User Datagram Protocol</i> */
15+
UDP;
16+
17+
/**
18+
* The default {@link InternetProtocol}: {@link #TCP}
19+
*/
20+
public static final InternetProtocol DEFAULT = TCP;
21+
22+
/**
23+
* Returns a string representation of this {@link InternetProtocol} suitable
24+
* for inclusion in a JSON message.
25+
* The output is the lowercased name of the Protocol, e.g. <code>tcp</code>.
26+
*
27+
* @return a string representation of this {@link InternetProtocol}
28+
*/
29+
@Override
30+
public String toString() {
31+
return super.toString().toLowerCase();
32+
}
33+
34+
/**
35+
* Parses a string to an {@link InternetProtocol}.
36+
*
37+
* @param serialized the protocol, e.g. <code>tcp</code> or <code>TCP</code>
38+
* @return an {@link InternetProtocol} described by the string
39+
* @throws IllegalArgumentException if the argument cannot be parsed
40+
*/
41+
public static InternetProtocol parse(String serialized) throws IllegalArgumentException {
42+
try {
43+
return valueOf(serialized.toUpperCase());
44+
} catch (Exception e) {
45+
throw new IllegalArgumentException("Error parsing Protocol '" + serialized + "'");
46+
}
47+
}
48+
49+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import static com.github.dockerjava.api.model.InternetProtocol.*;
4+
import static org.testng.Assert.assertEquals;
5+
6+
import org.testng.annotations.Test;
7+
8+
public class InternetProtocolTest {
9+
10+
@Test
11+
public void defaultProtocol() {
12+
assertEquals(InternetProtocol.DEFAULT, TCP);
13+
}
14+
15+
@Test
16+
public void stringify() {
17+
assertEquals(TCP.toString(), "tcp");
18+
}
19+
20+
@Test
21+
public void parseUpperCase() {
22+
assertEquals(InternetProtocol.parse("TCP"), TCP);
23+
}
24+
25+
@Test
26+
public void parseLowerCase() {
27+
assertEquals(InternetProtocol.parse("tcp"), TCP);
28+
}
29+
30+
@Test(expectedExceptions = IllegalArgumentException.class,
31+
expectedExceptionsMessageRegExp = "Error parsing Protocol.*")
32+
public void parseInvalidInput() {
33+
InternetProtocol.parse("xx");
34+
}
35+
36+
@Test(expectedExceptions = IllegalArgumentException.class,
37+
expectedExceptionsMessageRegExp = "Error parsing Protocol 'null'")
38+
public void parseNull() {
39+
InternetProtocol.parse(null);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)