Skip to content

Commit cbee411

Browse files
authored
Change PortWithProtocol to PortsWithProtocol (GoogleContainerTools#506)
1 parent 926b491 commit cbee411

3 files changed

Lines changed: 89 additions & 56 deletions

File tree

‎jib-core/src/main/java/com/google/cloud/tools/jib/configuration/PortWithProtocol.java‎

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.configuration;
18+
19+
import com.google.common.base.Preconditions;
20+
21+
/** Holds port number and protocol for an exposed port. */
22+
public class PortsWithProtocol {
23+
24+
/** Represents the protocol portion of the port. */
25+
public enum Protocol {
26+
TCP("tcp"),
27+
UDP("udp");
28+
29+
private final String stringRepresentation;
30+
31+
Protocol(String stringRepresentation) {
32+
this.stringRepresentation = stringRepresentation;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return stringRepresentation;
38+
}
39+
}
40+
41+
/**
42+
* Creates a new {@link PortsWithProtocol} with the specified range and protocol.
43+
*
44+
* @param minPort the minimum port number
45+
* @param maxPort the maximum port number
46+
* @param protocol the protocol
47+
* @return the {@link PortsWithProtocol}
48+
*/
49+
public static PortsWithProtocol forRange(int minPort, int maxPort, Protocol protocol) {
50+
Preconditions.checkArgument(
51+
minPort <= maxPort, "minPort must be less than or equal to maxPort in port range");
52+
return new PortsWithProtocol(minPort, maxPort, protocol);
53+
}
54+
55+
/**
56+
* Creates a new {@link PortsWithProtocol} with the port number and protocol.
57+
*
58+
* @param port the port number
59+
* @param protocol the protocol
60+
* @return the {@link PortsWithProtocol}
61+
*/
62+
public static PortsWithProtocol forSingle(int port, Protocol protocol) {
63+
return new PortsWithProtocol(port, port, protocol);
64+
}
65+
66+
private final int minPort;
67+
private final int maxPort;
68+
private final Protocol protocol;
69+
70+
public int getMinPort() {
71+
return minPort;
72+
}
73+
74+
public int getMaxPort() {
75+
return maxPort;
76+
}
77+
78+
public Protocol getProtocol() {
79+
return protocol;
80+
}
81+
82+
private PortsWithProtocol(int minPort, int maxPort, Protocol protocol) {
83+
this.minPort = minPort;
84+
this.maxPort = maxPort;
85+
this.protocol = protocol;
86+
}
87+
}

‎jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ExposedPortsParser.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.cloud.tools.jib.frontend;
1818

1919
import com.google.cloud.tools.jib.builder.BuildLogger;
20-
import com.google.cloud.tools.jib.configuration.PortWithProtocol;
20+
import com.google.cloud.tools.jib.configuration.PortsWithProtocol;
2121
import com.google.common.annotations.VisibleForTesting;
2222
import com.google.common.base.Strings;
2323
import com.google.common.collect.ImmutableList;
@@ -35,7 +35,7 @@ public class ExposedPortsParser {
3535
private static final Pattern portPattern = Pattern.compile("(\\d+)(?:-(\\d+))?(/tcp|/udp)?");
3636

3737
/**
38-
* TODO: Return list of {@link PortWithProtocol}s instead of strings
38+
* TODO: Return list of {@link PortsWithProtocol}s instead of strings
3939
*
4040
* <p>Converts/validates a list of ports with ranges to an expanded form without ranges.
4141
*

0 commit comments

Comments
 (0)