From e0b05cc169b96e52c193c7fb9c0d383fcd33acf7 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 4 Aug 2018 12:09:10 +0300 Subject: [PATCH] Fix a code smell --- .../plan/data/store/mutators/TPSMutator.java | 9 +++- .../databases/sql/patches/IPAnonPatch.java | 42 ++++++++++--------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java b/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java index 5ca6176cb..302d8e914 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java @@ -10,6 +10,7 @@ import com.djrapitops.plugin.api.TimeAmount; import java.util.ArrayList; import java.util.List; import java.util.OptionalDouble; +import java.util.function.Predicate; import java.util.stream.Collectors; /** @@ -35,12 +36,16 @@ public class TPSMutator { return new TPSMutator(new ArrayList<>(mutator.tpsData)); } - public TPSMutator filterDataBetween(long after, long before) { + public TPSMutator filterBy(Predicate filter) { return new TPSMutator(tpsData.stream() - .filter(tps -> tps.getDate() >= after && tps.getDate() <= before) + .filter(filter) .collect(Collectors.toList())); } + public TPSMutator filterDataBetween(long after, long before) { + return filterBy(tps -> tps.getDate() >= after && tps.getDate() <= before); + } + public List all() { return tpsData; } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/IPAnonPatch.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/IPAnonPatch.java index c2240be62..8a56a38b6 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/IPAnonPatch.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/IPAnonPatch.java @@ -64,25 +64,29 @@ public class IPAnonPatch extends Patch { public void prepare(PreparedStatement statement) throws SQLException { for (List geoInfos : allGeoInfo.values()) { for (GeoInfo geoInfo : geoInfos) { - try { - String oldIP = geoInfo.getIp(); - if (oldIP.endsWith(".xx.xx") || oldIP.endsWith("xx..")) { - continue; - } - GeoInfo updatedInfo = new GeoInfo( - InetAddress.getByName(oldIP), - geoInfo.getGeolocation(), - geoInfo.getDate() - ); - statement.setString(1, updatedInfo.getIp()); - statement.setString(2, updatedInfo.getIpHash()); - statement.setString(3, geoInfo.getIp()); - statement.addBatch(); - } catch (UnknownHostException | UnsupportedEncodingException | NoSuchAlgorithmException e) { - if (Settings.DEV_MODE.isTrue()) { - Log.toLog(this.getClass(), e); - } - } + addToBatch(statement, geoInfo); + } + } + } + + private void addToBatch(PreparedStatement statement, GeoInfo geoInfo) throws SQLException { + try { + String oldIP = geoInfo.getIp(); + if (oldIP.endsWith(".xx.xx") || oldIP.endsWith("xx..")) { + return; + } + GeoInfo updatedInfo = new GeoInfo( + InetAddress.getByName(oldIP), + geoInfo.getGeolocation(), + geoInfo.getDate() + ); + statement.setString(1, updatedInfo.getIp()); + statement.setString(2, updatedInfo.getIpHash()); + statement.setString(3, geoInfo.getIp()); + statement.addBatch(); + } catch (UnknownHostException | UnsupportedEncodingException | NoSuchAlgorithmException e) { + if (Settings.DEV_MODE.isTrue()) { + Log.toLog(this.getClass(), e); } } }