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- * Copyright (c) 2002-2023 IP2Location.com
17- * <p>
18- *
19- * @author IP2Location.com
20- * @version 8.11.2
21- */
22- public class Country {
23- private final Map <String , Map <String , String >> records = new HashMap <>();
24-
25- /**
26- * This constructor reads the country information CSV and store the parsed info.
27- *
28- * @param CSVFile The full path to the country information CSV file.
29- */
30- public Country (String CSVFile ) throws IOException , CsvValidationException {
31- Map <String , String > line ;
32- File file = new File (CSVFile );
33- if (!file .exists ()) {
34- throw new IOException ("The CSV file '" + CSVFile + "' is not found." );
35- }
36- FileReader fr = new FileReader (file );
37- if (fr .read () == -1 ) {
38- throw new IOException ("Unable to read '" + CSVFile + "'." );
39- }
40- CSVReaderHeaderAware reader = new CSVReaderHeaderAware (new FileReader (file ));
41- while ((line = reader .readMap ()) != null ) {
42- if (line .containsKey ("country_code" )) {
43- records .put (line .get ("country_code" ), line );
44- } else {
45- throw new IOException ("Invalid country information CSV file." );
46- }
47- }
48- }
49-
50- /**
51- * This function gets the country information for the supplied country code.
52- *
53- * @param CountryCode ISO-3166 country code
54- * @return Map
55- */
56- public Map <String , String > GetCountryInfo (String CountryCode ) throws IOException {
57- if (records .isEmpty ()) {
58- throw new IOException ("No record available." );
59- } else {
60- return records .getOrDefault (CountryCode , null );
61- }
62- }
63-
64- /**
65- * This function gets the country information for all countries.
66- *
67- * @return List
68- */
69- public List <Map <String , String >> GetCountryInfo () throws IOException {
70- List <Map <String , String >> results = new ArrayList <>();
71- if (records .isEmpty ()) {
72- throw new IOException ("No record available." );
73- } else {
74- for (Map .Entry <String , Map <String , String >> entry : records .entrySet ()) {
75- results .add (entry .getValue ());
76- }
77- }
78- return results ;
79- }
80-
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+ * Copyright (c) 2002-2024 IP2Location.com
17+ * <p>
18+ *
19+ * @author IP2Location.com
20+ * @version 8.12.0
21+ */
22+ public class Country {
23+ private final Map <String , Map <String , String >> records = new HashMap <>();
24+
25+ /**
26+ * This constructor reads the country information CSV and store the parsed info.
27+ *
28+ * @param CSVFile The full path to the country information CSV file.
29+ */
30+ public Country (String CSVFile ) throws IOException , CsvValidationException {
31+ Map <String , String > line ;
32+ File file = new File (CSVFile );
33+ if (!file .exists ()) {
34+ throw new IOException ("The CSV file '" + CSVFile + "' is not found." );
35+ }
36+ FileReader fr = new FileReader (file );
37+ if (fr .read () == -1 ) {
38+ throw new IOException ("Unable to read '" + CSVFile + "'." );
39+ }
40+ CSVReaderHeaderAware reader = new CSVReaderHeaderAware (new FileReader (file ));
41+ while ((line = reader .readMap ()) != null ) {
42+ if (line .containsKey ("country_code" )) {
43+ records .put (line .get ("country_code" ), line );
44+ } else {
45+ throw new IOException ("Invalid country information CSV file." );
46+ }
47+ }
48+ }
49+
50+ /**
51+ * This function gets the country information for the supplied country code.
52+ *
53+ * @param CountryCode ISO-3166 country code
54+ * @return Map
55+ */
56+ public Map <String , String > GetCountryInfo (String CountryCode ) throws IOException {
57+ if (records .isEmpty ()) {
58+ throw new IOException ("No record available." );
59+ } else {
60+ return records .getOrDefault (CountryCode , null );
61+ }
62+ }
63+
64+ /**
65+ * This function gets the country information for all countries.
66+ *
67+ * @return List
68+ */
69+ public List <Map <String , String >> GetCountryInfo () throws IOException {
70+ List <Map <String , String >> results = new ArrayList <>();
71+ if (records .isEmpty ()) {
72+ throw new IOException ("No record available." );
73+ } else {
74+ for (Map .Entry <String , Map <String , String >> entry : records .entrySet ()) {
75+ results .add (entry .getValue ());
76+ }
77+ }
78+ return results ;
79+ }
80+
8181}
0 commit comments