@@ -107,69 +107,85 @@ public Map<ExposedPort, Binding[]> getBindings() {
107107 // return bindings.toArray(new PortBinding[bindings.size()]);
108108 // }
109109
110- /**
111- * Creates a {@link Binding} for the given IP address and port number.
112- */
113- public static Binding binding (String hostIp , String hostPort ) {
114- return new Binding (hostIp , hostPort );
115- }
116-
117- /**
118- * Creates a {@link Binding} for the given port number, leaving the IP address undefined.
119- */
120- public static Binding binding (String hostPort ) {
121- return new Binding (null , hostPort );
122- }
123-
124110 /**
125111 * A {@link Binding} represents a socket on the Docker host that is used in a {@link PortBinding}. It is characterized by an
126- * {@link #getHostIp() IP address} and a {@link #getHostPort () port number }. Both properties may be <code>null</code> in order to let
127- * Docker assign them dynamically/using defaults.
112+ * {@link #getHostIp() IP address} and a {@link #getHostPortSpec () port spec }. Both properties may be <code>null</code> in order to
113+ * let Docker assign them dynamically/using defaults.
128114 *
129115 * @see Ports#bind(ExposedPort, Binding)
130116 * @see ExposedPort
131117 */
132118 public static class Binding {
133119
134120 /**
135- * Creates a {@link Binding} for the given {@link #getHostPort () port number or range }, leaving the {@link #getHostIp() IP address}
121+ * Creates a {@link Binding} for the given {@link #getHostPortSpec () port spec }, leaving the {@link #getHostIp() IP address}
136122 * undefined.
137123 *
138124 * @see Ports#bind(ExposedPort, Binding)
139125 * @see ExposedPort
140126 */
141- public static Binding forPort (String port ) {
142- return new Binding (null , port );
127+ public static Binding bindPortSpec (String portSpec ) {
128+ return new Binding (null , portSpec );
143129 }
144130
145131 /**
146- * Creates a {@link Binding} for the given {@link #getHostIp() IP address}, leaving the {@link #getHostPort () port number }
132+ * Creates a {@link Binding} for the given {@link #getHostIp() IP address}, leaving the {@link #getHostPortSpec () port spec }
147133 * undefined.
148134 */
149- public static Binding forIp (String hostIp ) {
135+ public static Binding bindIp (String hostIp ) {
150136 return new Binding (hostIp , null );
151137 }
152138
139+ /**
140+ * Creates a {@link Binding} for the given {@link #getHostIp() IP address} and port number.
141+ */
142+ public static Binding bindIpAndPort (String hostIp , int port ) {
143+ return new Binding (hostIp , "" + port );
144+ }
145+
146+ /**
147+ * Creates a {@link Binding} for the given {@link #getHostIp() IP address} and port range.
148+ */
149+ public static Binding bindIpAndPortRange (String hostIp , int lowPort , int highPort ) {
150+ return new Binding (hostIp , lowPort + "-" + highPort );
151+ }
152+
153+ /**
154+ * Creates a {@link Binding} for the given port range, leaving the {@link #getHostIp() IP address}
155+ * undefined.
156+ */
157+ public static Binding bindPortRange (int lowPort , int highPort ) {
158+ return bindIpAndPortRange (null , lowPort , highPort );
159+ }
160+
161+ /**
162+ * Creates a {@link Binding} for the given port leaving the {@link #getHostIp() IP address}
163+ * undefined.
164+ */
165+ public static Binding bindPort (int port ) {
166+ return bindIpAndPort (null , port );
167+ }
168+
169+ /**
170+ * Creates an empty {@link Binding}.
171+ */
172+ public static Binding empty () {
173+ return new Binding (null , null );
174+ }
175+
153176 private final String hostIp ;
154177
155- private final String hostPort ;
178+ private final String hostPortSpec ;
156179
157180 /**
158- * Creates a {@link Binding} for the given {@link #getHostIp() IP address} and {@link #getHostPort () port number }.
181+ * Creates a {@link Binding} for the given {@link #getHostIp() host IP address} and {@link #getHostPortSpec () host port spec }.
159182 *
160183 * @see Ports#bind(ExposedPort, Binding)
161184 * @see ExposedPort
162185 */
163- public Binding (String hostIp , String hostPort ) {
186+ public Binding (String hostIp , String hostPortSpec ) {
164187 this .hostIp = isEmpty (hostIp ) ? null : hostIp ;
165- this .hostPort = hostPort ;
166- }
167-
168- /**
169- * Creates a {@link Binding} with both {@link #getHostIp() IP address} and {@link #getHostPort() port number} undefined.
170- */
171- public Binding () {
172- this (null , null );
188+ this .hostPortSpec = hostPortSpec ;
173189 }
174190
175191 /**
@@ -181,16 +197,17 @@ public String getHostIp() {
181197 }
182198
183199 /**
184- * @return the port number on the Docker host. May be <code>null</code>, in which case Docker will dynamically assign a port.
200+ * @return the port spec for the binding on the Docker host. May reference a single port ("1234"), a port range ("1234-2345") or
201+ * <code>null</code>, in which case Docker will dynamically assign a port.
185202 */
186- public String getHostPort () {
187- return hostPort ;
203+ public String getHostPortSpec () {
204+ return hostPortSpec ;
188205 }
189206
190207 /**
191208 * Parses a textual host and port specification (as used by the Docker CLI) to a {@link Binding}.
192209 * <p>
193- * Legal syntax: <code>IP|IP:port|port </code>
210+ * Legal syntax: <code>IP|IP:portSpec|portSpec </code> where <code>portSpec</code> is either a single port or a port range
194211 *
195212 * @param serialized
196213 * serialized the specification, e.g. <code>127.0.0.1:80</code>
@@ -201,7 +218,7 @@ public String getHostPort() {
201218 public static Binding parse (String serialized ) throws IllegalArgumentException {
202219 try {
203220 if (serialized .isEmpty ()) {
204- return new Binding ();
221+ return Binding . empty ();
205222 }
206223
207224 String [] parts = serialized .split (":" );
@@ -210,7 +227,7 @@ public static Binding parse(String serialized) throws IllegalArgumentException {
210227 return new Binding (parts [0 ], parts [1 ]);
211228 }
212229 case 1 : {
213- return parts [0 ].contains ("." ) ? Binding .forIp (parts [0 ]) : Binding .forPort (parts [0 ]);
230+ return parts [0 ].contains ("." ) ? Binding .bindIp (parts [0 ]) : Binding .bindPortSpec (parts [0 ]);
214231 }
215232 default : {
216233 throw new IllegalArgumentException ();
@@ -230,19 +247,19 @@ public static Binding parse(String serialized) throws IllegalArgumentException {
230247 @ Override
231248 public String toString () {
232249 if (isEmpty (hostIp )) {
233- return hostPort ;
234- } else if (hostPort == null ) {
250+ return hostPortSpec ;
251+ } else if (hostPortSpec == null ) {
235252 return hostIp ;
236253 } else {
237- return hostIp + ":" + hostPort ;
254+ return hostIp + ":" + hostPortSpec ;
238255 }
239256 }
240257
241258 @ Override
242259 public boolean equals (Object obj ) {
243260 if (obj instanceof Binding ) {
244261 Binding other = (Binding ) obj ;
245- return new EqualsBuilder ().append (hostIp , other .getHostIp ()).append (hostPort , other .getHostPort ())
262+ return new EqualsBuilder ().append (hostIp , other .getHostIp ()).append (hostPortSpec , other .getHostPortSpec ())
246263 .isEquals ();
247264 } else {
248265 return super .equals (obj );
@@ -293,7 +310,7 @@ public void serialize(Ports portBindings, JsonGenerator jsonGen, SerializerProvi
293310 for (Binding binding : entry .getValue ()) {
294311 jsonGen .writeStartObject ();
295312 jsonGen .writeStringField ("HostIp" , binding .getHostIp () == null ? "" : binding .getHostIp ());
296- jsonGen .writeStringField ("HostPort" , binding .getHostPort () == null ? "" : binding .getHostPort ());
313+ jsonGen .writeStringField ("HostPort" , binding .getHostPortSpec () == null ? "" : binding .getHostPortSpec ());
297314 jsonGen .writeEndObject ();
298315 }
299316 jsonGen .writeEndArray ();
0 commit comments