Protocol { public static Protocol parse(String value) { Protocol protocol = PREDEFINED_PROTOCOLS.get(value.toLowerCase(Locale.ENGLISH)); if (protocol != null) return protocol; Matcher matcher = SINGLE_PORT_PATTERN.matcher(value); if (matcher.matches()) return new Protocol(TCP, Integer.parseInt(matcher.group())); matcher = PORT_RANGE_PATTERN.matcher(value); if (matcher.matches()) return new Protocol(TCP, Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))); throw new IllegalArgumentException("unknown protocol, protocol=" + value); } Protocol(String ipProtocol, int port);  Protocol(String ipProtocol, int fromPort, int toPort); static Protocol parse(String value); @Override boolean equals(Object object); @Override int hashCode(); @Override String toString(); final String ipProtocol; final int fromPort; final int toPort; }
@Test void parsePredefinedProtocol() { Protocol protocol = Protocol.parse("http"); assertEquals(Protocol.TCP, protocol.ipProtocol); assertEquals(80, protocol.fromPort); assertEquals(80, protocol.toPort); }
@Test void parsePortRange() { Protocol protocol = Protocol.parse("10000-12345"); assertEquals(Protocol.TCP, protocol.ipProtocol); assertEquals(10000, protocol.fromPort); assertEquals(12345, protocol.toPort); }
@Test void parseSinglePort() { Protocol protocol = Protocol.parse("9300"); assertEquals(Protocol.TCP, protocol.ipProtocol); assertEquals(9300, protocol.fromPort); assertEquals(9300, protocol.toPort); }