Made loop continue statements easier to understand

Removed ScatterGraphCreator
This commit is contained in:
Rsl1122 2017-08-16 12:22:54 +03:00
parent 26ab4e94d2
commit 174eaa7f31
6 changed files with 58 additions and 123 deletions

View File

@ -12,7 +12,7 @@ import java.util.UUID;
public class KillData {
private final UUID victim;
private final int victimUserID;
private int victimUserID;
private final long date;
private final String weapon;
@ -98,4 +98,7 @@ public class KillData {
return hash;
}
public void setVictimUserID(int victimUserID) {
this.victimUserID = victimUserID;
}
}

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
@ -9,7 +10,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Rsl1122
@ -108,14 +108,11 @@ public class KillsTable extends Table {
* @throws SQLException
*/
public void savePlayerKills(int userId, List<KillData> kills) throws SQLException {
if (kills == null) {
if (Verify.isEmpty(kills)) {
return;
}
Benchmark.start("Save Kills");
kills.removeAll(getPlayerKills(userId));
if (kills.isEmpty()) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
@ -125,22 +122,24 @@ public class KillsTable extends Table {
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
for (KillData kill : kills) {
if (kill == null) {
continue;
}
statement.setInt(1, userId);
int victimUserID = kill.getVictimUserID();
kills.stream().filter(Objects::nonNull).forEach(killData -> {
int victimUserID = killData.getVictimUserID();
if (victimUserID == -1) {
victimUserID = db.getUsersTable().getUserId(kill.getVictim());
if (victimUserID == -1) {
continue;
try {
int newVictimID = db.getUsersTable().getUserId(killData.getVictim());
killData.setVictimUserID(newVictimID);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return;
}
}
statement.setInt(2, victimUserID);
});
for (KillData kill : kills) {
if (kill == null || kill.getVictimUserID() == -1) {
continue;
}
statement.setInt(1, userId);
statement.setInt(2, kill.getVictimUserID());
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getDate());
statement.addBatch();
@ -215,8 +214,6 @@ public class KillsTable extends Table {
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
int i = 0;
for (Map.Entry<Integer, List<KillData>> entrySet : kills.entrySet()) {
Integer id = entrySet.getKey();
List<KillData> playerKills = entrySet.getValue();
@ -227,32 +224,28 @@ public class KillsTable extends Table {
playerKills.removeAll(s);
}
playerKills.stream().filter(Objects::nonNull).forEach(killData -> {
int victimUserID = killData.getVictimUserID();
if (victimUserID == -1) {
try {
int newVictimID = db.getUsersTable().getUserId(killData.getVictim());
killData.setVictimUserID(newVictimID);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return;
}
}
});
for (KillData kill : playerKills) {
if (kill == null) {
if (kill == null || kill.getVictimUserID() == -1) {
continue;
}
statement.setInt(1, id);
int victimUserID = kill.getVictimUserID();
if (victimUserID == -1) {
List<Integer> matchingIds = uuids.entrySet()
.stream().filter(e -> e.getValue().equals(kill.getVictim()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (matchingIds.isEmpty()) {
continue;
}
victimUserID = matchingIds.get(0);
}
statement.setInt(2, victimUserID);
statement.setInt(2, kill.getVictimUserID());
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getDate());
statement.addBatch();
commitRequired = true;
i++;
}
if (commitRequired) {

View File

@ -166,12 +166,9 @@ public class NicknamesTable extends UserIDTable {
set = statement.executeQuery();
while (set.next()) {
Integer id = set.getInt(columnUserID);
if (!ids.contains(id)) {
continue;
}
String nickname = set.getString(columnNick);
if (nickname.isEmpty()) {
if (!ids.contains(id) || nickname.isEmpty()) {
continue;
}

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import com.djrapitops.plugin.utilities.player.Fetch;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.UserData;
@ -732,34 +733,33 @@ public class UsersTable extends Table {
private List<UserData> updateExistingUserData(Collection<UserData> data) throws SQLException {
PreparedStatement statement = null;
try {
List<UserData> saveLast = new ArrayList<>();
List<UserData> newUserData = new ArrayList<>();
String sql = getUpdateStatement();
statement = prepareStatement(sql);
boolean commitRequired = false;
Set<UUID> savedUUIDs = getSavedUUIDs();
int i = 0;
for (UserData uData : data) {
if (uData == null) {
continue;
}
UUID uuid = uData.getUuid();
if (uuid == null) {
try {
uData.setUuid(UUIDUtility.getUUIDOf(uData.getName(), db));
} catch (Exception ex) {
continue;
UUID uuid = null;
// Get new UUID if uuid == null
if (uData != null) {
uuid = uData.getUuid();
if (uuid == null) {
uuid = UUIDUtility.getUUIDOf(uData.getName(), db);
uData.setUuid(uuid);
}
}
uuid = uData.getUuid();
if (uuid == null) {
continue;
}
if (!savedUUIDs.contains(uuid)) {
if (!saveLast.contains(uData)) {
saveLast.add(uData);
}
boolean isNew = !savedUUIDs.contains(uuid) && !newUserData.contains(uData);
if (isNew) {
newUserData.add(uData);
}
if (!Verify.notNull(uData, uuid) || isNew) {
continue;
}
uData.access();
statement.setString(1, uData.getGeolocation());
GMTimes gmTimes = uData.getGmTimes();
@ -782,12 +782,11 @@ public class UsersTable extends Table {
statement.addBatch();
uData.stopAccessing();
commitRequired = true;
i++;
}
if (commitRequired) {
statement.executeBatch();
}
return saveLast;
return newUserData;
} finally {
close(statement);
}

View File

@ -33,6 +33,6 @@ public class PlayerActivityGraphCreator {
.map(session -> new Point[]{new Point(session.getSessionStart(), 1), new Point(session.getSessionEnd(), 0)})
.flatMap(Arrays::stream)
.collect(Collectors.toList());
return ScatterGraphCreator.scatterGraph(points, true, false);
return SeriesCreator.seriesGraph(points, true, false);
}
}

View File

@ -1,57 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.ui.html.graphs;
import main.java.com.djrapitops.plan.utilities.analysis.DouglasPeuckerAlgorithm;
import main.java.com.djrapitops.plan.utilities.analysis.Point;
import main.java.com.djrapitops.plan.utilities.analysis.ReduceGapTriangles;
import java.util.List;
/**
* Abstract scatter graph creator used by other graph creators.
*
* @author Rsl1122
* @since 3.5.2
*/
public class ScatterGraphCreator {
/**
* Constructor used to hide the public constructor
*/
private ScatterGraphCreator() {
throw new IllegalStateException("Utility class");
}
public static String scatterGraph(List<Point> points, boolean reduceGapTriangles) {
return scatterGraph(points, reduceGapTriangles, true);
}
public static String scatterGraph(List<Point> points, boolean reduceGapTriangles, boolean reducePoints) {
StringBuilder arrayBuilder = new StringBuilder("[");
if (reducePoints) {
points = DouglasPeuckerAlgorithm.reducePoints(points, 0);
}
if (reduceGapTriangles) {
points = ReduceGapTriangles.reduce(points);
}
int size = points.size();
for (int i = 0; i < size; i++) {
Point point = points.get(i);
double y = point.getY();
long date = (long) point.getX();
arrayBuilder.append("{").append("x:").append(date).append(", y:").append(y).append("}");
if (i < size - 1) {
arrayBuilder.append(",");
}
}
arrayBuilder.append("]");
return arrayBuilder.toString();
}
}