Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/io/github/adrianulbona/cloc/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.github.adrianulbona.cloc;

import java.util.Objects;

public final class Country implements Comparable<Country> {
public final String name;
public final String iso2;
public final String iso3;
public final String isoId;

public Country(final String name, final String iso2, final String iso3, final String isoId) {
this.name = name;
this.iso2 = iso2;
this.iso3 = iso3;
this.isoId = isoId;
}

public String getName() {
return name;
}

public String getIso2() {
return iso2;
}

public String getIso3() {
return iso3;
}

public String getIsoId() {
return isoId;
}

@Override
public int compareTo(Country that) {
return this.name.compareTo(that.name);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Country country = (Country) o;
return Objects.equals(name, country.name) &&
Objects.equals(iso2, country.iso2) &&
Objects.equals(iso3, country.iso3) &&
Objects.equals(isoId, country.isoId);
}

@Override
public int hashCode() {
return Objects.hash(name, iso2, iso3, isoId);
}

@Override
public String toString() {
return "{" +
"\"name\":\"" + name + "\"," +
"\"iso2\":\"" + iso2 + "\"," +
"\"iso3\":\"" + iso3 + "\"," +
"\"isoId\":" + isoId +
'}';
}
}
27 changes: 27 additions & 0 deletions src/main/java/io/github/adrianulbona/cloc/CountryLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class CountryLocator implements Serializable {
private final Map<Integer, List<String>> countriesIndex;

public List<String> locate(String geoHash) {
return locateCountryName(geoHash);
}

public List<String> locateCountryName(String geoHash) {
if (geoHash == null || geoHash.isEmpty()) {
throw new IllegalArgumentException();
}
Expand All @@ -31,9 +35,32 @@ public List<String> locate(String geoHash) {
.map(this.countriesIndex::get)
.flatMap(List::stream)
.distinct()
.map(CountryLocator::extractName)
.collect(toList());
}

public List<Country> locateCountry(String geoHash) {
if (geoHash == null || geoHash.isEmpty()) {
throw new IllegalArgumentException();
}
final List<Integer> packages = new Collector().collect(this.geoHashTree, Symbol.decodeMultiple(geoHash));
return packages.stream()
.map(this.countriesIndex::get)
.flatMap(List::stream)
.distinct()
.map(CountryLocator::extractCountry)
.collect(toList());
}

private static String extractName(final String str) {
return str.split(";")[0];
}

private static Country extractCountry(final String str) {
final String[] data = str.split(";");
return new Country(data[0], data[1], data[2], data[3]);
}

public static CountryLocator create() {
final Node<Integer> load = new GeoHashTreeLoader().load();
final Map<Integer, List<String>> countriesIndex = new CountriesIndexLoader().load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static java.nio.file.Paths.get;
import static java.util.stream.Collectors.toMap;
Expand All @@ -26,12 +27,19 @@ public Map<Integer, List<String>> load() {
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
return reader.lines()
.map(this::parseIdCountries)
.collect(toMap(idCountries -> idCountries.id, idCountries -> idCountries.countries));
.collect(toMap(
idCountries -> idCountries.id,
idCountries -> idCountries.countries,
(u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
},
LinkedHashMap::new));
} catch (IOException e) {
throw new RuntimeException("Unable to load countries.index", e);
}
}


private IdCountries parseIdCountries(String rawEntry) {
final String[] idCountries = rawEntry.split("=");
final Integer id = Integer.valueOf(idCountries[0]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import $ivy.`com.github.pathikrit::better-files:3.6.0`
import better.files._
import better.files.Dsl._
import better.files.Dsl.SymbolicOperations
val table = file"/Users/odd/Code/cloc/src/main/resources/io/github/adrianulbona/cloc/data/table.txt"
val index = file"/Users/odd/Code/cloc/src/main/resources/io/github/adrianulbona/cloc/data/countries.index"
val out = file"/Users/odd/Code/cloc/src/main/resources/io/github/adrianulbona/cloc/data/countries2.index"
val Line = """^([^\t]+)\t+([^\t]+)\t+([^\t]+)\t+([^\t]+)$""".r
val IndexLine = """^([^=]+)=([^\n]+)$""".r
val byName = table.lines.map { case l @ Line(name, iso2, iso3, id) => (name, (id.toInt, name, iso2, iso3)) }.toMap
out.printLines(index.lines.collect {
case l @ IndexLine(id, names) => (id, names.split(",").toList)
}.map {
case (id, names) => (id, names.map(byName.applyOrElse(_, (name: String) => (-1, name, "--", "---"))))
}.map {
case (id, codes) => s"$id=${codes.map(t => t._2 + ";" + t._3 + ";" + t._4 + ";" + t._1).mkString("", ",", "")}"
})
Loading