Find the address class and address type from the IP address with Java [No. 2 decoction]

at first

I decided to use a cloud server, and thankfully I got a global IP. In order to separate the processing between the in-house PC (private IP) and the cloud server (global IP), it is now possible to determine the address class and private IP from the IP.

(Original story) Obtaining the address class and address type from the IP address with Java

(reference) IP Address-Wikipedia

Implementation

IPAddressInfo.java


public class IPAddressInfo {

	public enum AddressClass{
		ClassA(128) {
			@Override
			protected boolean isPrivate(int first, int second) {
				return first == 10;
			}
		},
		ClassB(128 + 64) {
			@Override
			protected boolean isPrivate(int first, int second) {
				return first == 172 && (16 <= second && second <= 31);
			}
		},
		ClassC(128 + 64 + 32) {
			@Override
			protected boolean isPrivate(int first, int second) {
				return first == 192 && second == 168;
			}
		},
		ClassD(128 + 64 + 32 + 16) {
			@Override
			protected boolean isPrivate(int first, int second) {
				return false;
			}
		},
		ClassE(256) {
			@Override
			protected boolean isPrivate(int first, int second) {
				return false;
			}
		},;

		private final int outOfNo;

		/**
		 *Five address classes of IP addresses.
		 * @param outOfNo The first IP of the following address class
		 */
		private AddressClass(int outOfNo){
			this.outOfNo = outOfNo;
		}

		protected abstract boolean isPrivate(int first, int second);
	}

	public AddressClass getAddressClass(int first){
		return  Arrays.stream(AddressClass.values()).filter(p -> first < p.outOfNo).findFirst().orElseThrow(() -> new IllegalArgumentException("Not IP:" + first));
	}

	public AddressClass getAddressClass(String ip){
		List<String> addressList = Arrays.asList(ip.split("\\."));
		return getAddressClass(Integer.valueOf(addressList.get(0)));
	}

	public boolean isPrivate(String ip){
		List<String> addressList = Arrays.asList(ip.split("\\."));
		int first = Integer.valueOf(addressList.get(0));
		int second = Integer.valueOf(addressList.get(1));
		return getAddressClass(first).isPrivate(first, second);
	}
}

test

IPAddressInfo.java


	@Test
	public void getAddressClass(){
		assertThat(getAddressClass("0.0.0.0"), is(AddressClass.ClassA));
		assertThat(getAddressClass("10.0.0.0"), is(AddressClass.ClassA));
		assertThat(getAddressClass("10.255.255.255"), is(AddressClass.ClassA));
		assertThat(getAddressClass("127.255.255.255"), is(AddressClass.ClassA));

		assertThat(getAddressClass("128.0.0.0"), is(AddressClass.ClassB));
		assertThat(getAddressClass("172.16.0.0"), is(AddressClass.ClassB));
		assertThat(getAddressClass("172.31.255.255"), is(AddressClass.ClassB));
		assertThat(getAddressClass("191.255.255.255"), is(AddressClass.ClassB));

		assertThat(getAddressClass("192.0.0.0"), is(AddressClass.ClassC));
		assertThat(getAddressClass("192.168.0.0"), is(AddressClass.ClassC));
		assertThat(getAddressClass("192.168.255.255"), is(AddressClass.ClassC));
		assertThat(getAddressClass("223.255.255.255"), is(AddressClass.ClassC));

		assertThat(getAddressClass("224.0.0.0"), is(AddressClass.ClassD));
		assertThat(getAddressClass("239.255.255.255"), is(AddressClass.ClassD));

		assertThat(getAddressClass("240.0.0.0"), is(AddressClass.ClassE));
		assertThat(getAddressClass("255.255.255.255"), is(AddressClass.ClassE));
	}

	@Test
	public void isPrivate(){
		assertThat(isPrivate("0.0.0.0"), is(false));
		assertThat(isPrivate("10.0.0.0"), is(true));
		assertThat(isPrivate("10.255.255.255"), is(true));
		assertThat(isPrivate("127.255.255.255"), is(false));

		assertThat(isPrivate("128.0.0.0"), is(false));
		assertThat(isPrivate("172.16.0.0"), is(true));
		assertThat(isPrivate("172.31.255.255"), is(true));
		assertThat(isPrivate("191.255.255.255"), is(false));

		assertThat(isPrivate("192.0.0.0"), is(false));
		assertThat(isPrivate("192.168.0.0"), is(true));
		assertThat(isPrivate("192.168.255.255"), is(true));
		assertThat(isPrivate("223.255.255.255"), is(false));

		assertThat(isPrivate("224.0.0.0"), is(false));
		assertThat(isPrivate("239.255.255.255"), is(false));

		assertThat(isPrivate("240.0.0.0"), is(false));
		assertThat(isPrivate("255.255.255.255"), is(false));
	}

	@Test(expected=IllegalArgumentException.class)
	public void error(){
		getAddressClass("256.255.255.255");
	}

Recommended Posts

Find the address class and address type from the IP address with Java [No. 2 decoction]
Find the address class and address type from the IP address with Java
Find the greatest common divisor and least common multiple with JAVA
Get country from IP address (Java)
[Java] How to convert from String to Path type and get the path
[Java] Get the date with the LocalDateTime class
<java> Split the address before and after the street address with a regular expression
[Java] Set the time from the browser with jsoup
Increment with the third argument of iterate method of Stream class added from Java9
"TCP / IP JAVA network programming understood from the basics" Compiled with Eclipse. Debug with Wireshark.
Feel the basic type and reference type easily with ruby
Write ABNF in Java and pass the email address
Java language from the perspective of Kotlin and C #
[Java] Get the date 10 days later with the Calendar class
Feel the basic type and reference type easily with ruby 2
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Java class type field
I want to return a type different from the input element with Java8 StreamAPI reduce ()
JSON in Java and Jackson Part 1 Return JSON from the server
Difference between Java and JavaScript (how to find the average)
What is the LocalDateTime class? [Java beginner] -Date and time class-
Implement Java Interface in JRuby class and call it from Java
Set the date and time from the character string with POI
Find out all timezone IDs supported by the Java TimeZone class
Correct the character code in Java and read from the URL
[Java] Check the difference between orElse and orElseGet with IntStream
Java class definition and instantiation
Search and execute method by name from instance with processing (java)
Install the memcached plugin on MySQL and access it from Java
How to set the IP address and host name of CentOS8
I want to return to the previous screen with kotlin and java!
[Android, Java] Method to find the elapsed date from two dates
Memorandum No.2 "Making a search history with ArrayList and HashSet" [Java]
Read the first 4 bytes of the Java class file and output CAFEBABE
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
Read barometric pressure and temperature with Java from Raspberry Pi 3 & BMP180
Create a calendar from the Calendar class by specifying the year and month
[Gradle] Build a Java project with a configuration different from the convention
Consideration and coping with the fact that SSL communication errors are more likely to occur from Java 11