|
| 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 | +} |
0 commit comments