/
AddressExpander.java
61 lines (49 loc) · 1.73 KB
/
AddressExpander.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.mapzen.jpostal;
import com.mapzen.jpostal.ExpanderOptions;
public class AddressExpander {
static {
System.loadLibrary("jpostal"); // Load native library at runtime
}
private volatile static AddressExpander instance = null;
public static AddressExpander getInstanceDataDir(String dataDir) {
if (instance == null) {
synchronized(AddressExpander.class) {
if (instance == null) {
instance = new AddressExpander(dataDir);
}
}
}
return instance;
}
public static AddressExpander getInstance() {
return getInstanceDataDir(null);
}
static native synchronized void setup();
static native synchronized void setupDataDir(String dataDir);
private static native synchronized String[] libpostalExpand(String address, ExpanderOptions options);
static native synchronized void teardown();
public String[] expandAddress(String address) {
return expandAddressWithOptions(address, new ExpanderOptions.Builder().build());
}
public String[] expandAddressWithOptions(String address, ExpanderOptions options) {
if (address == null) {
throw new NullPointerException("String address must not be null");
}
if (options == null) {
throw new NullPointerException("ExpanderOptions options must not be null");
}
synchronized(this) {
return libpostalExpand(address, options);
}
}
protected AddressExpander(String dataDir) {
if (dataDir == null) {
setup();
} else {
setupDataDir(dataDir);
}
}
protected void finalize() {
teardown();
}
}