This commit is contained in:
Brokkonaut
2019-06-23 16:45:14 +02:00
parent 76f7f8701d
commit 1525d7682f
25 changed files with 260 additions and 285 deletions

View File

@@ -1,14 +1,48 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public enum Logging { public enum Logging {
BLOCKPLACE(true), BLOCKBREAK(true), SIGNTEXT(true), TNTEXPLOSION(true), CREEPEREXPLOSION(true), BLOCKPLACE(true),
GHASTFIREBALLEXPLOSION(true), ENDERDRAGON(true), MISCEXPLOSION(true), FIRE(true), LEAVESDECAY, BLOCKBREAK(true),
LAVAFLOW, WATERFLOW, CHESTACCESS, KILL, CHAT, SNOWFORM, SNOWFADE, DOORINTERACT, SIGNTEXT(true),
SWITCHINTERACT, CAKEEAT, ENDERMEN, NOTEBLOCKINTERACT, DIODEINTERACT, COMPARATORINTERACT, TNTEXPLOSION(true),
PRESUREPLATEINTERACT, TRIPWIREINTERACT, CREATURECROPTRAMPLE, CROPTRAMPLE, CREEPEREXPLOSION(true),
NATURALSTRUCTUREGROW, GRASSGROWTH, MYCELIUMSPREAD, VINEGROWTH, MUSHROOMSPREAD, GHASTFIREBALLEXPLOSION(true),
WITHER(true), WITHER_SKULL(true), BONEMEALSTRUCTUREGROW, WORLDEDIT, TNTMINECARTEXPLOSION(true), ENDERDRAGON(true),
ENDERCRYSTALEXPLOSION(true), BEDEXPLOSION(true), DRAGONEGGTELEPORT(true), DAYLIGHTDETECTORINTERACT, MISCEXPLOSION(true),
FIRE(true),
LEAVESDECAY,
LAVAFLOW,
WATERFLOW,
CHESTACCESS,
KILL,
CHAT,
SNOWFORM,
SNOWFADE,
DOORINTERACT,
SWITCHINTERACT,
CAKEEAT,
ENDERMEN,
NOTEBLOCKINTERACT,
DIODEINTERACT,
COMPARATORINTERACT,
PRESUREPLATEINTERACT,
TRIPWIREINTERACT,
CREATURECROPTRAMPLE,
CROPTRAMPLE,
NATURALSTRUCTUREGROW,
GRASSGROWTH,
MYCELIUMSPREAD,
VINEGROWTH,
MUSHROOMSPREAD,
WITHER(true),
WITHER_SKULL(true),
BONEMEALSTRUCTUREGROW,
WORLDEDIT,
TNTMINECARTEXPLOSION(true),
ENDERCRYSTALEXPLOSION(true),
BEDEXPLOSION(true),
DRAGONEGGTELEPORT(true),
DAYLIGHTDETECTORINTERACT,
LECTERNBOOKCHANGE(true); LECTERNBOOKCHANGE(true);
public static final int length = Logging.values().length; public static final int length = Logging.values().length;

View File

@@ -291,21 +291,26 @@ public final class QueryParams implements Cloneable {
} }
if (bct == BlockChangeType.KILLS) { if (bct == BlockChangeType.KILLS) {
if (sum == SummarizationMode.PLAYERS) { if (sum == SummarizationMode.PLAYERS) {
return "SELECT playername, UUID, SUM(kills) AS kills, SUM(killed) AS killed FROM ((SELECT killer AS playerid, count(*) AS kills, 0 as killed FROM `" + getTable() + "-kills` INNER JOIN `lb-players` as killers ON (killer=killers.playerid) INNER JOIN `lb-players` as victims ON (victim=victims.playerid) " + getWhere(BlockChangeType.KILLS) + "GROUP BY killer) UNION (SELECT victim AS playerid, 0 as kills, count(*) AS killed FROM `" + getTable() + "-kills` INNER JOIN `lb-players` as killers ON (killer=killers.playerid) INNER JOIN `lb-players` as victims ON (victim=victims.playerid) " + getWhere(BlockChangeType.KILLS) + "GROUP BY victim)) AS t INNER JOIN `lb-players` USING (playerid) GROUP BY playerid ORDER BY SUM(kills) + SUM(killed) " + order + " " + getLimit(); return "SELECT playername, UUID, SUM(kills) AS kills, SUM(killed) AS killed FROM ((SELECT killer AS playerid, count(*) AS kills, 0 as killed FROM `" + getTable() + "-kills` INNER JOIN `lb-players` as killers ON (killer=killers.playerid) INNER JOIN `lb-players` as victims ON (victim=victims.playerid) " + getWhere(BlockChangeType.KILLS) + "GROUP BY killer) UNION (SELECT victim AS playerid, 0 as kills, count(*) AS killed FROM `" + getTable()
+ "-kills` INNER JOIN `lb-players` as killers ON (killer=killers.playerid) INNER JOIN `lb-players` as victims ON (victim=victims.playerid) " + getWhere(BlockChangeType.KILLS) + "GROUP BY victim)) AS t INNER JOIN `lb-players` USING (playerid) GROUP BY playerid ORDER BY SUM(kills) + SUM(killed) " + order + " " + getLimit();
} }
throw new IllegalStateException("Invalid summarization for kills"); throw new IllegalStateException("Invalid summarization for kills");
} }
if (bct == BlockChangeType.ENTITIES || bct == BlockChangeType.ENTITIES_CREATED || bct == BlockChangeType.ENTITIES_KILLED) { if (bct == BlockChangeType.ENTITIES || bct == BlockChangeType.ENTITIES_CREATED || bct == BlockChangeType.ENTITIES_KILLED) {
if (sum == SummarizationMode.TYPES) { if (sum == SummarizationMode.TYPES) {
return "SELECT entitytypeid, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT entitytypeid, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-entities` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.ENTITIES_CREATED) + "GROUP BY entitytypeid) UNION (SELECT entitytypeid, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-entities` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.ENTITIES_KILLED) + "GROUP BY entitytypeid)) AS t GROUP BY entitytypeid ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit(); return "SELECT entitytypeid, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT entitytypeid, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-entities` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.ENTITIES_CREATED) + "GROUP BY entitytypeid) UNION (SELECT entitytypeid, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-entities` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.ENTITIES_KILLED)
+ "GROUP BY entitytypeid)) AS t GROUP BY entitytypeid ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit();
} else { } else {
return "SELECT playername, UUID, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT playerid, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-entities` " + getWhere(BlockChangeType.ENTITIES_CREATED) + "GROUP BY playerid) UNION (SELECT playerid, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-entities` " + getWhere(BlockChangeType.ENTITIES_KILLED) + "GROUP BY playerid)) AS t INNER JOIN `lb-players` USING (playerid) GROUP BY playerid ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit(); return "SELECT playername, UUID, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT playerid, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-entities` " + getWhere(BlockChangeType.ENTITIES_CREATED) + "GROUP BY playerid) UNION (SELECT playerid, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-entities` " + getWhere(BlockChangeType.ENTITIES_KILLED)
+ "GROUP BY playerid)) AS t INNER JOIN `lb-players` USING (playerid) GROUP BY playerid ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit();
} }
} }
if (sum == SummarizationMode.TYPES) { if (sum == SummarizationMode.TYPES) {
return "SELECT type, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT type, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-blocks` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.CREATED) + "GROUP BY type) UNION (SELECT replaced AS type, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-blocks` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.DESTROYED) + "GROUP BY replaced)) AS t GROUP BY type ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit(); return "SELECT type, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT type, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-blocks` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.CREATED) + "GROUP BY type) UNION (SELECT replaced AS type, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-blocks` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.DESTROYED)
+ "GROUP BY replaced)) AS t GROUP BY type ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit();
} else { } else {
return "SELECT playername, UUID, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT playerid, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-blocks` " + getWhere(BlockChangeType.CREATED) + "GROUP BY playerid) UNION (SELECT playerid, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-blocks` " + getWhere(BlockChangeType.DESTROYED) + "GROUP BY playerid)) AS t INNER JOIN `lb-players` USING (playerid) GROUP BY playerid ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit(); return "SELECT playername, UUID, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT playerid, count(*) AS created, 0 AS destroyed FROM `" + getTable() + "-blocks` " + getWhere(BlockChangeType.CREATED) + "GROUP BY playerid) UNION (SELECT playerid, 0 AS created, count(*) AS destroyed FROM `" + getTable() + "-blocks` " + getWhere(BlockChangeType.DESTROYED)
+ "GROUP BY playerid)) AS t INNER JOIN `lb-players` USING (playerid) GROUP BY playerid ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit();
} }
} }
@@ -581,15 +586,13 @@ public final class QueryParams implements Cloneable {
where, where,
loc.getBlockX(), loc.getBlockX(), loc.getBlockX(), loc.getBlockX(),
loc.getBlockY(), loc.getBlockY(), loc.getBlockY(), loc.getBlockY(),
loc.getBlockZ(), loc.getBlockZ() loc.getBlockZ(), loc.getBlockZ());
);
} else if (radius > 0) { } else if (radius > 0) {
compileLocationQuery( compileLocationQuery(
where, where,
loc.getBlockX() - radius + 1, loc.getBlockX() + radius - 1, loc.getBlockX() - radius + 1, loc.getBlockX() + radius - 1,
loc.getBlockY() - radius + 1, loc.getBlockY() + radius - 1, loc.getBlockY() - radius + 1, loc.getBlockY() + radius - 1,
loc.getBlockZ() - radius + 1, loc.getBlockZ() + radius - 1 loc.getBlockZ() - radius + 1, loc.getBlockZ() + radius - 1);
);
} }
} else if (sel != null) { } else if (sel != null) {
@@ -597,8 +600,7 @@ public final class QueryParams implements Cloneable {
where, where,
sel.getMinimumPoint().getBlockX(), sel.getMaximumPoint().getBlockX(), sel.getMinimumPoint().getBlockX(), sel.getMaximumPoint().getBlockX(),
sel.getMinimumPoint().getBlockY(), sel.getMaximumPoint().getBlockY(), sel.getMinimumPoint().getBlockY(), sel.getMaximumPoint().getBlockY(),
sel.getMinimumPoint().getBlockZ(), sel.getMaximumPoint().getBlockZ() sel.getMinimumPoint().getBlockZ(), sel.getMaximumPoint().getBlockZ());
);
} }
} }
if (!players.isEmpty() && sum != SummarizationMode.PLAYERS && blockChangeType != BlockChangeType.KILLS) { if (!players.isEmpty() && sum != SummarizationMode.PLAYERS && blockChangeType != BlockChangeType.KILLS) {
@@ -1028,14 +1030,26 @@ public final class QueryParams implements Cloneable {
} }
public static enum BlockChangeType { public static enum BlockChangeType {
ALL, BOTH, CHESTACCESS, CREATED, DESTROYED, CHAT, KILLS, ENTITIES, ENTITIES_CREATED, ENTITIES_KILLED, ALL,
BOTH,
CHESTACCESS,
CREATED,
DESTROYED,
CHAT,
KILLS,
ENTITIES,
ENTITIES_CREATED,
ENTITIES_KILLED,
} }
public static enum Order { public static enum Order {
ASC, DESC ASC,
DESC
} }
public static enum SummarizationMode { public static enum SummarizationMode {
NONE, PLAYERS, TYPES NONE,
PLAYERS,
TYPES
} }
} }

