11package 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+
36import java .io .IOException ;
47import java .util .Map .Entry ;
58
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 )
3235public 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 > {
0 commit comments