From db9bbf974c9c190615ebea37c83069be146e5975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Szathma=CC=81ry?= Date: Mon, 4 Jan 2016 09:47:41 +0100 Subject: [PATCH] make NoCache a singleton --- sample/Benchmark.java | 4 ++-- src/main/java/com/maxmind/db/NoCache.java | 11 ++++++++++- src/main/java/com/maxmind/db/Reader.java | 8 +++----- src/test/java/com/maxmind/db/PointerTest.java | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sample/Benchmark.java b/sample/Benchmark.java index 665c3e44..c21126ba 100644 --- a/sample/Benchmark.java +++ b/sample/Benchmark.java @@ -21,8 +21,8 @@ public class Benchmark { public static void main(String[] args) throws IOException, InvalidDatabaseException { File file = new File(args.length > 0 ? args[0] : "GeoLite2-City.mmdb"); System.out.println("No caching"); - loop("Warming up", file, WARMUPS, new NoCache()); - loop("Benchmarking", file, BENCHMARKS, new NoCache()); + loop("Warming up", file, WARMUPS, NoCache.getInstance()); + loop("Benchmarking", file, BENCHMARKS, NoCache.getInstance()); System.out.println("With caching"); loop("Warming up", file, WARMUPS, new CHMCache()); diff --git a/src/main/java/com/maxmind/db/NoCache.java b/src/main/java/com/maxmind/db/NoCache.java index 176747a3..45276a27 100644 --- a/src/main/java/com/maxmind/db/NoCache.java +++ b/src/main/java/com/maxmind/db/NoCache.java @@ -5,13 +5,22 @@ import com.fasterxml.jackson.databind.JsonNode; /** - * A no-op cache. + * A no-op cache singleton. */ public class NoCache implements NodeCache { + private static final NoCache INSTANCE = new NoCache(); + + private NoCache() { + } + @Override public JsonNode get(int key, Loader loader) throws IOException { return loader.load(key); } + public static NoCache getInstance() { + return INSTANCE; + } + } diff --git a/src/main/java/com/maxmind/db/Reader.java b/src/main/java/com/maxmind/db/Reader.java index 198f0ff3..32d0b864 100644 --- a/src/main/java/com/maxmind/db/Reader.java +++ b/src/main/java/com/maxmind/db/Reader.java @@ -20,8 +20,6 @@ public final class Reader implements Closeable { (byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.', 'c', 'o', 'm'}; - private static final NodeCache NO_CACHE = new NoCache(); - private final int ipV4Start; private final Metadata metadata; private final AtomicReference bufferHolderReference; @@ -52,7 +50,7 @@ public enum FileMode { * @throws IOException if there is an error opening or reading from the file. */ public Reader(File database) throws IOException { - this(database, NO_CACHE); + this(database, NoCache.getInstance()); } /** @@ -76,7 +74,7 @@ public Reader(File database, NodeCache cache) throws IOException { * @throws IOException if there is an error reading from the Stream. */ public Reader(InputStream source) throws IOException { - this(source, NO_CACHE); + this(source, NoCache.getInstance()); } /** @@ -101,7 +99,7 @@ public Reader(InputStream source, NodeCache cache) throws IOException { * @throws IOException if there is an error opening or reading from the file. */ public Reader(File database, FileMode fileMode) throws IOException { - this(database, fileMode, NO_CACHE); + this(database, fileMode, NoCache.getInstance()); } /** diff --git a/src/test/java/com/maxmind/db/PointerTest.java b/src/test/java/com/maxmind/db/PointerTest.java index 54531987..f1067cf2 100644 --- a/src/test/java/com/maxmind/db/PointerTest.java +++ b/src/test/java/com/maxmind/db/PointerTest.java @@ -17,7 +17,7 @@ public class PointerTest { public void testWithPointers() throws IOException { File file = ReaderTest.getFile("maps-with-pointers.raw"); BufferHolder ptf = new BufferHolder(file, FileMode.MEMORY); - Decoder decoder = new Decoder(new NoCache(), ptf.get(), 0); + Decoder decoder = new Decoder(NoCache.getInstance(), ptf.get(), 0); ObjectMapper om = new ObjectMapper();