Skip to content

Commit

Permalink
Update to idiomatic java 7. <> and try with resources
Browse files Browse the repository at this point in the history
  • Loading branch information
LunNova committed Feb 7, 2016
1 parent cb3a9e3 commit f938703
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 248 deletions.
20 changes: 10 additions & 10 deletions src/main/java/nallar/tickprofiler/minecraft/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
import java.util.*;

public abstract class Command extends CommandBase {
protected boolean requireOp() {
return false;
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender commandSender) {
return !requireOp() || super.canCommandSenderUseCommand(commandSender);
}

public static void sendChat(ICommandSender commandSender, String message) {
if (commandSender == MinecraftServer.getServer()) {
Log.info('\n' + message);
Expand All @@ -37,9 +28,18 @@ public static void sendChat(ICommandSender commandSender, String message) {
}
}

protected boolean requireOp() {
return false;
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender commandSender) {
return !requireOp() || super.canCommandSenderUseCommand(commandSender);
}

@Override
public final void processCommand(ICommandSender commandSender, String... argumentsArray) {
processCommand(commandSender, new ArrayList<String>(Arrays.asList(argumentsArray)));
processCommand(commandSender, new ArrayList<>(Arrays.asList(argumentsArray)));
}

protected abstract void processCommand(ICommandSender commandSender, List<String> arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,43 @@
import java.util.concurrent.*;

public class TPSCommand extends Command {
private static final int tpsWidth = 40;
public static String name = "tps";

private static String getTPSString(boolean withColour) {
double targetTPS = 20;
double tps = TimeUnit.SECONDS.toNanos(1) / (double) TickProfiler.tickTime;
if (tps > 20) {
tps = 20;
}
double difference = Math.abs(targetTPS - tps);
int charsFirst = (int) Math.round((tps / targetTPS) * tpsWidth);
int charsAfter = tpsWidth - charsFirst;
return " " +
TableFormatter.formatDoubleWithPrecision(tps, 2) +
" TPS [ " +
(withColour ? getColourForDifference(difference, targetTPS) : "") +
Strings.repeat("#", charsFirst) +
Strings.repeat("~", charsAfter) +
(withColour ? ChatFormat.RESET : "") +
" ] ";
}

private static String getColourForDifference(double difference, double targetTPS) {
switch ((int) (difference / (targetTPS / 4))) {
case 0:
return ChatFormat.GREEN.toString();
case 1:
return ChatFormat.YELLOW.toString();
case 2:
return ChatFormat.RED.toString();
case 3:
return ChatFormat.RED.toString() + ChatFormat.BOLD;
default:
return ChatFormat.MAGIC.toString();
}
}

@Override
public String getCommandName() {
return name;
Expand Down Expand Up @@ -63,43 +98,4 @@ public void processCommand(ICommandSender commandSender, List<String> arguments)
tf.sb.append('\n').append(getTPSString(commandSender instanceof EntityPlayer));
sendChat(commandSender, tf.toString());
}

private static final int tpsWidth = 40;

private static String getTPSString(boolean withColour) {
double targetTPS = 20;
double tps = TimeUnit.SECONDS.toNanos(1) / (double) TickProfiler.tickTime;
if (tps > 20) {
tps = 20;
}
double difference = Math.abs(targetTPS - tps);
int charsFirst = (int) Math.round((tps / targetTPS) * tpsWidth);
int charsAfter = tpsWidth - charsFirst;
StringBuilder sb = new StringBuilder();
sb
.append(' ')
.append(TableFormatter.formatDoubleWithPrecision(tps, 2))
.append(" TPS [ ")
.append(withColour ? getColourForDifference(difference, targetTPS) : "")
.append(Strings.repeat("#", charsFirst))
.append(Strings.repeat("~", charsAfter))
.append(withColour ? ChatFormat.RESET : "")
.append(" ] ");
return sb.toString();
}

private static String getColourForDifference(double difference, double targetTPS) {
switch ((int) (difference / (targetTPS / 4))) {
case 0:
return ChatFormat.GREEN.toString();
case 1:
return ChatFormat.YELLOW.toString();
case 2:
return ChatFormat.RED.toString();
case 3:
return ChatFormat.RED.toString() + ChatFormat.BOLD;
default:
return ChatFormat.MAGIC.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@
import java.util.concurrent.atomic.*;

public class EntityTickProfiler {
public static final EntityTickProfiler INSTANCE = new EntityTickProfiler();
private static final Method isActiveChunk = getIsActiveChunkMethod();
public static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private final HashMap<Class<?>, AtomicInteger> invocationCount = new HashMap<>();
private final HashMap<Class<?>, AtomicLong> time = new HashMap<>();
private final HashMap<Object, AtomicLong> singleTime = new HashMap<>();
private final HashMap<Object, AtomicLong> singleInvocationCount = new HashMap<>();
private final AtomicLong totalTime = new AtomicLong();
private int lastCX = Integer.MIN_VALUE;
private int lastCZ = Integer.MIN_VALUE;
private boolean cachedActive = false;
private int ticks;
private volatile int chunkX;
private volatile int chunkZ;
private volatile long startTime;
private EntityTickProfiler() {
}

private static Method getIsActiveChunkMethod() {
Class<World> clazz = World.class;
Expand All @@ -35,24 +51,6 @@ private static Method getIsActiveChunkMethod() {
return null;
}

private int lastCX = Integer.MIN_VALUE;
private int lastCZ = Integer.MIN_VALUE;
private boolean cachedActive = false;
public static final EntityTickProfiler INSTANCE = new EntityTickProfiler();
public static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private final HashMap<Class<?>, AtomicInteger> invocationCount = new HashMap<Class<?>, AtomicInteger>();
private final HashMap<Class<?>, AtomicLong> time = new HashMap<Class<?>, AtomicLong>();
private final HashMap<Object, AtomicLong> singleTime = new HashMap<Object, AtomicLong>();
private final HashMap<Object, AtomicLong> singleInvocationCount = new HashMap<Object, AtomicLong>();
private int ticks;
private final AtomicLong totalTime = new AtomicLong();
private volatile int chunkX;
private volatile int chunkZ;
private volatile long startTime;

private EntityTickProfiler() {
}

public static synchronized boolean startProfiling(ProfileCommand.ProfilingState profilingState_) {
if (profilingState != ProfileCommand.ProfilingState.NONE) {
return false;
Expand All @@ -65,6 +63,45 @@ public static synchronized void endProfiling() {
profilingState = ProfileCommand.ProfilingState.NONE;
}

private static <T> List<T> sortedKeys(Map<T, ? extends Comparable<?>> map, int elements) {
List<T> list = Ordering.natural().reverse().onResultOf(Functions.forMap(map)).immutableSortedCopy(map.keySet());
return list.size() > elements ? list.subList(0, elements) : list;
}

private static int getDimension(TileEntity o) {
if (o.getWorldObj() == null) return -999;
WorldProvider worldProvider = o.getWorldObj().provider;
return worldProvider == null ? -999 : worldProvider.dimensionId;
}

private static int getDimension(Entity o) {
if (o.worldObj == null) return -999;
WorldProvider worldProvider = o.worldObj.provider;
return worldProvider == null ? -999 : worldProvider.dimensionId;
}

private static Object niceName(Object o) {
if (o instanceof TileEntity) {
return niceName(o.getClass()) + ' ' + ((TileEntity) o).xCoord + ',' + ((TileEntity) o).yCoord + ',' + ((TileEntity) o).zCoord + ':' + getDimension((TileEntity) o);
} else if (o instanceof Entity) {
return niceName(o.getClass()) + ' ' + (int) ((Entity) o).posX + ',' + (int) ((Entity) o).posY + ',' + (int) ((Entity) o).posZ + ':' + getDimension((Entity) o);
}
return o.toString().substring(0, 48);
}

private static String niceName(Class<?> clazz) {
String name = clazz.getName();
if (name.contains(".")) {
String cName = name.substring(name.lastIndexOf('.') + 1);
String pName = name.substring(0, name.lastIndexOf('.'));
if (pName.contains(".")) {
pName = pName.substring(pName.lastIndexOf('.') + 1);
}
return (cName.length() < 15 ? pName + '.' : "") + cName;
}
return name;
}

public void setLocation(final int x, final int z) {
chunkX = x;
chunkZ = z;
Expand All @@ -74,7 +111,7 @@ public boolean startProfiling(final Runnable runnable, ProfileCommand.ProfilingS
if (time <= 0) {
throw new IllegalArgumentException("time must be > 0");
}
final Collection<World> worlds = new ArrayList<World>(worlds_);
final Collection<World> worlds = new ArrayList<>(worlds_);
synchronized (EntityTickProfiler.class) {
if (!startProfiling(state)) {
return false;
Expand Down Expand Up @@ -168,6 +205,7 @@ public synchronized void tick() {
}
}

@SuppressWarnings("unchecked")
public void writeJSONData(File file) throws IOException {
TableFormatter tf = new TableFormatter(StringFiller.FIXED_WIDTH);
tf.recordTables();
Expand All @@ -182,11 +220,6 @@ public void writeJSONData(File file) throws IOException {
objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, tables);
}

private static <T> List<T> sortedKeys(Map<T, ? extends Comparable<?>> map, int elements) {
List<T> list = Ordering.natural().reverse().onResultOf(Functions.forMap(map)).immutableSortedCopy(map.keySet());
return list.size() > elements ? list.subList(0, elements) : list;
}

public TableFormatter writeStringData(TableFormatter tf) {
return writeStringData(tf, 5);
}
Expand All @@ -199,13 +232,13 @@ public TableFormatter writeStringData(TableFormatter tf, int elements) {
}

public TableFormatter writeData(TableFormatter tf, int elements) {
Map<Class<?>, Long> time = new HashMap<Class<?>, Long>();
Map<Class<?>, Long> time = new HashMap<>();
synchronized (this.time) {
for (Map.Entry<Class<?>, AtomicLong> entry : this.time.entrySet()) {
time.put(entry.getKey(), entry.getValue().get());
}
}
Map<Object, Long> singleTime = new HashMap<Object, Long>();
Map<Object, Long> singleTime = new HashMap<>();
synchronized (this.singleTime) {
for (Map.Entry<Object, AtomicLong> entry : this.singleTime.entrySet()) {
singleTime.put(entry.getKey(), entry.getValue().get());
Expand Down Expand Up @@ -282,7 +315,7 @@ public ComparableLongHolder get(Object key_) {
}
tf.finishTable();
tf.sb.append('\n');
Map<Class<?>, Long> timePerTick = new HashMap<Class<?>, Long>();
Map<Class<?>, Long> timePerTick = new HashMap<>();
for (Map.Entry<Class<?>, AtomicLong> entry : this.time.entrySet()) {
timePerTick.put(entry.getKey(), entry.getValue().get() / invocationCount.get(entry.getKey()).get());
}
Expand All @@ -300,40 +333,6 @@ public ComparableLongHolder get(Object key_) {
return tf;
}

private static int getDimension(TileEntity o) {
if (o.getWorldObj() == null) return -999;
WorldProvider worldProvider = o.getWorldObj().provider;
return worldProvider == null ? -999 : worldProvider.dimensionId;
}

private static int getDimension(Entity o) {
if (o.worldObj == null) return -999;
WorldProvider worldProvider = o.worldObj.provider;
return worldProvider == null ? -999 : worldProvider.dimensionId;
}

private static Object niceName(Object o) {
if (o instanceof TileEntity) {
return niceName(o.getClass()) + ' ' + ((TileEntity) o).xCoord + ',' + ((TileEntity) o).yCoord + ',' + ((TileEntity) o).zCoord + ':' + getDimension((TileEntity) o);
} else if (o instanceof Entity) {
return niceName(o.getClass()) + ' ' + (int) ((Entity) o).posX + ',' + (int) ((Entity) o).posY + ',' + (int) ((Entity) o).posZ + ':' + getDimension((Entity) o);
}
return o.toString().substring(0, 48);
}

private static String niceName(Class<?> clazz) {
String name = clazz.getName();
if (name.contains(".")) {
String cName = name.substring(name.lastIndexOf('.') + 1);
String pName = name.substring(0, name.lastIndexOf('.'));
if (pName.contains(".")) {
pName = pName.substring(pName.lastIndexOf('.') + 1);
}
return (cName.length() < 15 ? pName + '.' : "") + cName;
}
return name;
}

private AtomicLong getSingleInvocationCount(Object o) {
AtomicLong t = singleInvocationCount.get(o);
if (t == null) {
Expand Down Expand Up @@ -366,6 +365,7 @@ private AtomicLong getTime(Class<?> clazz) {
return this.getTime(clazz, time);
}

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private <T> AtomicLong getTime(T clazz, HashMap<T, AtomicLong> time) {
AtomicLong t = time.get(clazz);
if (t == null) {
Expand Down Expand Up @@ -400,19 +400,6 @@ private boolean isActiveChunk(World world, int chunkX, int chunkZ) {
}
}

private class ComparableLongHolder implements Comparable<ComparableLongHolder> {
public long value;

ComparableLongHolder() {
}

@Override
public int compareTo(final ComparableLongHolder comparableLongHolder) {
long otherValue = comparableLongHolder.value;
return (value < otherValue) ? -1 : ((value == otherValue) ? 0 : 1);
}
}

private static final class ChunkCoords {
public final int chunkXPos;
public final int chunkZPos;
Expand All @@ -434,4 +421,17 @@ public int hashCode() {
return (chunkXPos << 16) ^ (chunkZPos << 4) ^ dimension;
}
}

private class ComparableLongHolder implements Comparable<ComparableLongHolder> {
public long value;

ComparableLongHolder() {
}

@Override
public int compareTo(final ComparableLongHolder comparableLongHolder) {
long otherValue = comparableLongHolder.value;
return (value < otherValue) ? -1 : ((value == otherValue) ? 0 : 1);
}
}
}

0 comments on commit f938703

Please sign in to comment.