Skip to content

Commit adf0d22

Browse files
Added Country class to query country information and Region class to lookup the ISO 3166-2 subdivision code for country code and region name
1 parent 76aae2a commit adf0d22

File tree

7 files changed

+252
-6
lines changed

7 files changed

+252
-6
lines changed

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,87 @@ public class Main
407407
}
408408
}
409409
}
410-
```
410+
```
411+
412+
## COUNTRY CLASS
413+
414+
## Methods
415+
Below are the methods supported in this class.
416+
417+
|Method Name|Description|
418+
|---|---|
419+
|Constructor(CSVFile)|Expect a IP2Location Country Information CSV file. This database is free for download at https://www.ip2location.com/free/country-information|
420+
|GetCountryInfo(CountryCode)|Returns the country information.|
421+
422+
## Usage
423+
424+
```java
425+
import com.ip2location.*;
426+
import java.util.*;
427+
428+
public class Main
429+
{
430+
public Main()
431+
{
432+
}
433+
public static void main(String[] args)
434+
{
435+
try
436+
{
437+
Country cc = new Country("/usr/data/IP2LOCATION-COUNTRY-INFORMATION.CSV");
438+
439+
Map<String, String> ccresult = cc.GetCountryInfo("US");
440+
441+
System.out.println(ccresult.toString());
442+
443+
List <Map<String, String>> ccresults = cc.GetCountryInfo();
444+
445+
System.out.println(ccresults.toString());
446+
}
447+
catch(Exception e)
448+
{
449+
System.out.println(e);
450+
e.printStackTrace(System.out);
451+
}
452+
}
453+
}
454+
```
455+
456+
## REGION CLASS
457+
458+
## Methods
459+
Below are the methods supported in this class.
460+
461+
|Method Name|Description|
462+
|---|---|
463+
|Constructor(CSVFile)|Expect a IP2Location ISO 3166-2 Subdivision Code CSV file. This database is free for download at https://www.ip2location.com/free/iso3166-2|
464+
|GetRegionCode(CountryCode, RegionName)|Returns the region code for the supplied country code and region name.|
465+
466+
## Usage
467+
468+
```java
469+
import com.ip2location.*;
470+
471+
public class Main
472+
{
473+
public Main()
474+
{
475+
}
476+
public static void main(String[] args)
477+
{
478+
try
479+
{
480+
Region reg = new Region("/usr/data/IP2LOCATION-ISO3166-2.CSV");
481+
482+
String regionCode = reg.GetRegionCode("US", "Nevada");
483+
484+
System.out.println(regionCode);
485+
}
486+
catch(Exception e)
487+
{
488+
System.out.println(e);
489+
e.printStackTrace(System.out);
490+
}
491+
}
492+
}
493+
```

com/ip2location/Country.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.ip2location;
2+
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.io.File;
10+
import com.opencsv.*;
11+
import com.opencsv.exceptions.*;
12+
13+
/**
14+
* This class parses country information CSV and returns the country information.
15+
* <p>
16+
* <b>Requirements:</b> Java SDK 1.4 or later<br>
17+
* <p>
18+
* Copyright (c) 2002-2022 IP2Location.com
19+
* <p>
20+
*
21+
* @author IP2Location.com
22+
* @version 8.10.0
23+
*/
24+
public class Country {
25+
private final Map<String, Map<String, String>> records = new HashMap<>();
26+
27+
/**
28+
* This constructor reads the country information CSV and store the parsed info.
29+
*
30+
* @param CSVFile The full path to the country information CSV file.
31+
*/
32+
public Country(String CSVFile) throws IOException, CsvValidationException {
33+
Map<String, String> line;
34+
File file = new File(CSVFile);
35+
if (!file.exists()) {
36+
throw new IOException("The CSV file '" + CSVFile + "' is not found.");
37+
}
38+
FileReader fr = new FileReader(file);
39+
if (fr.read() == -1) {
40+
throw new IOException("Unable to read '" + CSVFile + "'.");
41+
}
42+
CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file));
43+
while ((line = reader.readMap()) != null) {
44+
if (line.containsKey("country_code")) {
45+
records.put(line.get("country_code"), line);
46+
} else {
47+
throw new IOException("Invalid country information CSV file.");
48+
}
49+
}
50+
}
51+
52+
/**
53+
* This function gets the country information for the supplied country code.
54+
*
55+
* @param CountryCode ISO-3166 country code
56+
* @return Map
57+
*/
58+
public Map<String, String> GetCountryInfo(String CountryCode) throws IOException {
59+
if (records.isEmpty()) {
60+
throw new IOException("No record available.");
61+
} else {
62+
return records.getOrDefault(CountryCode, null);
63+
}
64+
}
65+
66+
/**
67+
* This function gets the country information for all countries.
68+
*
69+
* @return List
70+
*/
71+
public List<Map<String, String>> GetCountryInfo() throws IOException {
72+
List<Map<String, String>> results = new ArrayList<>();
73+
if (records.isEmpty()) {
74+
throw new IOException("No record available.");
75+
} else {
76+
for (Map.Entry<String, Map<String, String>> entry : records.entrySet()) {
77+
results.add(entry.getValue());
78+
}
79+
}
80+
return results;
81+
}
82+
83+
}