View File

@@ -1,5 +1,7 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public enum ToolBehavior { public enum ToolBehavior {
TOOL, BLOCK, NONE TOOL,
BLOCK,
NONE
} }

View File

@@ -1,7 +1,11 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public enum ToolMode { public enum ToolMode {
CLEARLOG("logblock.clearlog"), LOOKUP("logblock.lookup"), REDO("logblock.rollback"), ROLLBACK("logblock.rollback"), WRITELOGFILE("logblock.rollback"); CLEARLOG("logblock.clearlog"),
LOOKUP("logblock.lookup"),
REDO("logblock.rollback"),
ROLLBACK("logblock.rollback"),
WRITELOGFILE("logblock.rollback");
private final String permission; private final String permission;
private ToolMode(String permission) { private ToolMode(String permission) {

View File

@@ -944,6 +944,7 @@ class Updater {
public static class MaterialUpdater1_13 { public static class MaterialUpdater1_13 {
BlockData[][] blockDataMapping; BlockData[][] blockDataMapping;
Material[][] itemMapping = new Material[10][]; Material[][] itemMapping = new Material[10][];
public MaterialUpdater1_13(LogBlock plugin) throws IOException { public MaterialUpdater1_13(LogBlock plugin) throws IOException {
blockDataMapping = new BlockData[256][16]; blockDataMapping = new BlockData[256][16];
try (JarFile file = new JarFile(plugin.getFile())) { try (JarFile file = new JarFile(plugin.getFile())) {

View File

@@ -91,7 +91,6 @@ public class WorldEditor implements Runnable {
return blacklistCollisions; return blacklistCollisions;
} }
public void setSender(CommandSender sender) { public void setSender(CommandSender sender) {
this.sender = sender; this.sender = sender;
} }
@@ -202,7 +201,9 @@ public class WorldEditor implements Runnable {
} }
public static enum PerformResult { public static enum PerformResult {
SUCCESS, BLACKLISTED, NO_ACTION SUCCESS,
BLACKLISTED,
NO_ACTION
} }
public interface Edit { public interface Edit {
@@ -440,6 +441,7 @@ public class WorldEditor implements Runnable {
public static class EditComparator implements Comparator<Edit> { public static class EditComparator implements Comparator<Edit> {
private final int mult; private final int mult;
public EditComparator(QueryParams.Order order) { public EditComparator(QueryParams.Order order) {
mult = order == Order.DESC ? 1 : -1; mult = order == Order.DESC ? 1 : -1;
} }

View File

@@ -58,7 +58,9 @@ public class Config {
public static boolean mb4 = false; public static boolean mb4 = false;
public static enum LogKillsLevel { public static enum LogKillsLevel {
PLAYERS, MONSTERS, ANIMALS; PLAYERS,
MONSTERS,
ANIMALS;
} }
public static void load(LogBlock logblock) throws DataFormatException, IOException { public static void load(LogBlock logblock) throws DataFormatException, IOException {

View File

@@ -65,4 +65,3 @@ public class CreatureInteractLogging extends LoggingListener {
} }
} }
} }

View File

@@ -16,7 +16,6 @@ import org.bukkit.event.entity.EntityDeathEvent;
import static de.diddiz.LogBlock.config.Config.*; import static de.diddiz.LogBlock.config.Config.*;
public class KillLogging extends LoggingListener { public class KillLogging extends LoggingListener {
public KillLogging(LogBlock lb) { public KillLogging(LogBlock lb) {

View File

@@ -16,12 +16,6 @@ public class SnowFormLogging extends LoggingListener {
super(lb); super(lb);
} }
// @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
// public void onLeavesDecay(LeavesDecayEvent event) {
// if (isLogging(event.getBlock().getWorld(), Logging.SNOWFORM))
// consumer.queueBlockBreak("LeavesDecay", event.getBlock().getState());
// }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockForm(BlockFormEvent event) { public void onBlockForm(BlockFormEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.SNOWFORM)) { if (isLogging(event.getBlock().getWorld(), Logging.SNOWFORM)) {

View File

@@ -61,17 +61,14 @@ import java.util.Stack;
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a> * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
* @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a> * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
*/ */
public class ComparableVersion public class ComparableVersion implements Comparable<ComparableVersion> {
implements Comparable<ComparableVersion>
{
private String value; private String value;
private String canonical; private String canonical;
private ListItem items; private ListItem items;
private interface Item private interface Item {
{
int INTEGER_ITEM = 0; int INTEGER_ITEM = 0;
int STRING_ITEM = 1; int STRING_ITEM = 1;
int LIST_ITEM = 2; int LIST_ITEM = 2;
@@ -86,44 +83,35 @@ public class ComparableVersion
/** /**
* Represents a numeric item in the version item list. * Represents a numeric item in the version item list.
*/ */
private static class IntegerItem private static class IntegerItem implements Item {
implements Item
{
private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0"); private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0");
private final BigInteger value; private final BigInteger value;
public static final IntegerItem ZERO = new IntegerItem(); public static final IntegerItem ZERO = new IntegerItem();
private IntegerItem() private IntegerItem() {
{
this.value = BIG_INTEGER_ZERO; this.value = BIG_INTEGER_ZERO;
} }
public IntegerItem( String str ) public IntegerItem(String str) {
{
this.value = new BigInteger(str); this.value = new BigInteger(str);
} }
public int getType() public int getType() {
{
return INTEGER_ITEM; return INTEGER_ITEM;
} }
public boolean isNull() public boolean isNull() {
{
return BIG_INTEGER_ZERO.equals(value); return BIG_INTEGER_ZERO.equals(value);
} }
public int compareTo( Item item ) public int compareTo(Item item) {
{ if (item == null) {
if ( item == null )
{
return BIG_INTEGER_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1 return BIG_INTEGER_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1
} }
switch ( item.getType() ) switch (item.getType()) {
{
case INTEGER_ITEM: case INTEGER_ITEM:
return value.compareTo(((IntegerItem) item).value); return value.compareTo(((IntegerItem) item).value);
@@ -138,8 +126,7 @@ public class ComparableVersion
} }
} }
public String toString() public String toString() {
{
return value.toString(); return value.toString();
} }
} }
@@ -147,16 +134,13 @@ public class ComparableVersion
/** /**
* Represents a string in the version item list, usually a qualifier. * Represents a string in the version item list, usually a qualifier.
*/ */
private static class StringItem private static class StringItem implements Item {
implements Item
{
private static final String[] QUALIFIERS = { "alpha", "beta", "milestone", "rc", "snapshot", "", "sp" }; private static final String[] QUALIFIERS = { "alpha", "beta", "milestone", "rc", "snapshot", "", "sp" };
private static final List<String> _QUALIFIERS = Arrays.asList(QUALIFIERS); private static final List<String> _QUALIFIERS = Arrays.asList(QUALIFIERS);
private static final Properties ALIASES = new Properties(); private static final Properties ALIASES = new Properties();
static static {
{
ALIASES.put("ga", ""); ALIASES.put("ga", "");
ALIASES.put("final", ""); ALIASES.put("final", "");
ALIASES.put("cr", "rc"); ALIASES.put("cr", "rc");
@@ -170,13 +154,10 @@ public class ComparableVersion
private String value; private String value;
public StringItem( String value, boolean followedByDigit ) public StringItem(String value, boolean followedByDigit) {
{ if (followedByDigit && value.length() == 1) {
if ( followedByDigit && value.length() == 1 )
{
// a1 = alpha-1, b1 = beta-1, m1 = milestone-1 // a1 = alpha-1, b1 = beta-1, m1 = milestone-1
switch ( value.charAt( 0 ) ) switch (value.charAt(0)) {
{
case 'a': case 'a':
value = "alpha"; value = "alpha";
break; break;
@@ -191,13 +172,11 @@ public class ComparableVersion
this.value = ALIASES.getProperty(value, value); this.value = ALIASES.getProperty(value, value);
} }
public int getType() public int getType() {
{
return STRING_ITEM; return STRING_ITEM;
} }
public boolean isNull() public boolean isNull() {
{
return (comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX) == 0); return (comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX) == 0);
} }
@@ -213,22 +192,18 @@ public class ComparableVersion
* @param qualifier * @param qualifier
* @return an equivalent value that can be used with lexical comparison * @return an equivalent value that can be used with lexical comparison
*/ */
public static String comparableQualifier( String qualifier ) public static String comparableQualifier(String qualifier) {
{
int i = _QUALIFIERS.indexOf(qualifier); int i = _QUALIFIERS.indexOf(qualifier);
return i == -1 ? (_QUALIFIERS.size() + "-" + qualifier) : String.valueOf(i); return i == -1 ? (_QUALIFIERS.size() + "-" + qualifier) : String.valueOf(i);
} }
public int compareTo( Item item ) public int compareTo(Item item) {
{ if (item == null) {
if ( item == null )
{
// 1-rc < 1, 1-ga > 1 // 1-rc < 1, 1-ga > 1
return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX); return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX);
} }
switch ( item.getType() ) switch (item.getType()) {
{
case INTEGER_ITEM: case INTEGER_ITEM:
return -1; // 1.any < 1.1 ? return -1; // 1.any < 1.1 ?
@@ -243,8 +218,7 @@ public class ComparableVersion
} }
} }
public String toString() public String toString() {
{
return value; return value;
} }
} }
@@ -253,51 +227,37 @@ public class ComparableVersion
* Represents a version list item. This class is used both for the global item list and for sub-lists (which start * Represents a version list item. This class is used both for the global item list and for sub-lists (which start
* with '-(number)' in the version specification). * with '-(number)' in the version specification).
*/ */
private static class ListItem private static class ListItem extends ArrayList<Item> implements Item {
extends ArrayList<Item>
implements Item
{
private static final long serialVersionUID = 5914575811857700009L; private static final long serialVersionUID = 5914575811857700009L;
public int getType() public int getType() {
{
return LIST_ITEM; return LIST_ITEM;
} }
public boolean isNull() public boolean isNull() {
{
return (size() == 0); return (size() == 0);
} }
void normalize() void normalize() {
{ for (ListIterator<Item> iterator = listIterator(size()); iterator.hasPrevious();) {
for ( ListIterator<Item> iterator = listIterator( size() ); iterator.hasPrevious(); )
{
Item item = iterator.previous(); Item item = iterator.previous();
if ( item.isNull() ) if (item.isNull()) {
{
iterator.remove(); // remove null trailing items: 0, "", empty list iterator.remove(); // remove null trailing items: 0, "", empty list
} } else {
else
{
break; break;
} }
} }
} }
public int compareTo( Item item ) public int compareTo(Item item) {
{ if (item == null) {
if ( item == null ) if (size() == 0) {
{
if ( size() == 0 )
{
return 0; // 1-0 = 1- (normalize) = 1 return 0; // 1-0 = 1- (normalize) = 1
} }
Item first = get(0); Item first = get(0);
return first.compareTo(null); return first.compareTo(null);
} }
switch ( item.getType() ) switch (item.getType()) {
{
case INTEGER_ITEM: case INTEGER_ITEM:
return -1; // 1-1 < 1.0.x return -1; // 1-1 < 1.0.x
@@ -308,16 +268,14 @@ public class ComparableVersion
Iterator<Item> left = iterator(); Iterator<Item> left = iterator();
Iterator<Item> right = ((ListItem) item).iterator(); Iterator<Item> right = ((ListItem) item).iterator();
while ( left.hasNext() || right.hasNext() ) while (left.hasNext() || right.hasNext()) {
{
Item l = left.hasNext() ? left.next() : null; Item l = left.hasNext() ? left.next() : null;
Item r = right.hasNext() ? right.next() : null; Item r = right.hasNext() ? right.next() : null;
// if this is shorter, then invert the compare and mul with -1 // if this is shorter, then invert the compare and mul with -1
int result = l == null ? (r == null ? 0 : -1 * r.compareTo(l)) : l.compareTo(r); int result = l == null ? (r == null ? 0 : -1 * r.compareTo(l)) : l.compareTo(r);
if ( result != 0 ) if (result != 0) {
{
return result; return result;
} }
} }
@@ -329,14 +287,11 @@ public class ComparableVersion
} }
} }
public String toString() public String toString() {
{
StringBuilder buffer = new StringBuilder("("); StringBuilder buffer = new StringBuilder("(");
for ( Iterator<Item> iter = iterator(); iter.hasNext(); ) for (Iterator<Item> iter = iterator(); iter.hasNext();) {
{
buffer.append(iter.next()); buffer.append(iter.next());
if ( iter.hasNext() ) if (iter.hasNext()) {
{
buffer.append(','); buffer.append(',');
} }
} }
@@ -345,13 +300,11 @@ public class ComparableVersion
} }
} }
public ComparableVersion( String version ) public ComparableVersion(String version) {
{
parseVersion(version); parseVersion(version);
} }
public final void parseVersion( String version ) public final void parseVersion(String version) {
{
this.value = version; this.value = version;
items = new ListItem(); items = new ListItem();
@@ -367,40 +320,28 @@ public class ComparableVersion
int startIndex = 0; int startIndex = 0;
for ( int i = 0; i < version.length(); i++ ) for (int i = 0; i < version.length(); i++) {
{
char c = version.charAt(i); char c = version.charAt(i);
if ( c == '.' ) if (c == '.') {
{ if (i == startIndex) {
if ( i == startIndex )
{
list.add(IntegerItem.ZERO); list.add(IntegerItem.ZERO);
} } else {
else
{
list.add(parseItem(isDigit, version.substring(startIndex, i))); list.add(parseItem(isDigit, version.substring(startIndex, i)));
} }
startIndex = i + 1; startIndex = i + 1;
} } else if (c == '-') {
else if ( c == '-' ) if (i == startIndex) {
{
if ( i == startIndex )
{
list.add(IntegerItem.ZERO); list.add(IntegerItem.ZERO);
} } else {
else
{
list.add(parseItem(isDigit, version.substring(startIndex, i))); list.add(parseItem(isDigit, version.substring(startIndex, i)));
} }
startIndex = i + 1; startIndex = i + 1;
if ( isDigit ) if (isDigit) {
{
list.normalize(); // 1.0-* = 1-* list.normalize(); // 1.0-* = 1-*
if ( ( i + 1 < version.length() ) && Character.isDigit( version.charAt( i + 1 ) ) ) if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) {
{
// new ListItem only if previous were digits and new char is a digit, // new ListItem only if previous were digits and new char is a digit,
// ie need to differentiate only 1.1 from 1-1 // ie need to differentiate only 1.1 from 1-1
list.add(list = new ListItem()); list.add(list = new ListItem());
@@ -408,21 +349,15 @@ public class ComparableVersion
stack.push(list); stack.push(list);
} }
} }
} } else if (Character.isDigit(c)) {
else if ( Character.isDigit( c ) ) if (!isDigit && i > startIndex) {
{
if ( !isDigit && i > startIndex )
{
list.add(new StringItem(version.substring(startIndex, i), true)); list.add(new StringItem(version.substring(startIndex, i), true));
startIndex = i; startIndex = i;
} }
isDigit = true; isDigit = true;
} } else {
else if (isDigit && i > startIndex) {
{
if ( isDigit && i > startIndex )
{
list.add(parseItem(true, version.substring(startIndex, i))); list.add(parseItem(true, version.substring(startIndex, i)));
startIndex = i; startIndex = i;
} }
@@ -431,13 +366,11 @@ public class ComparableVersion
} }
} }
if ( version.length() > startIndex ) if (version.length() > startIndex) {
{
list.add(parseItem(isDigit, version.substring(startIndex))); list.add(parseItem(isDigit, version.substring(startIndex)));
} }
while ( !stack.isEmpty() ) while (!stack.isEmpty()) {
{
list = (ListItem) stack.pop(); list = (ListItem) stack.pop();
list.normalize(); list.normalize();
} }
@@ -445,39 +378,31 @@ public class ComparableVersion
canonical = items.toString(); canonical = items.toString();
} }
private static Item parseItem( boolean isDigit, String buf ) private static Item parseItem(boolean isDigit, String buf) {
{
return isDigit ? new IntegerItem(buf) : new StringItem(buf, false); return isDigit ? new IntegerItem(buf) : new StringItem(buf, false);
} }
public int compareTo( ComparableVersion o ) public int compareTo(ComparableVersion o) {
{
return items.compareTo(o.items); return items.compareTo(o.items);
} }
public int compareTo( String version ) public int compareTo(String version) {
{
return compareTo(new ComparableVersion(version)); return compareTo(new ComparableVersion(version));
} }
public String toString() public String toString() {
{
return value; return value;
} }
public String toCanonicalString() public String toCanonicalString() {
{
return canonical; return canonical;
} }
public boolean equals( Object o ) public boolean equals(Object o) {
{
return (o instanceof ComparableVersion) && canonical.equals(((ComparableVersion) o).canonical); return (o instanceof ComparableVersion) && canonical.equals(((ComparableVersion) o).canonical);
} }
public int hashCode() public int hashCode() {
{
return canonical.hashCode(); return canonical.hashCode();
} }
} }

View File

@@ -1,6 +1,5 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.util.Utils; import de.diddiz.util.Utils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;