Skip to content

Commit 6f5ac59

Browse files
Sam ShpuntoffSam Shpuntoff
authored andcommitted
fix: correct usage of cleaner api
1 parent 89744ce commit 6f5ac59

4 files changed

Lines changed: 68 additions & 22 deletions

File tree

‎src/main/java/com/mapzen/jpostal/AddressExpander.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.concurrent.atomic.AtomicBoolean;
66

77
public class AddressExpander {
8-
private static final Cleaner cleaner = Cleaner.create();
98
private static final AtomicBoolean isShutdown = new AtomicBoolean(false);
109

1110
private volatile static AddressExpander instance = null;
@@ -79,8 +78,20 @@ public String[] expandAddressWithOptions(String address, ExpanderOptions options
7978
}
8079
}
8180

82-
// Register cleanup
83-
this.cleanable = cleaner.register(this, () -> {
81+
this.cleanable = Utils.registerForCleanup(this, new CleanupAction(isShutdown));
82+
}
83+
84+
private static class CleanupAction implements Runnable {
85+
private final AtomicBoolean isShutdown;
86+
private final LibPostal libPostal;
87+
88+
CleanupAction(AtomicBoolean isShutdown, LibPostal libPostal) {
89+
this.isShutdown = isShutdown;
90+
this.libPostal = libPostal;
91+
}
92+
93+
@Override
94+
public void run() {
8495
if (isShutdown.compareAndSet(false, true)) {
8596
try {
8697
synchronized (libPostal) {
@@ -90,6 +101,6 @@ public String[] expandAddressWithOptions(String address, ExpanderOptions options
90101
System.err.println("AddressExpander cleaner error: " + e.getMessage());
91102
}
92103
}
93-
});
104+
}
94105
}
95-
}
106+
}

‎src/main/java/com/mapzen/jpostal/AddressParser.java‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.concurrent.atomic.AtomicBoolean;
77

88
public class AddressParser {
9-
private static final Cleaner cleaner = Cleaner.create();
109
private static final AtomicBoolean isShutdown = new AtomicBoolean(false);
1110

1211
private volatile static AddressParser instance = null;
@@ -80,8 +79,20 @@ public ParsedComponent[] parseAddressWithOptions(String address, ParserOptions o
8079
}
8180
}
8281

83-
// Register cleanup
84-
this.cleanable = cleaner.register(this, () -> {
82+
this.cleanable = Utils.registerForCleanup(this, new CleanupAction(isShutdown));
83+
}
84+
85+
private static class CleanupAction implements Runnable {
86+
private final AtomicBoolean isShutdown;
87+
private final LibPostal libPostal;
88+
89+
CleanupAction(AtomicBoolean isShutdown, LibPostal libPostal) {
90+
this.isShutdown = isShutdown;
91+
this.libPostal = libPostal;
92+
}
93+
94+
@Override
95+
public void run() {
8596
if (isShutdown.compareAndSet(false, true)) {
8697
try {
8798
synchronized (libPostal) {
@@ -91,6 +102,6 @@ public ParsedComponent[] parseAddressWithOptions(String address, ParserOptions o
91102
System.err.println("AddressParser cleaner error: " + e.getMessage());
92103
}
93104
}
94-
});
105+
}
95106
}
96107
}

‎src/main/java/com/mapzen/jpostal/LibPostal.java‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.concurrent.atomic.AtomicBoolean;
55

66
final class LibPostal {
7-
private static final Cleaner cleaner = Cleaner.create();
87
private static final AtomicBoolean isShutdown = new AtomicBoolean(false);
98

109
private final Config config;
@@ -25,17 +24,7 @@ private LibPostal(final Config config) {
2524
}
2625

2726
this.config = config;
28-
29-
// Register cleanup
30-
this.cleanable = cleaner.register(this, () -> {
31-
if (isShutdown.compareAndSet(false, true)) {
32-
try {
33-
teardown();
34-
} catch (Exception e) {
35-
System.err.println("LibPostal teardown error: " + e.getMessage());
36-
}
37-
}
38-
});
27+
this.cleanable = Utils.registerForCleanup(this, new CleanupAction(isShutdown));
3928
}
4029

4130
Config getConfig() {
@@ -60,4 +49,23 @@ static LibPostal getInstance(final Config config) {
6049
}
6150
return instance;
6251
}
63-
}
52+
53+
private static class CleanupAction implements Runnable {
54+
private final AtomicBoolean isShutdown;
55+
56+
CleanupAction(AtomicBoolean isShutdown) {
57+
this.isShutdown = isShutdown;
58+
}
59+
60+
@Override
61+
public void run() {
62+
if (isShutdown.compareAndSet(false, true)) {
63+
try {
64+
teardown();
65+
} catch (Exception e) {
66+
System.err.println("LibPostal teardown error: " + e.getMessage());
67+
}
68+
}
69+
}
70+
}
71+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mapzen.jpostal;
2+
3+
import java.lang.ref.Cleaner;
4+
5+
final class Utils {
6+
7+
private static final Cleaner CLEANER = Cleaner.create();
8+
9+
private Utils() {
10+
throw new UnsupportedOperationException("Utility class");
11+
}
12+
13+
static Cleaner.Cleanable registerForCleanup(Object obj, Runnable cleanupAction) {
14+
return CLEANER.register(obj, cleanupAction);
15+
}
16+
}

0 commit comments

Comments
 (0)