Skip to content

Commit

Permalink
refactor source
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufsyaifudin committed Nov 6, 2016
1 parent 42d068e commit 6d7fff1
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 0 deletions.
32 changes: 32 additions & 0 deletions wilayah-indonesia/src/main/java/wilayah/indonesia/Loader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package wilayah.indonesia;

import java.io.*;
import org.apache.log4j.Logger;

/**
* Created by yusuf on 24/10/16.
*/
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
*/
public static String read(String file) throws Exception {
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);
}
in.close();

logger.debug("Load " + file + " took " + (System.currentTimeMillis() - start) + " ms");
return line;
}
}
130 changes: 130 additions & 0 deletions wilayah-indonesia/src/main/java/wilayah/indonesia/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package wilayah.indonesia;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
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.
*/
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();

public static final Gson gson = new Gson();

public static List<Province> Provinces() throws Exception {
String provinces = Loader.read("./resources/list_of_area/provinces.json");
return gson.fromJson(provinces, 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<District> Districts() throws Exception {
String districts = Loader.read("./resources/list_of_area/districts.json");
return gson.fromJson(districts, DISTRICT);
}

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<Province> ProvinceTrees(TREE_LEVEL tree_level) throws Exception {
String provincesStr = "[{}]";
String regenciesStr = "[{}]";
String districtsStr = "[{}]";
String villagesStr = "[{}]";

switch (tree_level) {
case PROVINCE:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
break;

case REGENCY:
provincesStr = Loader.read("./resources/list_of_area/provinces.json");
regenciesStr = Loader.read("./resources/list_of_area/regencies.json");
break;

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;

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");
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");
return newProvinces;
}

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

import java.util.ArrayList;
import java.util.List;

/**
* Created by yusuf on 24/10/16.
*/
public class District {
private String id;
private String regency_id;
private String name;
private String alt_name;
private Double latitude;
private Double longitude;
private List<Village> villages;

public String getId() { return id; }

public void setId(String id) { this.id = id; }

public String getRegency_id() { return regency_id; }

public void setRegency_id(String regency_id) { this.regency_id = regency_id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getAlt_name() { return alt_name; }

public void setAlt_name(String alt_name) { this.alt_name = alt_name; }

public Double getLatitude() {
return (latitude == null) ? 0 : latitude;
}

public void setLatitude(Double latitude) { this.latitude = latitude; }

public Double getLongitude() {
return (longitude == null) ? 0 : longitude;
}

public void setLongitude(Double longitude) { this.longitude = longitude; }

public List<Village> getVillages() {
return (villages != null) ? villages : new ArrayList<>();
}

public void setVillages(List<Village> villages) {
this.villages = villages;
}

public String toString() {
return "{id: " + getId().toString() +
", regency_id: " + getRegency_id().toString() +
", name: " + getName() +
", alt_name: " + getAlt_name() +
", latitude: " + getLatitude().toString() +
", longitute: " + getLongitude().toString() +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package wilayah.indonesia.model;

import java.util.ArrayList;
import java.util.List;

/**
* Created by yusuf on 24/10/16.
*/
public class Province {
private String id;
private String name;
private String alt_name;
private Double latitude;
private Double longitude;
private List<Regency> regencies;

public String getId() { return id; }

public void setId(String id) { this.id = id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getAlt_name() { return alt_name; }

public void setAlt_name(String alt_name) { this.alt_name = alt_name; }

public Double getLatitude() {
return (latitude == null) ? 0 : latitude;
}

public void setLatitude(Double latitude) { this.latitude = latitude; }

public Double getLongitude() {
return (longitude == null) ? 0 : longitude;
}

public void setLongitude(Double longitude) { this.longitude = longitude; }

public List<Regency> getRegencies() {
return (regencies != null) ? regencies : new ArrayList<>();
}

public void setRegencies(List<Regency> regencies) {
this.regencies = regencies;
}

public String toString() {
return "{id: " + getId().toString() +
", name: " + getName() +
", alt_name: " + getAlt_name() +
", latitude: " + getLatitude().toString() +
", longitute: " + getLongitude().toString() +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package wilayah.indonesia.model;

import java.util.ArrayList;
import java.util.List;

/**
* Created by yusuf on 24/10/16.
*/
public class Regency {

private String id;
private String province_id;
private String name;
private String alt_name;
private Double latitude;
private Double longitude;
private List<District> districts;

public String getId() { return id; }

public void setId(String id) { this.id = id; }

public String getProvince_id() { return province_id; }

public void setProvince_id(String province_id) { this.province_id = province_id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getAlt_name() { return alt_name; }

public void setAlt_name(String alt_name) { this.alt_name = alt_name; }

public Double getLatitude() {
return (latitude == null) ? 0 : latitude;
}

public void setLatitude(Double latitude) { this.latitude = latitude; }

public Double getLongitude() {
return (longitude == null) ? 0 : longitude;
}

public void setLongitude(Double longitude) { this.longitude = longitude; }

public List<District> getDistricts() {
return (districts != null) ? districts : new ArrayList<>();
}

public void setDistricts(List<District> districts) {
this.districts = districts;
}


public String toString() {
return "{id: " + getId().toString() +
", province_id: " + getProvince_id().toString() +
", name: " + getName() +
", alt_name: " + getAlt_name() +
", latitude: " + getLatitude().toString() +
", longitute: " + getLongitude().toString() +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package wilayah.indonesia.model;

/**
* Created by yusuf on 24/10/16.
*/
public class Village {
private String id;
private String district_id;
private String name;
private String alt_name;
private Double latitude;
private Double longitude;

public String getId() { return id; }

public void setId(String id) { this.id = id; }

public String getDistrict_id() { return district_id; }

public void setDistrict_id(String district_id) { this.district_id = district_id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public Double getLatitude() {
return (latitude == null) ? 0 : latitude;
}

public void setLatitude(Double latitude) { this.latitude = latitude; }

public Double getLongitude() {
return (longitude == null) ? 0 : longitude;
}

public void setLongitude(Double longitude) { this.longitude = longitude; }

public String toString() {
return "{id: " + getId().toString() +
", district_id: " + getDistrict_id() +
", name: " + getName() +
", latitude: " + getLatitude().toString() +
", longitute: " + getLongitude().toString() +
"}";
}
}
Loading

0 comments on commit 6d7fff1

Please sign in to comment.