com/ip2location/IP2Location.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* <p>
3737
*
3838
* @author IP2Location.com
39-
* @version 8.9.1
39+
* @version 8.10.0
4040
*/
4141
public class IP2Location {
4242
private static final Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); // IPv4

com/ip2location/IP2LocationWebService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* <p>
2929
*
3030
* @author IP2Location.com
31-
* @version 8.9.1
31+
* @version 8.10.0
3232
*/
3333
public class IP2LocationWebService {
3434
private static final Pattern pattern = Pattern.compile("^[\\dA-Z]{10}$");

com/ip2location/IPResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* <p>
1010
*
1111
* @author IP2Location.com
12-
* @version 8.9.1
12+
* @version 8.10.0
1313
*/
1414
public class IPResult {
1515
static final String NOT_SUPPORTED = "Not_Supported";
@@ -38,7 +38,7 @@ public class IPResult {
3838
String category;
3939
String status;
4040
boolean delay = false;
41-
String version = "Version 8.9.1";
41+
String version = "Version 8.10.0";
4242

4343
IPResult(String ipstring) {
4444
ip_address = ipstring;

com/ip2location/IPTools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* <p>
3535
*
3636
* @author IP2Location.com
37-
* @version 8.9.1
37+
* @version 8.10.0
3838
*/
3939
public class IPTools {
4040
private static final BigInteger MAX_IPV4_RANGE = new BigInteger("4294967295");

com/ip2location/Region.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.ip2location;
2+
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
import java.util.*;
6+
import java.io.File;
7+
import com.opencsv.*;
8+
import com.opencsv.exceptions.*;
9+
10+
/**
11+
* This class parses region information CSV and returns the region code.
12+
* <p>
13+
* <b>Requirements:</b> Java SDK 1.4 or later<br>
14+
* <p>
15+
* Copyright (c) 2002-2022 IP2Location.com
16+
* <p>
17+
*
18+
* @author IP2Location.com
19+
* @version 8.10.0
20+
*/
21+
public class Region {
22+
private final Map<String, List<Map<String, String>>> records = new HashMap<>();
23+
24+
/**
25+
* This constructor reads the region information CSV and store the parsed info.
26+
*
27+
* @param CSVFile The full path to the region information CSV file.
28+
*/
29+
public Region(String CSVFile) throws IOException, CsvValidationException {
30+
Map<String, String> line;
31+
File file = new File(CSVFile);
32+
if (!file.exists()) {
33+
throw new IOException("The CSV file '" + CSVFile + "' is not found.");
34+
}
35+
FileReader fr = new FileReader(file);
36+
if (fr.read() == -1) {
37+
throw new IOException("Unable to read '" + CSVFile + "'.");
38+
}
39+
CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file));
40+
while ((line = reader.readMap()) != null) {
41+
if (line.containsKey("subdivision_name")) {
42+
String cc = line.get("country_code");
43+
if (!records.containsKey(cc)) {
44+
records.put(cc, new ArrayList<>());
45+
}
46+
Map<String, String> item = new HashMap<>();
47+
item.put(line.get("subdivision_name").toUpperCase(), line.get("code"));
48+
records.get(cc).add(item);
49+
} else {
50+
throw new IOException("Invalid region information CSV file.");
51+
}
52+
}
53+
}
54+
55+
/**
56+
* This function gets the region code for the supplied country code and region name.
57+
*
58+
* @param CountryCode ISO-3166 country code
59+
* @param RegionName Region name
60+
* @return String
61+
*/
62+
public String GetRegionCode(String CountryCode, String RegionName) throws IOException {
63+
if (records.isEmpty()) {
64+
throw new IOException("No record available.");
65+
} else {
66+
if (!records.containsKey(CountryCode)) {
67+
return null;
68+
}
69+
List<Map<String, String>> items = records.get(CountryCode);
70+
71+
for (Map<String, String> item : items) {
72+
if (item.containsKey(RegionName.toUpperCase())) {
73+
return item.get(RegionName.toUpperCase());
74+
}
75+
}
76+
}
77+
return null;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)