Skip to content

Package: PortRange

PortRange

nameinstructionbranchcomplexitylinemethod
PortRange(int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
PortRange(int, int)
M: 23 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getLower()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getUpper()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toString()
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
valueOf(String)
M: 43 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
3: * Copyright (c) 2018 Payara Services Ltd.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package org.glassfish.grizzly;
19:
20: import java.util.regex.Matcher;
21: import java.util.regex.Pattern;
22:
23: /**
24: * Immutable class representing a port range.
25: *
26: * @author Gerd Behrmann
27: * @author Tigran Mkrtchyan
28: */
29: public class PortRange {
30:
31: /** Pattern matching <PORT>[:<PORT>] */
32: private final static Pattern FORMAT = Pattern.compile("(\\d+)(?:(?:,|:)(\\d+))?");
33: private final int lower;
34: private final int upper;
35:
36: /**
37: * Creates a port range with the given bounds (both inclusive).
38: *
39: * @param low start of port range
40: * @param high end of port range
41: * @throws IllegalArgumentException is either bound is not between 1 and 65535, or if <code>high</code> is lower than
42: * <code>low</code>.
43: */
44: public PortRange(final int low, final int high) {
45:• if (low < 1 || high < low || 65535 < high) {
46: throw new IllegalArgumentException("Invalid range");
47: }
48: lower = low;
49: upper = high;
50: }
51:
52: /**
53: * Creates a port range containing a single port.
54: *
55: * @param port port
56: */
57: public PortRange(final int port) {
58: this(port, port);
59: }
60:
61: /**
62: * Parse a port range. A port range consists of either a single integer, or two integers separated by either a comma or
63: * a colon.
64: *
65: * The bounds must be between 1 and 65535, both inclusive.
66: *
67: * @param s either "number" or "number:number"
68: * @return The port range represented by <code>s</code>.
69: */
70: public static PortRange valueOf(String s) throws IllegalArgumentException {
71: Matcher m = FORMAT.matcher(s);
72:
73:• if (!m.matches()) {
74: throw new IllegalArgumentException("Invalid string format: " + s);
75: }
76:
77: int low;
78: int high;
79:
80: try {
81: low = Integer.parseInt(m.group(1));
82:• high = m.groupCount() == 1 ? low : Integer.parseInt(m.group(2));
83: } catch (NumberFormatException e) {
84: throw new IllegalArgumentException("Invalid string format: " + s);
85: }
86:
87: return new PortRange(low, high);
88: }
89:
90: public int getLower() {
91: return lower;
92: }
93:
94: public int getUpper() {
95: return upper;
96: }
97:
98: @Override
99: public String toString() {
100: return String.format("%d:%d", lower, upper);
101: }
102: }