Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java code: Improve code quality & test coverage #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion wilayah-indonesia/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/resources/*
/target
maps.iml
maps.iml
*.iml
6 changes: 4 additions & 2 deletions wilayah-indonesia/indonesia.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.16" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.20" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.12" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.slf4j:slf4j-log4j12:1.7.12" level="project" />
</component>
</module>
15 changes: 13 additions & 2 deletions wilayah-indonesia/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>

Expand All @@ -38,6 +38,17 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
<scope>runtime</scope>
</dependency>

</dependencies>
</project>
41 changes: 24 additions & 17 deletions wilayah-indonesia/src/main/java/wilayah/indonesia/Loader.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package wilayah.indonesia;

import java.io.*;
import org.apache.log4j.Logger;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

/**
* Created by yusuf on 24/10/16.
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Loader {

final static Logger logger = Logger.getLogger(Loader.class);
/**
* Load a file content to string using BufferedInputStream since it fast
* @param file
* @return
* @throws Exception
*
* @param file file path
* @return content string
* @throws IOException throws if failed to load the file
*/
public static String read(String file) throws Exception {
public static String read(String file) throws IOException {
final long start = System.currentTimeMillis();
final InputStream in = new BufferedInputStream(new FileInputStream(file));
int bytesRead = 0;
String line = "";
final byte[] buf = new byte[50130];
while ((bytesRead = in.read(buf)) != -1) {
line += new String(buf, 0, bytesRead);
try (final InputStream in = new BufferedInputStream(new FileInputStream(file))) {
var bytesRead = 0;
var line = new StringBuilder();
final byte[] buf = new byte[50130];
while ((bytesRead = in.read(buf)) != -1) {
line.append(new String(buf, 0, bytesRead));
}
log.debug("Load {} took {} ms", file, (System.currentTimeMillis() - start));
return line.toString();
}
in.close();

logger.debug("Load " + file + " took " + (System.currentTimeMillis() - start) + " ms");
return line;
}
}
175 changes: 89 additions & 86 deletions wilayah-indonesia/src/main/java/wilayah/indonesia/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,138 @@

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import wilayah.indonesia.model.District;
import wilayah.indonesia.model.Province;
import wilayah.indonesia.model.Regency;
import wilayah.indonesia.model.Village;
import org.apache.log4j.Logger;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
* Created by yusuf on 24/10/16.
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Location {

public enum TREE_LEVEL {
PROVINCE, REGENCY, DISTRICT, VILLAGE
}

final static Logger logger = Logger.getLogger(Location.class);

private static final Type PROVINCES = new TypeToken<List<Province>>() {}.getType();
private static final Type REGENCIES = new TypeToken<List<Regency>>() {}.getType();
private static final Type DISTRICT = new TypeToken<List<District>>() {}.getType();
private static final Type VILLAGE = new TypeToken<List<Village>>() {}.getType();
private static final String EMPTY_JSON = "[{}]";
private static final Type PROVINCES = TypeToken.getParameterized(List.class, Province.class)
.getType();
private static final Type REGENCIES = TypeToken.getParameterized(List.class, Regency.class)
.getType();
private static final Type DISTRICT = TypeToken.getParameterized(List.class, District.class)
.getType();
private static final Type VILLAGE = TypeToken.getParameterized(List.class, Village.class)
.getType();

public static final Gson gson = new Gson();
public static final String PROVINCE_TREE_PATH = "./resources/list_of_area/indonesia-region.min.json";
public static final String PROVINCE_PATH = "./resources/list_of_area/provinces.json";
public static final String REGENCY_PATH = "./resources/list_of_area/regencies.json";
public static final String DISTRICT_PATH = "./resources/list_of_area/districts.json";
public static final String VILLAGE_PATH = "./resources/list_of_area/villages.json";

public static List<Province> provinces() throws IOException, LocationException {
return gson.fromJson(Loader.read(PROVINCE_PATH), PROVINCES);
}

public static List<Province> Provinces() throws Exception {
String provinces = Loader.read("./resources/list_of_area/provinces.json");
return gson.fromJson(provinces, PROVINCES);
@Deprecated
public static List<Province> Provinces() throws IOException, LocationException {
return provinces();
}

public static List<Regency> Regencies() throws Exception {
String regencies = Loader.read("./resources/list_of_area/regencies.json");
return gson.fromJson(regencies, REGENCIES);
public static List<Regency> regencies() throws IOException, LocationException {
return gson.fromJson(Loader.read(REGENCY_PATH), REGENCIES);
}

public static List<District> Districts() throws Exception {
String districts = Loader.read("./resources/list_of_area/districts.json");
return gson.fromJson(districts, DISTRICT);
@Deprecated
public static List<Regency> Regencies() throws IOException, LocationException {
return regencies();
}

public static List<Village> Villages() throws Exception {
String villages = Loader.read("./resources/list_of_area/villages.json");
return gson.fromJson(villages, VILLAGE);
public static List<District> districts() throws IOException, LocationException {
return gson.fromJson(Loader.read(DISTRICT_PATH), DISTRICT);
}

public static List<Province> ProvinceTrees() throws Exception {
String treeStr = Loader.read("./resources/list_of_area/indonesia-region.min.json");
List<Province> tree = gson.fromJson(treeStr, PROVINCES);
return tree;
@Deprecated
public static List<District> Districts() throws IOException, LocationException {
return districts();
}

public static List<Province> ProvinceTrees(TREE_LEVEL tree_level) throws Exception {
String provincesStr = "[{}]";
String regenciesStr = "[{}]";
String districtsStr = "[{}]";
String villagesStr = "[{}]";
public static List<Village> villages() throws IOException, LocationException {
return gson.fromJson(Loader.read(VILLAGE_PATH), VILLAGE);
}

switch (tree_level) {
case PROVINCE:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
break;
@Deprecated
public static List<Village> Villages() throws IOException, LocationException {
return villages();
}

case REGENCY:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
regenciesStr = Loader.read("./resources/list_of_area/regencies.json");
break;
public static List<Province> provinceTrees() throws IOException, LocationException {
return gson.fromJson(Loader.read(PROVINCE_TREE_PATH), PROVINCES);
}

case DISTRICT:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
regenciesStr = Loader.read("./resources/list_of_area/regencies.json");
districtsStr = Loader.read("./resources/list_of_area/districts.json");
break;
public static List<Province> provinceTrees(final TREE_LEVEL treeLevel)
throws IOException, LocationException {

if (treeLevel == null) {
throw new LocationException();
}

final String provincesStr = Loader.read(PROVINCE_PATH);
String regenciesStr = EMPTY_JSON;
String districtsStr = EMPTY_JSON;
String villagesStr = EMPTY_JSON;

switch (treeLevel) {
case VILLAGE:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
regenciesStr = Loader.read("./resources/list_of_area/regencies.json");
districtsStr = Loader.read("./resources/list_of_area/districts.json");
villagesStr = Loader.read("./resources/list_of_area/villages.json");
villagesStr = Loader.read(VILLAGE_PATH);
// fallthrough
case DISTRICT:
districtsStr = Loader.read(DISTRICT_PATH);
// fallthrough
case REGENCY:
regenciesStr = Loader.read(REGENCY_PATH);
// fallthrough
case PROVINCE:
break;

default:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
break;
}


List<Province> provinces = gson.fromJson(provincesStr, PROVINCES);
List<Regency> regencies = gson.fromJson(regenciesStr, REGENCIES);
List<District> districts = gson.fromJson(districtsStr, DISTRICT);
List<Village> villages = gson.fromJson(villagesStr, VILLAGE);

final long start = System.currentTimeMillis();
List<Province> newProvinces = new ArrayList<>();
provinces.forEach(p -> {

List<Regency> newRegencies = new ArrayList<>();
regencies.forEach(r -> {
if(p.getId().equals( r.getProvince_id() )) {

List<District> newDistricts = new ArrayList<>();
districts.forEach(d -> {
if(r.getId().equals( d.getRegency_id() )) {

List<Village> newVillages = new ArrayList<>();
villages.forEach(v -> {
if(d.getId().equals( v.getDistrict_id() )) {
newVillages.add(v);
}
});

d.setVillages(newVillages);
newDistricts.add(d);
}
});

r.setDistricts(newDistricts);
newRegencies.add(r);
}
});

p.setRegencies(newRegencies);
newProvinces.add(p);
});

logger.debug("Mapping location tree dependency took " + (System.currentTimeMillis() - start) + " ms");

final List<Province> newProvinces = provinces.stream()
.map(province -> Utils.setChild(province, regencies, districts, villages))
.collect(Collectors.toList());

log.debug("Mapping location tree dependency took {} ms",
(System.currentTimeMillis() - start));
return newProvinces;
}

@Deprecated
public static List<Province> ProvinceTrees() throws IOException, LocationException {
return provinceTrees();
}

@Deprecated
public static List<Province> ProvinceTrees(final TREE_LEVEL treeLevel)
throws IOException, LocationException {
return provinceTrees(treeLevel);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wilayah.indonesia;


import lombok.NoArgsConstructor;

@NoArgsConstructor
public class LocationException extends RuntimeException {

public LocationException(String message) {
super(message);
}
}
Loading