From e4e7aa7eb35c9c9bc8a4bd49b7635f27b2beeed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 9 Apr 2014 00:41:33 +0200 Subject: [PATCH] Converted to Java 7, factored InstanceLock to own file. --- .classpath | 2 +- .settings/org.eclipse.jdt.core.prefs | 11 +++ .../gamecore/audio/JointVolume.java | 5 +- src/mightypork/gamecore/audio/SoundBank.java | 4 +- .../gamecore/audio/SoundSystem.java | 12 ++-- src/mightypork/gamecore/audio/Volume.java | 21 ++++++ .../audio/players/BaseAudioPlayer.java | 10 +-- .../gamecore/audio/players/EffectPlayer.java | 4 +- .../gamecore/audio/players/LoopPlayer.java | 4 +- src/mightypork/gamecore/control/GameLoop.java | 4 +- .../gamecore/control/bus/BufferedHashSet.java | 4 +- .../gamecore/control/bus/BusAccess.java | 2 - .../gamecore/control/bus/EventBus.java | 10 +-- .../gamecore/control/bus/EventChannel.java | 4 +- .../gamecore/control/bus/clients/BusNode.java | 4 +- .../control/bus/clients/RootBusNode.java | 5 +- .../gamecore/control/interf/NoImpl.java | 2 +- .../gamecore/control/timing/FpsMeter.java | 2 +- .../gamecore/gui/renderers/ElementHolder.java | 2 +- .../gamecore/gui/renderers/TextPainter.java | 8 +-- .../gamecore/gui/screens/LayeredScreen.java | 2 +- .../gamecore/gui/screens/ScreenLayer.java | 12 ++-- .../gamecore/gui/screens/ScreenRegistry.java | 2 +- .../gamecore/input/InputSystem.java | 2 + .../gamecore/input/KeyBindingPool.java | 2 +- src/mightypork/gamecore/input/KeyStroke.java | 2 +- .../gamecore/loading/AsyncResourceLoader.java | 2 +- .../gamecore/render/DisplaySystem.java | 3 +- src/mightypork/gamecore/render/Render.java | 7 +- .../gamecore/render/Screenshot.java | 8 +-- .../gamecore/render/fonts/DeferredFont.java | 16 ++--- .../gamecore/render/fonts/FontBank.java | 7 +- .../gamecore/render/fonts/FontRenderer.java | 15 ++-- .../gamecore/render/fonts/GLFont.java | 4 +- .../gamecore/render/fonts/NullFont.java | 4 +- .../gamecore/render/fonts/SlickFont.java | 9 ++- .../gamecore/render/textures/FilterMode.java | 4 +- .../gamecore/render/textures/TextureBank.java | 4 +- .../gamecore/render/textures/WrapMode.java | 4 +- src/mightypork/rogue/App.java | 45 ++---------- src/mightypork/rogue/Paths.java | 3 +- src/mightypork/rogue/screens/LayerFps.java | 4 +- .../test_bouncyboxes/LayerBouncyBoxes.java | 6 +- .../test_bouncyboxes/ScreenTestBouncy.java | 7 +- .../test_cat_sound/LayerFlyingCat.java | 2 +- .../screens/test_cat_sound/ScreenTestCat.java | 4 +- .../screens/test_font/ScreenTestFont.java | 4 +- .../utils/config/PropertyManager.java | 30 ++++---- src/mightypork/utils/config/SimpleConfig.java | 6 +- .../utils/config/SortedProperties.java | 4 +- src/mightypork/utils/files/FileTreeDiff.java | 64 +++++------------ src/mightypork/utils/files/FileUtils.java | 69 +++++-------------- src/mightypork/utils/files/InstanceLock.java | 51 ++++++++++++++ src/mightypork/utils/files/ZipBuilder.java | 12 ++-- src/mightypork/utils/files/ZipUtils.java | 68 +++--------------- src/mightypork/utils/files/ion/Ion.java | 29 ++------ src/mightypork/utils/logging/Log.java | 2 +- src/mightypork/utils/logging/LogInstance.java | 2 +- src/mightypork/utils/math/Calc.java | 2 +- .../utils/math/animation/AnimDouble.java | 4 +- .../math/constraints/ConstraintFactory.java | 29 +++++--- src/mightypork/utils/math/coord/Coord.java | 2 +- src/mightypork/utils/objects/Convert.java | 16 ++--- src/mightypork/utils/objects/MapSort.java | 8 +-- src/mightypork/utils/objects/ObjectUtils.java | 2 +- .../utils/objects/VarargsParser.java | 2 +- .../utils/string/StringProvider.java | 3 +- 67 files changed, 317 insertions(+), 392 deletions(-) create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/mightypork/gamecore/audio/Volume.java create mode 100644 src/mightypork/utils/files/InstanceLock.java diff --git a/.classpath b/.classpath index a3b32fd..45f6ff8 100644 --- a/.classpath +++ b/.classpath @@ -1,7 +1,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7341ab1 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/src/mightypork/gamecore/audio/JointVolume.java b/src/mightypork/gamecore/audio/JointVolume.java index df8a344..9ea90b4 100644 --- a/src/mightypork/gamecore/audio/JointVolume.java +++ b/src/mightypork/gamecore/audio/JointVolume.java @@ -10,7 +10,7 @@ import mightypork.utils.objects.Mutable; * * @author MightyPork */ -public class JointVolume extends Mutable { +public class JointVolume extends Volume { private final Mutable[] volumes; @@ -20,7 +20,8 @@ public class JointVolume extends Mutable { * * @param volumes individual volumes to join */ - public JointVolume(Mutable... volumes) { + @SafeVarargs + public JointVolume(Volume... volumes) { super(1D); this.volumes = volumes; } diff --git a/src/mightypork/gamecore/audio/SoundBank.java b/src/mightypork/gamecore/audio/SoundBank.java index fa3b683..bfe6111 100644 --- a/src/mightypork/gamecore/audio/SoundBank.java +++ b/src/mightypork/gamecore/audio/SoundBank.java @@ -17,8 +17,8 @@ public class SoundBank extends AppAdapter { private static final LoopPlayer NULL_LOOP = new LoopPlayer(NO_SOUND, 0, 0, null); private static final EffectPlayer NULL_EFFECT = new EffectPlayer(NO_SOUND, 0, 0, null); - private final Map effects = new HashMap(); - private final Map loops = new HashMap(); + private final Map effects = new HashMap<>(); + private final Map loops = new HashMap<>(); public SoundBank(AppAccess app) { diff --git a/src/mightypork/gamecore/audio/SoundSystem.java b/src/mightypork/gamecore/audio/SoundSystem.java index 53feae9..d374999 100644 --- a/src/mightypork/gamecore/audio/SoundSystem.java +++ b/src/mightypork/gamecore/audio/SoundSystem.java @@ -13,7 +13,6 @@ import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.interf.Updateable; import mightypork.utils.math.Calc.Buffers; import mightypork.utils.math.coord.Coord; -import mightypork.utils.objects.Mutable; import org.lwjgl.openal.AL; import org.lwjgl.openal.AL10; @@ -25,7 +24,6 @@ import org.newdawn.slick.openal.SoundStore; * * @author MightyPork */ -@SuppressWarnings("unchecked") public class SoundSystem extends RootBusNode implements Updateable { private static final Coord INITIAL_LISTENER_POS = new Coord(0, 0, 0); @@ -72,12 +70,12 @@ public class SoundSystem extends RootBusNode implements Updateable { // -- instance -- - public final Mutable masterVolume = new Mutable(1D); - public final Mutable effectsVolume = new JointVolume(masterVolume); - public final Mutable loopsVolume = new JointVolume(masterVolume); + public final Volume masterVolume = new Volume(1D); + public final Volume effectsVolume = new JointVolume(masterVolume); + public final Volume loopsVolume = new JointVolume(masterVolume); - private final Set loopPlayers = new HashSet(); - private final Set resources = new HashSet(); + private final Set loopPlayers = new HashSet<>(); + private final Set resources = new HashSet<>(); public SoundSystem(AppAccess app) { diff --git a/src/mightypork/gamecore/audio/Volume.java b/src/mightypork/gamecore/audio/Volume.java new file mode 100644 index 0000000..ef9e775 --- /dev/null +++ b/src/mightypork/gamecore/audio/Volume.java @@ -0,0 +1,21 @@ +package mightypork.gamecore.audio; + + +import mightypork.utils.math.Calc; +import mightypork.utils.objects.Mutable; + + +public class Volume extends Mutable { + + public Volume(Double d) { + super(d); + } + + + @Override + public void set(Double d) + { + super.set(Calc.clampd(d, 0, 1)); + } + +} diff --git a/src/mightypork/gamecore/audio/players/BaseAudioPlayer.java b/src/mightypork/gamecore/audio/players/BaseAudioPlayer.java index 25af30c..f95595c 100644 --- a/src/mightypork/gamecore/audio/players/BaseAudioPlayer.java +++ b/src/mightypork/gamecore/audio/players/BaseAudioPlayer.java @@ -2,8 +2,8 @@ package mightypork.gamecore.audio.players; import mightypork.gamecore.audio.DeferredAudio; +import mightypork.gamecore.audio.Volume; import mightypork.gamecore.control.interf.Destroyable; -import mightypork.utils.objects.Mutable; public abstract class BaseAudioPlayer implements Destroyable { @@ -18,21 +18,21 @@ public abstract class BaseAudioPlayer implements Destroyable { private final double basePitch; /** dedicated volume control */ - private final Mutable gainMultiplier; + private final Volume gainMultiplier; - public BaseAudioPlayer(DeferredAudio track, double baseGain, Mutable gainMultiplier) { + public BaseAudioPlayer(DeferredAudio track, double baseGain, Volume gainMultiplier) { this(track, 1, baseGain, gainMultiplier); } - public BaseAudioPlayer(DeferredAudio track, double basePitch, double baseGain, Mutable gainMultiplier) { + public BaseAudioPlayer(DeferredAudio track, double basePitch, double baseGain, Volume gainMultiplier) { this.audio = track; this.baseGain = baseGain; this.basePitch = basePitch; - if (gainMultiplier == null) gainMultiplier = new Mutable(1D); + if (gainMultiplier == null) gainMultiplier = new Volume(1D); this.gainMultiplier = gainMultiplier; } diff --git a/src/mightypork/gamecore/audio/players/EffectPlayer.java b/src/mightypork/gamecore/audio/players/EffectPlayer.java index 976d24b..2017c48 100644 --- a/src/mightypork/gamecore/audio/players/EffectPlayer.java +++ b/src/mightypork/gamecore/audio/players/EffectPlayer.java @@ -2,13 +2,13 @@ package mightypork.gamecore.audio.players; import mightypork.gamecore.audio.DeferredAudio; +import mightypork.gamecore.audio.Volume; import mightypork.utils.math.coord.Coord; -import mightypork.utils.objects.Mutable; public class EffectPlayer extends BaseAudioPlayer { - public EffectPlayer(DeferredAudio track, double basePitch, double baseGain, Mutable gainMultiplier) { + public EffectPlayer(DeferredAudio track, double basePitch, double baseGain, Volume gainMultiplier) { super(track, (float) basePitch, (float) baseGain, gainMultiplier); } diff --git a/src/mightypork/gamecore/audio/players/LoopPlayer.java b/src/mightypork/gamecore/audio/players/LoopPlayer.java index 9144f98..2713a2a 100644 --- a/src/mightypork/gamecore/audio/players/LoopPlayer.java +++ b/src/mightypork/gamecore/audio/players/LoopPlayer.java @@ -2,10 +2,10 @@ package mightypork.gamecore.audio.players; import mightypork.gamecore.audio.DeferredAudio; +import mightypork.gamecore.audio.Volume; import mightypork.gamecore.control.interf.Updateable; import mightypork.gamecore.control.timing.Pauseable; import mightypork.utils.math.animation.AnimDouble; -import mightypork.utils.objects.Mutable; import org.lwjgl.openal.AL10; @@ -29,7 +29,7 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable private double outTime = 1; - public LoopPlayer(DeferredAudio track, double pitch, double baseGain, Mutable gainMultiplier) { + public LoopPlayer(DeferredAudio track, double pitch, double baseGain, Volume gainMultiplier) { super(track, (float) pitch, (float) baseGain, gainMultiplier); paused = true; diff --git a/src/mightypork/gamecore/control/GameLoop.java b/src/mightypork/gamecore/control/GameLoop.java index b7cea00..c625b6c 100644 --- a/src/mightypork/gamecore/control/GameLoop.java +++ b/src/mightypork/gamecore/control/GameLoop.java @@ -19,9 +19,9 @@ import mightypork.gamecore.gui.screens.ScreenRegistry; */ public abstract class GameLoop extends AppModule implements MainLoopTaskRequest.Listener { - private final Queue taskQueue = new ConcurrentLinkedQueue(); + private final Queue taskQueue = new ConcurrentLinkedQueue<>(); private TimerDelta timer; - private Renderable mainRenderable; + private final Renderable mainRenderable; private boolean running = true; diff --git a/src/mightypork/gamecore/control/bus/BufferedHashSet.java b/src/mightypork/gamecore/control/bus/BufferedHashSet.java index 3399c9c..e0c14b5 100644 --- a/src/mightypork/gamecore/control/bus/BufferedHashSet.java +++ b/src/mightypork/gamecore/control/bus/BufferedHashSet.java @@ -16,8 +16,8 @@ import java.util.List; */ public class BufferedHashSet extends HashSet { - private final List toAdd = new LinkedList(); - private final List toRemove = new LinkedList(); + private final List toAdd = new LinkedList<>(); + private final List toRemove = new LinkedList<>(); private boolean buffering = false; diff --git a/src/mightypork/gamecore/control/bus/BusAccess.java b/src/mightypork/gamecore/control/bus/BusAccess.java index 7d7e927..7b98a82 100644 --- a/src/mightypork/gamecore/control/bus/BusAccess.java +++ b/src/mightypork/gamecore/control/bus/BusAccess.java @@ -1,8 +1,6 @@ package mightypork.gamecore.control.bus; - - /** * Access to an {@link EventBus} instance * diff --git a/src/mightypork/gamecore/control/bus/EventBus.java b/src/mightypork/gamecore/control/bus/EventBus.java index d010016..623ae58 100644 --- a/src/mightypork/gamecore/control/bus/EventBus.java +++ b/src/mightypork/gamecore/control/bus/EventBus.java @@ -22,13 +22,13 @@ import mightypork.utils.logging.Log; final public class EventBus implements Destroyable { /** Message channels */ - private final BufferedHashSet> channels = new BufferedHashSet>(); + private final BufferedHashSet> channels = new BufferedHashSet<>(); /** Registered clients */ - private final BufferedHashSet clients = new BufferedHashSet(); + private final BufferedHashSet clients = new BufferedHashSet<>(); /** Messages queued for delivery */ - private final DelayQueue sendQueue = new DelayQueue(); + private final DelayQueue sendQueue = new DelayQueue<>(); /** Queue polling thread */ private final QueuePollingThread busThread; @@ -235,7 +235,7 @@ final public class EventBus implements Destroyable { clients.add(client); - if(detailedLogging) Log.f3(" Client joined: "+Log.str(client)); + if (detailedLogging) Log.f3(" Client joined: " + Log.str(client)); } @@ -250,7 +250,7 @@ final public class EventBus implements Destroyable { clients.remove(client); - if(detailedLogging) Log.f3(" Client left: "+Log.str(client)); + if (detailedLogging) Log.f3(" Client left: " + Log.str(client)); } diff --git a/src/mightypork/gamecore/control/bus/EventChannel.java b/src/mightypork/gamecore/control/bus/EventChannel.java index 7de1186..6fa3ee7 100644 --- a/src/mightypork/gamecore/control/bus/EventChannel.java +++ b/src/mightypork/gamecore/control/bus/EventChannel.java @@ -53,7 +53,7 @@ final public class EventChannel, CLIENT> { { if (!canBroadcast(event)) return false; - return doBroadcast(eventClass.cast(event), clients, new HashSet()); + return doBroadcast(eventClass.cast(event), clients, new HashSet<>()); } @@ -151,7 +151,7 @@ final public class EventChannel, CLIENT> { */ public static , F_CLIENT> EventChannel create(Class eventClass, Class clientClass) { - return new EventChannel(eventClass, clientClass); + return new EventChannel<>(eventClass, clientClass); } diff --git a/src/mightypork/gamecore/control/bus/clients/BusNode.java b/src/mightypork/gamecore/control/bus/clients/BusNode.java index 509d190..16e1502 100644 --- a/src/mightypork/gamecore/control/bus/clients/BusNode.java +++ b/src/mightypork/gamecore/control/bus/clients/BusNode.java @@ -19,7 +19,7 @@ public abstract class BusNode implements BusAccess, DelegatingClient, Toggleable private final BusAccess busAccess; - private final Set clients = new LinkedHashSet(); + private final Set clients = new LinkedHashSet<>(); private boolean listening = true; private boolean delegating = true; @@ -57,7 +57,7 @@ public abstract class BusNode implements BusAccess, DelegatingClient, Toggleable */ public final void addChildClient(Object client) { - if(client instanceof RootBusNode) { + if (client instanceof RootBusNode) { throw new IllegalArgumentException("Cannot nest RootBusNode."); } diff --git a/src/mightypork/gamecore/control/bus/clients/RootBusNode.java b/src/mightypork/gamecore/control/bus/clients/RootBusNode.java index fa3d9de..1f4b12b 100644 --- a/src/mightypork/gamecore/control/bus/clients/RootBusNode.java +++ b/src/mightypork/gamecore/control/bus/clients/RootBusNode.java @@ -1,5 +1,6 @@ package mightypork.gamecore.control.bus.clients; + import mightypork.gamecore.control.bus.BusAccess; import mightypork.gamecore.control.interf.Destroyable; @@ -10,10 +11,10 @@ import mightypork.gamecore.control.interf.Destroyable; * @author MightyPork */ public abstract class RootBusNode extends BusNode implements Destroyable { - + public RootBusNode(BusAccess busAccess) { super(busAccess); - + getEventBus().subscribe(this); } diff --git a/src/mightypork/gamecore/control/interf/NoImpl.java b/src/mightypork/gamecore/control/interf/NoImpl.java index 6e38c69..0b6850d 100644 --- a/src/mightypork/gamecore/control/interf/NoImpl.java +++ b/src/mightypork/gamecore/control/interf/NoImpl.java @@ -10,7 +10,7 @@ import java.lang.annotation.Target; @Documented @Retention(RetentionPolicy.SOURCE) -@Target(value={ElementType.METHOD}) +@Target(value = { ElementType.METHOD }) public @interface NoImpl { // } diff --git a/src/mightypork/gamecore/control/timing/FpsMeter.java b/src/mightypork/gamecore/control/timing/FpsMeter.java index 5376b3c..eae4e5b 100644 --- a/src/mightypork/gamecore/control/timing/FpsMeter.java +++ b/src/mightypork/gamecore/control/timing/FpsMeter.java @@ -31,7 +31,7 @@ public class FpsMeter { if (System.currentTimeMillis() - lastTimeMillis > 1000) { lastSecFPS = frames; frames = 0; - long over = System.currentTimeMillis() - lastTimeMillis - 1000; + final long over = System.currentTimeMillis() - lastTimeMillis - 1000; lastTimeMillis = System.currentTimeMillis() - over; } frames++; diff --git a/src/mightypork/gamecore/gui/renderers/ElementHolder.java b/src/mightypork/gamecore/gui/renderers/ElementHolder.java index 3420710..3ad5dee 100644 --- a/src/mightypork/gamecore/gui/renderers/ElementHolder.java +++ b/src/mightypork/gamecore/gui/renderers/ElementHolder.java @@ -18,7 +18,7 @@ import mightypork.utils.math.coord.Rect; */ public abstract class ElementHolder extends BusNode implements PluggableRenderable { - private final LinkedList elements = new LinkedList(); + private final LinkedList elements = new LinkedList<>(); private RectConstraint context; diff --git a/src/mightypork/gamecore/gui/renderers/TextPainter.java b/src/mightypork/gamecore/gui/renderers/TextPainter.java index cb3a899..327d7b7 100644 --- a/src/mightypork/gamecore/gui/renderers/TextPainter.java +++ b/src/mightypork/gamecore/gui/renderers/TextPainter.java @@ -2,15 +2,13 @@ package mightypork.gamecore.gui.renderers; import mightypork.gamecore.render.fonts.FontRenderer; -import mightypork.gamecore.render.fonts.GLFont; import mightypork.gamecore.render.fonts.FontRenderer.Align; +import mightypork.gamecore.render.fonts.GLFont; import mightypork.utils.math.color.RGB; import mightypork.utils.string.StringProvider; import mightypork.utils.string.StringProvider.StringWrapper; - - public class TextPainter extends PluggableRenderer { private final FontRenderer font; @@ -41,6 +39,7 @@ public class TextPainter extends PluggableRenderer { this(font, align, color, (StringProvider) null); } + /** * Use size specified during font init instead of size provided by * {@link GLFont} instance (measured from tile heights.
@@ -49,7 +48,8 @@ public class TextPainter extends PluggableRenderer { * * @param enable use it */ - public void usePtSize(boolean enable) { + public void usePtSize(boolean enable) + { font.usePtSize(enable); } diff --git a/src/mightypork/gamecore/gui/screens/LayeredScreen.java b/src/mightypork/gamecore/gui/screens/LayeredScreen.java index 415fbbf..d51713a 100644 --- a/src/mightypork/gamecore/gui/screens/LayeredScreen.java +++ b/src/mightypork/gamecore/gui/screens/LayeredScreen.java @@ -10,7 +10,7 @@ import mightypork.gamecore.render.Render; public abstract class LayeredScreen extends Screen { - private final Collection layers = new TreeSet(); + private final Collection layers = new TreeSet<>(); public LayeredScreen(AppAccess app) { diff --git a/src/mightypork/gamecore/gui/screens/ScreenLayer.java b/src/mightypork/gamecore/gui/screens/ScreenLayer.java index d3fb9ef..cffc919 100644 --- a/src/mightypork/gamecore/gui/screens/ScreenLayer.java +++ b/src/mightypork/gamecore/gui/screens/ScreenLayer.java @@ -56,15 +56,17 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable screens = new HashMap(); + private final HashMap screens = new HashMap<>(); private Screen active = null; diff --git a/src/mightypork/gamecore/input/InputSystem.java b/src/mightypork/gamecore/input/InputSystem.java index 2c12920..adc8b2d 100644 --- a/src/mightypork/gamecore/input/InputSystem.java +++ b/src/mightypork/gamecore/input/InputSystem.java @@ -25,6 +25,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder { private final KeyBindingPool keybindings; private static boolean inited = false; + + public InputSystem(AppAccess app) { super(app); diff --git a/src/mightypork/gamecore/input/KeyBindingPool.java b/src/mightypork/gamecore/input/KeyBindingPool.java index 1403656..d0bc11f 100644 --- a/src/mightypork/gamecore/input/KeyBindingPool.java +++ b/src/mightypork/gamecore/input/KeyBindingPool.java @@ -16,7 +16,7 @@ import mightypork.utils.logging.Log; */ public class KeyBindingPool implements KeyBinder, KeyEvent.Listener { - private final Set bindings = new HashSet(); + private final Set bindings = new HashSet<>(); /** diff --git a/src/mightypork/gamecore/input/KeyStroke.java b/src/mightypork/gamecore/input/KeyStroke.java index 1cb8c0e..10cf933 100644 --- a/src/mightypork/gamecore/input/KeyStroke.java +++ b/src/mightypork/gamecore/input/KeyStroke.java @@ -15,7 +15,7 @@ import org.lwjgl.input.Keyboard; */ public class KeyStroke { - private final Set keys = new LinkedHashSet(); + private final Set keys = new LinkedHashSet<>(); private final boolean fallingEdge; diff --git a/src/mightypork/gamecore/loading/AsyncResourceLoader.java b/src/mightypork/gamecore/loading/AsyncResourceLoader.java index 9f21834..a588f62 100644 --- a/src/mightypork/gamecore/loading/AsyncResourceLoader.java +++ b/src/mightypork/gamecore/loading/AsyncResourceLoader.java @@ -26,7 +26,7 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L private final ExecutorService exs = Executors.newCachedThreadPool(); - private final LinkedBlockingQueue toLoad = new LinkedBlockingQueue(); + private final LinkedBlockingQueue toLoad = new LinkedBlockingQueue<>(); private boolean stopped; private final BusAccess app; diff --git a/src/mightypork/gamecore/render/DisplaySystem.java b/src/mightypork/gamecore/render/DisplaySystem.java index 6b0090e..13df80b 100644 --- a/src/mightypork/gamecore/render/DisplaySystem.java +++ b/src/mightypork/gamecore/render/DisplaySystem.java @@ -200,6 +200,7 @@ public class DisplaySystem extends RootBusNode implements RectConstraint { return new Rect(getSize()); } + /** * @return current FPS */ @@ -208,7 +209,6 @@ public class DisplaySystem extends RootBusNode implements RectConstraint { return fpsMeter.getFPS(); } - public static final NumberConstraint width = new NumberConstraint() { @Override @@ -218,7 +218,6 @@ public class DisplaySystem extends RootBusNode implements RectConstraint { } }; - public static final NumberConstraint height = new NumberConstraint() { @Override diff --git a/src/mightypork/gamecore/render/Render.java b/src/mightypork/gamecore/render/Render.java index 5ed364e..b0a5c0a 100644 --- a/src/mightypork/gamecore/render/Render.java +++ b/src/mightypork/gamecore/render/Render.java @@ -30,6 +30,7 @@ public class Render { private static final Coord AXIS_Y = new Coord(0, 1, 0); private static final Coord AXIS_Z = new Coord(0, 0, 1); + /** * Bind GL color * @@ -464,8 +465,8 @@ public class Render { { quadTextured(quad, txquad.uvs, txquad.tx, tint); } - - + + /** * Setup Ortho projection for 2D graphics */ @@ -480,7 +481,7 @@ public class Render { // back to modelview glMatrixMode(GL_MODELVIEW); - + glLoadIdentity(); glDisable(GL_LIGHTING); diff --git a/src/mightypork/gamecore/render/Screenshot.java b/src/mightypork/gamecore/render/Screenshot.java index 4ecf4a9..5f08da0 100644 --- a/src/mightypork/gamecore/render/Screenshot.java +++ b/src/mightypork/gamecore/render/Screenshot.java @@ -17,10 +17,10 @@ import javax.imageio.ImageIO; */ public class Screenshot { - private int width; - private int height; - private int bpp; - private ByteBuffer bytes; + private final int width; + private final int height; + private final int bpp; + private final ByteBuffer bytes; private BufferedImage image; diff --git a/src/mightypork/gamecore/render/fonts/DeferredFont.java b/src/mightypork/gamecore/render/fonts/DeferredFont.java index 2e9880b..aaaac9b 100644 --- a/src/mightypork/gamecore/render/fonts/DeferredFont.java +++ b/src/mightypork/gamecore/render/fonts/DeferredFont.java @@ -40,7 +40,7 @@ public class DeferredFont extends BaseDeferredResource implements GLFont { private final double size; private final FontStyle style; private final String extraChars; - private FilterMode filter; + private final FilterMode filter; /** @@ -107,11 +107,8 @@ public class DeferredFont extends BaseDeferredResource implements GLFont { */ protected Font getAwtFont(String resource, float size, int style) throws FontFormatException, IOException { - InputStream in = null; - try { - - in = FileUtils.getResource(resource); + try (InputStream in = FileUtils.getResource(resource)) { Font awtFont = Font.createFont(Font.TRUETYPE_FONT, in); @@ -119,14 +116,8 @@ public class DeferredFont extends BaseDeferredResource implements GLFont { awtFont = awtFont.deriveFont(style); return awtFont; - - } finally { - try { - if (in != null) in.close(); - } catch (final IOException e) { - //pass - } } + } @@ -171,6 +162,7 @@ public class DeferredFont extends BaseDeferredResource implements GLFont { return font.getGlyphHeight(); } + @Override public int getSize() { diff --git a/src/mightypork/gamecore/render/fonts/FontBank.java b/src/mightypork/gamecore/render/fonts/FontBank.java index 3304dc4..0f786ce 100644 --- a/src/mightypork/gamecore/render/fonts/FontBank.java +++ b/src/mightypork/gamecore/render/fonts/FontBank.java @@ -6,8 +6,6 @@ import java.util.HashMap; import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppAdapter; import mightypork.gamecore.control.bus.events.ResourceLoadRequest; -import mightypork.gamecore.render.fonts.DeferredFont.FontStyle; -import mightypork.gamecore.render.textures.FilterMode; import mightypork.utils.logging.Log; import org.newdawn.slick.opengl.Texture; @@ -21,12 +19,13 @@ import org.newdawn.slick.opengl.Texture; public class FontBank extends AppAdapter { private static final GLFont NULL_FONT = new NullFont(); - + + public FontBank(AppAccess app) { super(app); } - private final HashMap fonts = new HashMap(); + private final HashMap fonts = new HashMap<>(); /** diff --git a/src/mightypork/gamecore/render/fonts/FontRenderer.java b/src/mightypork/gamecore/render/fonts/FontRenderer.java index 0460acf..4cf8f99 100644 --- a/src/mightypork/gamecore/render/fonts/FontRenderer.java +++ b/src/mightypork/gamecore/render/fonts/FontRenderer.java @@ -37,7 +37,7 @@ public class FontRenderer { * @param font used font * @param color drawing color */ - public FontRenderer(GLFont font, RGB color) { + public FontRenderer(GLFont font, RGB color) { this.font = font; this.color = color; } @@ -131,12 +131,13 @@ public class FontRenderer { Render.popState(); } - + /** * Draw on screen * * @param text text to draw - * @param bounds drawing bounds (height for font height, horizontal bounds for align) + * @param bounds drawing bounds (height for font height, horizontal bounds + * for align) * @param align horizontal alignment (with respect to bounds) */ public void draw(String text, Rect bounds, Align align) @@ -144,13 +145,13 @@ public class FontRenderer { this.draw(text, bounds, align, this.color); } - - + /** * Draw on screen * * @param text text to draw - * @param bounds drawing bounds (height for font height, horizontal bounds for align) + * @param bounds drawing bounds (height for font height, horizontal bounds + * for align) * @param align horizontal alignment (with respect to bounds) * @param color drawing color */ @@ -190,7 +191,7 @@ public class FontRenderer { draw(text, pos, height, align, this.color); } - + /** * Draw on screen * diff --git a/src/mightypork/gamecore/render/fonts/GLFont.java b/src/mightypork/gamecore/render/fonts/GLFont.java index f16d112..04707a0 100644 --- a/src/mightypork/gamecore/render/fonts/GLFont.java +++ b/src/mightypork/gamecore/render/fonts/GLFont.java @@ -36,8 +36,8 @@ public interface GLFont { * @return space needed */ int getWidth(String text); - - + + /** * @return specified font size */ diff --git a/src/mightypork/gamecore/render/fonts/NullFont.java b/src/mightypork/gamecore/render/fonts/NullFont.java index d6d0a67..005febf 100644 --- a/src/mightypork/gamecore/render/fonts/NullFont.java +++ b/src/mightypork/gamecore/render/fonts/NullFont.java @@ -38,8 +38,8 @@ public class NullFont implements GLFont { { return 0; } - - + + @Override public int getSize() { diff --git a/src/mightypork/gamecore/render/fonts/SlickFont.java b/src/mightypork/gamecore/render/fonts/SlickFont.java index d79af79..d3eaaf8 100644 --- a/src/mightypork/gamecore/render/fonts/SlickFont.java +++ b/src/mightypork/gamecore/render/fonts/SlickFont.java @@ -4,7 +4,6 @@ package mightypork.gamecore.render.fonts; import static org.lwjgl.opengl.GL11.*; import java.awt.Font; -import java.lang.reflect.Field; import mightypork.gamecore.render.Render; import mightypork.gamecore.render.textures.FilterMode; @@ -24,7 +23,7 @@ public class SlickFont implements GLFont { private final TrueTypeFont ttf; private FilterMode filter; - private int fsize; + private final int fsize; /** @@ -63,7 +62,7 @@ public class SlickFont implements GLFont { private void prepareForRender() - { + { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter.num); @@ -114,8 +113,8 @@ public class SlickFont implements GLFont { { return ttf.getWidth(text); } - - + + @Override public int getSize() { diff --git a/src/mightypork/gamecore/render/textures/FilterMode.java b/src/mightypork/gamecore/render/textures/FilterMode.java index 5c7f4f9..bcab9d4 100644 --- a/src/mightypork/gamecore/render/textures/FilterMode.java +++ b/src/mightypork/gamecore/render/textures/FilterMode.java @@ -1,7 +1,9 @@ package mightypork.gamecore.render.textures; + import org.lwjgl.opengl.GL11; + public enum FilterMode { LINEAR(GL11.GL_LINEAR), NEAREST(GL11.GL_NEAREST); @@ -12,4 +14,4 @@ public enum FilterMode private FilterMode(int gl) { this.num = gl; } -} \ No newline at end of file +} diff --git a/src/mightypork/gamecore/render/textures/TextureBank.java b/src/mightypork/gamecore/render/textures/TextureBank.java index bdff2ac..be4abae 100644 --- a/src/mightypork/gamecore/render/textures/TextureBank.java +++ b/src/mightypork/gamecore/render/textures/TextureBank.java @@ -22,9 +22,9 @@ public class TextureBank extends AppAdapter { super(app); } - private final HashMap textures = new HashMap(); + private final HashMap textures = new HashMap<>(); - private final HashMap quads = new HashMap(); + private final HashMap quads = new HashMap<>(); private DeferredTexture lastTx; diff --git a/src/mightypork/gamecore/render/textures/WrapMode.java b/src/mightypork/gamecore/render/textures/WrapMode.java index 6d18219..a9b26c5 100644 --- a/src/mightypork/gamecore/render/textures/WrapMode.java +++ b/src/mightypork/gamecore/render/textures/WrapMode.java @@ -1,7 +1,9 @@ package mightypork.gamecore.render.textures; + import org.lwjgl.opengl.GL11; + public enum WrapMode { CLAMP(GL11.GL_CLAMP), REPEAT(GL11.GL_REPEAT); @@ -12,4 +14,4 @@ public enum WrapMode private WrapMode(int gl) { this.num = gl; } -} \ No newline at end of file +} diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index eec8bbd..190bfc7 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -1,9 +1,6 @@ package mightypork.rogue; -import java.io.File; -import java.io.RandomAccessFile; -import java.nio.channels.FileLock; import java.util.Locale; import java.util.logging.Level; @@ -27,6 +24,7 @@ import mightypork.rogue.events.ActionRequest.RequestType; import mightypork.rogue.screens.test_bouncyboxes.ScreenTestBouncy; import mightypork.rogue.screens.test_cat_sound.ScreenTestCat; import mightypork.rogue.screens.test_font.ScreenTestFont; +import mightypork.utils.files.InstanceLock; import mightypork.utils.logging.Log; import mightypork.utils.logging.LogInstance; @@ -266,13 +264,13 @@ public class App implements AppAccess { { if (!Config.SINGLE_INSTANCE) return; - if (!lockInstance()) { - System.err.println("Working directory is locked.\nOnly one instance can run at a time."); + if (!InstanceLock.onFile(Paths.LOCK)) { + System.err.println("Could not obtain lock.\nOnly one instance can run at a time."); //@formatter:off JOptionPane.showMessageDialog( null, - "The game is already running.", + "Another instance is already running.", "Instance error", JOptionPane.ERROR_MESSAGE ); @@ -284,41 +282,6 @@ public class App implements AppAccess { } - private static boolean lockInstance() - { - final File lockFile = new File(Paths.WORKDIR, ".lock"); - try { - - final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw"); - - final FileLock fileLock = randomAccessFile.getChannel().tryLock(); - if (fileLock != null) { - Runtime.getRuntime().addShutdownHook(new Thread() { - - @Override - public void run() - { - try { - fileLock.release(); - randomAccessFile.close(); - lockFile.delete(); - } catch (final Exception e) { - System.err.println("Unable to remove lock file."); - e.printStackTrace(); - } - } - }); - return true; - } - - } catch (final Exception e) { - System.err.println("Unable to create and/or lock file."); - e.printStackTrace(); - } - return false; - } - - /** * @return sound system of the running instance */ diff --git a/src/mightypork/rogue/Paths.java b/src/mightypork/rogue/Paths.java index ae85794..c25efab 100644 --- a/src/mightypork/rogue/Paths.java +++ b/src/mightypork/rogue/Paths.java @@ -9,11 +9,12 @@ import mightypork.utils.files.OsUtils; public class Paths { private static final String APPDIR_NAME = "rogue"; - + public static final File WORKDIR = OsUtils.getWorkDir(APPDIR_NAME); public static final File LOGS = OsUtils.getWorkDir(APPDIR_NAME, "logs"); public static final File SCREENSHOTS = OsUtils.getWorkDir(APPDIR_NAME, "screenshots"); public static final File CONFIG = new File(WORKDIR, "config.ini"); + public static final File LOCK = new File(WORKDIR, ".lock"); public static final String DIR_EFFECTS = "res/sounds/effects/"; public static final String DIR_MUSIC = "res/sounds/music/"; diff --git a/src/mightypork/rogue/screens/LayerFps.java b/src/mightypork/rogue/screens/LayerFps.java index 7805ceb..0fa4ae1 100644 --- a/src/mightypork/rogue/screens/LayerFps.java +++ b/src/mightypork/rogue/screens/LayerFps.java @@ -15,7 +15,7 @@ import mightypork.utils.math.coord.Coord; public class LayerFps extends ScreenLayer { TextPainter tp; - private FontRenderer fr; + private final FontRenderer fr; public LayerFps(Screen screen) { @@ -28,7 +28,7 @@ public class LayerFps extends ScreenLayer { @Override public void render() { - Coord pos = new Coord(DisplaySystem.getWidth() - 8, 8); + final Coord pos = new Coord(DisplaySystem.getWidth() - 8, 8); fr.draw(getDisplay().getFps() + " fps", pos, 32, Align.RIGHT); } diff --git a/src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java b/src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java index 511be4e..9b461a1 100644 --- a/src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java +++ b/src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java @@ -21,7 +21,7 @@ import mightypork.utils.string.StringProvider; public class LayerBouncyBoxes extends ScreenLayer { - List boxes = new ArrayList(); + List boxes = new ArrayList<>(); private RowHolder layout; @@ -57,7 +57,7 @@ public class LayerBouncyBoxes extends ScreenLayer { boxes.add(bbr); } - StringProvider sp = new StringProvider() { + final StringProvider sp = new StringProvider() { @Override public String getString() @@ -91,7 +91,7 @@ public class LayerBouncyBoxes extends ScreenLayer { bbr.goRight(); } } - + @Override public int getPriority() diff --git a/src/mightypork/rogue/screens/test_bouncyboxes/ScreenTestBouncy.java b/src/mightypork/rogue/screens/test_bouncyboxes/ScreenTestBouncy.java index c00164d..92077d4 100644 --- a/src/mightypork/rogue/screens/test_bouncyboxes/ScreenTestBouncy.java +++ b/src/mightypork/rogue/screens/test_bouncyboxes/ScreenTestBouncy.java @@ -18,10 +18,10 @@ public class ScreenTestBouncy extends LayeredScreen { super(app); layer = new LayerBouncyBoxes(this); - + addLayer(new LayerFps(this)); - - addLayer(layer); + + addLayer(layer); bindKeyStroke(new KeyStroke(Keys.KEY_C), new Runnable() { @@ -33,6 +33,7 @@ public class ScreenTestBouncy extends LayeredScreen { }); } + @Override public String getId() { diff --git a/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java b/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java index 6f97bfa..bb39e9a 100644 --- a/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java +++ b/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java @@ -15,7 +15,6 @@ import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.input.Keys; import mightypork.gamecore.render.DisplaySystem; -import mightypork.gamecore.render.fonts.FontRenderer; import mightypork.gamecore.render.fonts.FontRenderer.Align; import mightypork.rogue.Res; import mightypork.utils.math.animation.AnimDouble; @@ -106,6 +105,7 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt text.render(); } + @Override public int getPriority() { diff --git a/src/mightypork/rogue/screens/test_cat_sound/ScreenTestCat.java b/src/mightypork/rogue/screens/test_cat_sound/ScreenTestCat.java index c6d9653..bec4400 100644 --- a/src/mightypork/rogue/screens/test_cat_sound/ScreenTestCat.java +++ b/src/mightypork/rogue/screens/test_cat_sound/ScreenTestCat.java @@ -14,12 +14,10 @@ import mightypork.rogue.screens.LayerFps; public class ScreenTestCat extends LayeredScreen { - - public ScreenTestCat(AppAccess app) { super(app); - addLayer(new LayerFps(this)); + addLayer(new LayerFps(this)); addLayer(new LayerFlyingCat(this)); bindKeyStroke(new KeyStroke(Keys.KEY_ESCAPE), new Runnable() { diff --git a/src/mightypork/rogue/screens/test_font/ScreenTestFont.java b/src/mightypork/rogue/screens/test_font/ScreenTestFont.java index 23cf743..2c8638c 100644 --- a/src/mightypork/rogue/screens/test_font/ScreenTestFont.java +++ b/src/mightypork/rogue/screens/test_font/ScreenTestFont.java @@ -1,8 +1,8 @@ package mightypork.rogue.screens.test_font; -import mightypork.gamecore.control.AppAccess; import static mightypork.utils.math.constraints.ConstraintFactory.*; +import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.gui.renderers.TextPainter; import mightypork.gamecore.gui.screens.Screen; import mightypork.gamecore.render.fonts.FontRenderer.Align; @@ -22,7 +22,7 @@ public class ScreenTestFont extends Screen { tp = new TextPainter(Res.getFont("default"), Align.CENTER, RGB.GREEN); tp.setText("Hello World!"); - RectConstraint strbox = c_grow(c_center(this), 0, c_div(c_height(this), 10)); + final RectConstraint strbox = c_grow(c_center(this), 0, c_div(c_height(this), 10)); tp.setContext(strbox); } diff --git a/src/mightypork/utils/config/PropertyManager.java b/src/mightypork/utils/config/PropertyManager.java index 2bfdf0c..d0f33ac 100644 --- a/src/mightypork/utils/config/PropertyManager.java +++ b/src/mightypork/utils/config/PropertyManager.java @@ -1,9 +1,13 @@ package mightypork.utils.config; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; import java.util.Map.Entry; +import java.util.TreeMap; import mightypork.utils.math.Range; import mightypork.utils.math.coord.Coord; @@ -155,9 +159,9 @@ public class PropertyManager { */ public PropertyManager(File file, String comment) { this.file = file; - this.entries = new TreeMap>(); - this.overrideValues = new TreeMap(); - this.renameTable = new TreeMap(); + this.entries = new TreeMap<>(); + this.overrideValues = new TreeMap<>(); + this.renameTable = new TreeMap<>(); this.fileComment = comment; } @@ -168,27 +172,19 @@ public class PropertyManager { public void apply() { boolean needsSave = false; - FileInputStream fis = null; - try { - new File(file.getParent()).mkdirs(); - fis = new FileInputStream(file); + new File(file.getParent()).mkdirs(); + + try (FileInputStream fis = new FileInputStream(file)) { props.load(fis); - } catch (final IOException e) { needsSave = true; props = new SortedProperties(); - } finally { - try { - if (fis != null) fis.close(); - } catch (final IOException e) { - e.printStackTrace(); - } } props.cfgBlankRowBetweenSections = cfgSeparateSections; props.cfgBlankRowBeforeComment = cfgNewlineBeforeComments; - final ArrayList keyList = new ArrayList(); + final ArrayList keyList = new ArrayList<>(); // rename keys for (final Entry entry : renameTable.entrySet()) { diff --git a/src/mightypork/utils/config/SimpleConfig.java b/src/mightypork/utils/config/SimpleConfig.java index 3ace1e7..c4949a6 100644 --- a/src/mightypork/utils/config/SimpleConfig.java +++ b/src/mightypork/utils/config/SimpleConfig.java @@ -62,7 +62,7 @@ public class SimpleConfig { */ public static List listFromString(String text) { - final List list = new ArrayList(); + final List list = new ArrayList<>(); final String[] groupsLines = text.split("\n"); @@ -92,7 +92,7 @@ public class SimpleConfig { */ public static Map mapFromString(String text) { - final LinkedHashMap pairs = new LinkedHashMap(); + final LinkedHashMap pairs = new LinkedHashMap<>(); final String[] groupsLines = text.split("\n"); @@ -148,7 +148,7 @@ public class SimpleConfig { */ public static void mapToFile(File target, Map data, boolean allowNulls) throws IOException { - final List lines = new ArrayList(); + final List lines = new ArrayList<>(); for (final Entry e : data.entrySet()) { String key = e.getKey(); diff --git a/src/mightypork/utils/config/SortedProperties.java b/src/mightypork/utils/config/SortedProperties.java index 2dd453e..d45c24a 100644 --- a/src/mightypork/utils/config/SortedProperties.java +++ b/src/mightypork/utils/config/SortedProperties.java @@ -26,7 +26,7 @@ public class SortedProperties extends java.util.Properties { public boolean cfgBlankRowBetweenSections = true; /** Comments for individual keys */ - private final Hashtable keyComments = new Hashtable(); + private final Hashtable keyComments = new Hashtable<>(); private static void writeComments(BufferedWriter bw, String comm) throws IOException @@ -272,7 +272,7 @@ public class SortedProperties extends java.util.Properties { @Override - public void load(InputStream is) throws IOException + public synchronized void load(InputStream is) throws IOException { load(is, "utf-8"); } diff --git a/src/mightypork/utils/files/FileTreeDiff.java b/src/mightypork/utils/files/FileTreeDiff.java index eaef035..dc94dd9 100644 --- a/src/mightypork/utils/files/FileTreeDiff.java +++ b/src/mightypork/utils/files/FileTreeDiff.java @@ -23,7 +23,7 @@ public class FileTreeDiff { private boolean logging = true; - private final List> compared = new ArrayList>(); + private final List> compared = new ArrayList<>(); private final Comparator fileFirstSorter = new Comparator() { @Override @@ -67,59 +67,31 @@ public class FileTreeDiff { private void calcChecksum() throws NotEqualException { - FileInputStream in1 = null, in2 = null; - CheckedInputStream cin1 = null, cin2 = null; for (final Tuple pair : compared) { - try { - ck1.reset(); - ck2.reset(); - - in1 = new FileInputStream(pair.a); - in2 = new FileInputStream(pair.b); - - cin1 = new CheckedInputStream(in1, ck1); - cin2 = new CheckedInputStream(in2, ck2); + ck1.reset(); + ck2.reset(); + + try ( FileInputStream in1 = new FileInputStream(pair.a); + FileInputStream in2 = new FileInputStream(pair.b)) { - while (true) { - final int read1 = cin1.read(BUFFER); - final int read2 = cin2.read(BUFFER); + try ( CheckedInputStream cin1 = new CheckedInputStream(in1, ck1); + CheckedInputStream cin2 = new CheckedInputStream(in2, ck2)) { - if (read1 != read2 || ck1.getValue() != ck2.getValue()) { - throw new NotEqualException("Bytes differ:\n" + pair.a + "\n" + pair.b); + while (true) { + final int read1 = cin1.read(BUFFER); + final int read2 = cin2.read(BUFFER); + + if (read1 != read2 || ck1.getValue() != ck2.getValue()) { + throw new NotEqualException("Bytes differ:\n" + pair.a + "\n" + pair.b); + } + + if (read1 == -1) break; } - - if (read1 == -1) break; } } catch (final IOException e) { // ignore - } finally { - - try { - if (cin1 != null) cin1.close(); - } catch (final Exception e) { - // ignore - } - - try { - if (cin2 != null) cin2.close(); - } catch (final Exception e) { - // ignore - } - - try { - if (in1 != null) in1.close(); - } catch (final Exception e) { - // ignore - } - - try { - if (in2 != null) in2.close(); - } catch (final Exception e) { - // ignore - } - } } } @@ -152,7 +124,7 @@ public class FileTreeDiff { } } else { - compared.add(new Tuple(f1, f2)); + compared.add(new Tuple<>(f1, f2)); } } diff --git a/src/mightypork/utils/files/FileUtils.java b/src/mightypork/utils/files/FileUtils.java index 2423745..73d1bda 100644 --- a/src/mightypork/utils/files/FileUtils.java +++ b/src/mightypork/utils/files/FileUtils.java @@ -44,8 +44,8 @@ public class FileUtils { } final String[] children = source.list(); - for (int i = 0; i < children.length; i++) { - copyDirectory(new File(source, children[i]), new File(target, children[i]), filter, filesCopied); + for (final String element : children) { + copyDirectory(new File(source, element), new File(target, element), filter, filesCopied); } } else { @@ -71,8 +71,8 @@ public class FileUtils { { if (source.isDirectory()) { final String[] children = source.list(); - for (int i = 0; i < children.length; i++) { - listDirectoryRecursive(new File(source, children[i]), filter, files); + for (final String element : children) { + listDirectoryRecursive(new File(source, element), filter, files); } } else { @@ -94,25 +94,11 @@ public class FileUtils { */ public static void copyFile(File source, File target) throws IOException { - InputStream in = null; - OutputStream out = null; - try { - in = new FileInputStream(source); - out = new FileOutputStream(target); + try ( InputStream in = new FileInputStream(source); + OutputStream out = new FileOutputStream(target)) { copyStream(in, out); - } finally { - try { - if (in != null) in.close(); - } catch (final IOException e) { - e.printStackTrace(); - } - try { - if (out != null) out.close(); - } catch (final IOException e) { - e.printStackTrace(); - } } } @@ -175,9 +161,10 @@ public class FileUtils { */ public static String fileToString(File file) throws IOException { - final FileInputStream fin = new FileInputStream(file); - - return streamToString(fin); + try (FileInputStream fin = new FileInputStream(file)) { + + return streamToString(fin); + } } @@ -208,7 +195,7 @@ public class FileUtils { Log.e("Error creating folder " + dir, e); } - final List list = new ArrayList(); + final List list = new ArrayList<>(); try { for (final File f : dir.listFiles(filter)) { @@ -374,16 +361,12 @@ public class FileUtils { */ public static void stringToFile(File file, String text) throws IOException { - PrintStream out = null; - try { - out = new PrintStream(new FileOutputStream(file), false, "UTF-8"); + try (PrintStream out = new PrintStream(new FileOutputStream(file), false, "UTF-8")) { out.print(text); out.flush(); - } finally { - if (out != null) out.close(); } } @@ -499,26 +482,10 @@ public class FileUtils { */ public static void resourceToFile(String resname, File file) throws IOException { - InputStream in = null; - OutputStream out = null; - - try { - in = FileUtils.getResource(resname); - out = new FileOutputStream(file); + try ( InputStream in = FileUtils.getResource(resname); + OutputStream out = new FileOutputStream(file)) { FileUtils.copyStream(in, out); - } finally { - try { - if (in != null) in.close(); - } catch (final IOException e) { - // ignore - } - - try { - if (out != null) out.close(); - } catch (final IOException e) { - // ignore - } } } @@ -529,10 +496,12 @@ public class FileUtils { * * @param resname resource name * @return resource as string, empty string on failure + * @throws IOException on fail */ - public static String resourceToString(String resname) + public static String resourceToString(String resname) throws IOException { - final InputStream in = FileUtils.getResource(resname); - return streamToString(in); + try (InputStream in = FileUtils.getResource(resname)) { + return streamToString(in); + } } } diff --git a/src/mightypork/utils/files/InstanceLock.java b/src/mightypork/utils/files/InstanceLock.java new file mode 100644 index 0000000..4b63c92 --- /dev/null +++ b/src/mightypork/utils/files/InstanceLock.java @@ -0,0 +1,51 @@ +package mightypork.utils.files; + + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileLock; + + +/** + * Instance lock (avoid running twice) + * + * @author MightyPork + */ +public class InstanceLock { + + @SuppressWarnings("resource") + public static boolean onFile(final File lockFile) + { + try { + final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw"); + + final FileLock fileLock = randomAccessFile.getChannel().tryLock(); + if (fileLock != null) { + + Runtime.getRuntime().addShutdownHook(new Thread() { + + @Override + public void run() + { + try { + fileLock.release(); + randomAccessFile.close(); + lockFile.delete(); + } catch (final Exception e) { + System.err.println("Unable to remove lock file."); + e.printStackTrace(); + } + } + }); + + return true; + } + + return false; + } catch (IOException e) { + return false; + } + } + +} diff --git a/src/mightypork/utils/files/ZipBuilder.java b/src/mightypork/utils/files/ZipBuilder.java index e684e96..360a517 100644 --- a/src/mightypork/utils/files/ZipBuilder.java +++ b/src/mightypork/utils/files/ZipBuilder.java @@ -17,7 +17,7 @@ import mightypork.utils.logging.Log; public class ZipBuilder { private final ZipOutputStream out; - private final HashSet included = new HashSet(); + private final HashSet included = new HashSet<>(); /** @@ -70,8 +70,9 @@ public class ZipBuilder { out.putNextEntry(new ZipEntry(path)); - final InputStream in = FileUtils.stringToStream(text); - FileUtils.copyStream(in, out); + try (InputStream in = FileUtils.stringToStream(text)) { + FileUtils.copyStream(in, out); + } } @@ -90,8 +91,9 @@ public class ZipBuilder { out.putNextEntry(new ZipEntry(path)); - final InputStream in = FileUtils.getResource(resPath); - FileUtils.copyStream(in, out); + try (InputStream in = FileUtils.getResource(resPath)) { + FileUtils.copyStream(in, out); + } } diff --git a/src/mightypork/utils/files/ZipUtils.java b/src/mightypork/utils/files/ZipUtils.java index b9f81f6..d279f68 100644 --- a/src/mightypork/utils/files/ZipUtils.java +++ b/src/mightypork/utils/files/ZipUtils.java @@ -1,11 +1,7 @@ package mightypork.utils.files; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @@ -36,18 +32,8 @@ public class ZipUtils { */ public static List extractZip(File file, File outputDir, StringFilter filter) throws IOException { - ZipFile zip = null; - try { - zip = new ZipFile(file); - + try (ZipFile zip = new ZipFile(file)) { return extractZip(zip, outputDir, filter); - - } finally { - try { - if (zip != null) zip.close(); - } catch (final IOException e) { - // ignore - } } } @@ -63,7 +49,7 @@ public class ZipUtils { */ public static List extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException { - final ArrayList files = new ArrayList(); + final ArrayList files = new ArrayList<>(); outputDir.mkdirs(); @@ -102,16 +88,8 @@ public class ZipUtils { */ public static List listZip(File zipFile) throws IOException { - ZipFile zip = null; - try { - zip = new ZipFile(zipFile); + try (ZipFile zip = new ZipFile(zipFile)) { return listZip(zip); - } finally { - try { - if (zip != null) zip.close(); - } catch (final IOException e) { - // ignore - } } } @@ -125,7 +103,7 @@ public class ZipUtils { */ public static List listZip(ZipFile zip) throws IOException { - final ArrayList files = new ArrayList(); + final ArrayList files = new ArrayList<>(); final Enumeration zipFileEntries = zip.entries(); @@ -154,29 +132,12 @@ public class ZipUtils { { destFile.getParentFile().mkdirs(); - BufferedInputStream is = null; - FileOutputStream fos = null; - BufferedOutputStream dest = null; - - try { - is = new BufferedInputStream(zip.getInputStream(entry)); - fos = new FileOutputStream(destFile); - dest = new BufferedOutputStream(fos, BUFFER_SIZE); + try ( InputStream in = zip.getInputStream(entry); + BufferedInputStream is = new BufferedInputStream(in); + FileOutputStream fos = new FileOutputStream(destFile); + BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) { FileUtils.copyStream(is, dest); - } finally { - try { - if (is != null) is.close(); - } catch (final IOException e) { - // ignore - } - - try { - if (dest != null) dest.close(); - } catch (final IOException e) { - // ignore - } - } } @@ -208,19 +169,10 @@ public class ZipUtils { public static boolean entryExists(File selectedFile, String string) { - ZipFile zf = null; - - try { - zf = new ZipFile(selectedFile); + try (ZipFile zf = new ZipFile(selectedFile)) { return zf.getEntry(string) != null; } catch (final Exception e) { return false; - } finally { - try { - if (zf != null) zf.close(); - } catch (final IOException e) { - // ignore - } } } diff --git a/src/mightypork/utils/files/ion/Ion.java b/src/mightypork/utils/files/ion/Ion.java index 4e990a7..def0264 100644 --- a/src/mightypork/utils/files/ion/Ion.java +++ b/src/mightypork/utils/files/ion/Ion.java @@ -16,7 +16,7 @@ import mightypork.utils.math.Calc; public class Ion { /** Ionizables */ - private static Map> customIonizables = new HashMap>(); + private static Map> customIonizables = new HashMap<>(); // register default ionizables static { @@ -67,22 +67,13 @@ public class Ion { */ public static Object fromFile(File file) throws IonException { - InputStream in = null; - try { - in = new FileInputStream(file); + try (InputStream in = new FileInputStream(file)) { + final Object obj = fromStream(in); return obj; } catch (final IOException e) { throw new IonException("Error loading ION file.", e); - } finally { - if (in != null) { - try { - in.close(); - } catch (final IOException e) { - e.printStackTrace(); - } - } } } @@ -122,30 +113,18 @@ public class Ion { */ public static void toFile(File path, Object obj) throws IonException { - OutputStream out = null; - try { + try (OutputStream out = new FileOutputStream(path)) { final String f = path.toString(); final File dir = new File(f.substring(0, f.lastIndexOf(File.separator))); dir.mkdirs(); - out = new FileOutputStream(path); - toStream(out, obj); out.flush(); out.close(); } catch (final Exception e) { throw new IonException("Error writing to ION file.", e); - } finally { - if (out != null) { - try { - out.close(); - } catch (final IOException e) { - e.printStackTrace(); - } - } - } } diff --git a/src/mightypork/utils/logging/Log.java b/src/mightypork/utils/logging/Log.java index 76976ee..e3d29fe 100644 --- a/src/mightypork/utils/logging/Log.java +++ b/src/mightypork/utils/logging/Log.java @@ -116,7 +116,7 @@ public class Log { staticLogging = flag; } - private static HashMap logs = new HashMap(); + private static HashMap logs = new HashMap<>(); private static LogInstance main = null; diff --git a/src/mightypork/utils/logging/LogInstance.java b/src/mightypork/utils/logging/LogInstance.java index b665b80..1b14492 100644 --- a/src/mightypork/utils/logging/LogInstance.java +++ b/src/mightypork/utils/logging/LogInstance.java @@ -50,7 +50,7 @@ public class LogInstance { private boolean sysout = true; private int monitorId = 0; - private final HashMap monitors = new HashMap(); + private final HashMap monitors = new HashMap<>(); private LogToSysoutMonitor sysoutMonitor; diff --git a/src/mightypork/utils/math/Calc.java b/src/mightypork/utils/math/Calc.java index 9d73dd3..e931896 100644 --- a/src/mightypork/utils/math/Calc.java +++ b/src/mightypork/utils/math/Calc.java @@ -890,7 +890,7 @@ public class Calc { } final String[] parts = list.split(","); - final ArrayList intList = new ArrayList(); + final ArrayList intList = new ArrayList<>(); for (final String part : parts) { try { diff --git a/src/mightypork/utils/math/animation/AnimDouble.java b/src/mightypork/utils/math/animation/AnimDouble.java index 4c965e1..5d08a9e 100644 --- a/src/mightypork/utils/math/animation/AnimDouble.java +++ b/src/mightypork/utils/math/animation/AnimDouble.java @@ -319,8 +319,8 @@ public class AnimDouble implements Updateable, Pauseable, NumberConstraint { { return paused; } - - + + @Override public double getValue() { diff --git a/src/mightypork/utils/math/constraints/ConstraintFactory.java b/src/mightypork/utils/math/constraints/ConstraintFactory.java index 1a7952d..d8035f2 100644 --- a/src/mightypork/utils/math/constraints/ConstraintFactory.java +++ b/src/mightypork/utils/math/constraints/ConstraintFactory.java @@ -25,6 +25,7 @@ public class ConstraintFactory { }; } + public static NumberConstraint c_max(final Object a, final Object b) { return new NumberConstraint() { @@ -58,7 +59,7 @@ public class ConstraintFactory { @Override public double getValue() { - return a.getValue()/2; + return a.getValue() / 2; } }; } @@ -128,6 +129,7 @@ public class ConstraintFactory { }; } + public static NumberConstraint c_add(final Object a, final Object b) { return new NumberConstraint() { @@ -140,6 +142,7 @@ public class ConstraintFactory { }; } + public static NumberConstraint c_sub(final Object a, final Object b) { return new NumberConstraint() { @@ -151,7 +154,7 @@ public class ConstraintFactory { } }; } - + public static NumberConstraint c_mul(final Object a, final Object b) { @@ -164,7 +167,7 @@ public class ConstraintFactory { } }; } - + public static NumberConstraint c_div(final Object a, final Object b) { @@ -178,6 +181,7 @@ public class ConstraintFactory { }; } + public static NumberConstraint c_percent(final Object whole, final Object percent) { return new NumberConstraint() { @@ -257,7 +261,7 @@ public class ConstraintFactory { public static RectConstraint c_shrink(RectConstraint r, Object shrink) { - NumberConstraint n = n(shrink); + final NumberConstraint n = n(shrink); return c_shrink(r, n, n, n, n); } @@ -293,9 +297,10 @@ public class ConstraintFactory { }; } + public static RectConstraint c_grow(RectConstraint r, Object grow) { - NumberConstraint n = n(grow); + final NumberConstraint n = n(grow); return c_grow(r, n, n, n, n); } @@ -338,6 +343,7 @@ public class ConstraintFactory { }; } + public static RectConstraint c_box(final RectConstraint r, final Object width, final Object height) { return new RectConstraint() { @@ -384,6 +390,7 @@ public class ConstraintFactory { /** * Center rect around given coords + * * @param r rect * @param x * @param y @@ -419,7 +426,8 @@ public class ConstraintFactory { } }; } - + + public static RectConstraint c_move(final RectConstraint r, final Object x, final Object y) { return new RectConstraint() { @@ -432,16 +440,19 @@ public class ConstraintFactory { }; } + /** * Convert {@link Double} to {@link NumberConstraint} if needed + * * @param o unknown numeric value * @return converted */ - public static NumberConstraint n(final Object o) { + public static NumberConstraint n(final Object o) + { - if(o instanceof NumberConstraint) return (NumberConstraint) o; + if (o instanceof NumberConstraint) return (NumberConstraint) o; - if(o instanceof Number) return new NumberConstraint() { + if (o instanceof Number) return new NumberConstraint() { @Override public double getValue() diff --git a/src/mightypork/utils/math/coord/Coord.java b/src/mightypork/utils/math/coord/Coord.java index dc540b3..4a6980c 100644 --- a/src/mightypork/utils/math/coord/Coord.java +++ b/src/mightypork/utils/math/coord/Coord.java @@ -203,7 +203,7 @@ public class Coord { */ public boolean isInRect(RectConstraint rect) { - Rect r = rect.getRect(); + final Rect r = rect.getRect(); return isInRect(r.getMin(), r.getMax()); } diff --git a/src/mightypork/utils/objects/Convert.java b/src/mightypork/utils/objects/Convert.java index 6d57bb3..8400bcb 100644 --- a/src/mightypork/utils/objects/Convert.java +++ b/src/mightypork/utils/objects/Convert.java @@ -120,21 +120,21 @@ public class Convert { if (o == null) return def; if (o instanceof String) return ((String) o); - if(o instanceof Boolean) { + if (o instanceof Boolean) { return (Boolean) o ? "True" : "False"; } - - if(o instanceof Coord) { - Coord c = (Coord) o; + + if (o instanceof Coord) { + final Coord c = (Coord) o; return String.format("[%f:%f:%f]", c.x, c.y, c.z); } - - if(o instanceof Range) { - Range c = (Range) o; + + if (o instanceof Range) { + final Range c = (Range) o; return String.format("%f:%f", c.getMin(), c.getMax()); } - if(o instanceof Class) { + if (o instanceof Class) { return Log.str(o); } diff --git a/src/mightypork/utils/objects/MapSort.java b/src/mightypork/utils/objects/MapSort.java index 768ad54..be3477a 100644 --- a/src/mightypork/utils/objects/MapSort.java +++ b/src/mightypork/utils/objects/MapSort.java @@ -22,7 +22,7 @@ public class MapSort { @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map sortByKeys(Map map, final Comparator comparator) { - final List keys = new LinkedList(map.keySet()); + final List keys = new LinkedList<>(map.keySet()); if (comparator == null) { Collections.sort(keys); @@ -32,7 +32,7 @@ public class MapSort { // LinkedHashMap will keep the keys in the order they are inserted // which is currently sorted on natural ordering - final Map sortedMap = new LinkedHashMap(); + final Map sortedMap = new LinkedHashMap<>(); for (final K key : keys) { sortedMap.put(key, map.get(key)); } @@ -51,7 +51,7 @@ public class MapSort { @SuppressWarnings("rawtypes") public static Map sortByValues(Map map, final Comparator comparator) { - final List> entries = new LinkedList>(map.entrySet()); + final List> entries = new LinkedList<>(map.entrySet()); Collections.sort(entries, new Comparator>() { @@ -65,7 +65,7 @@ public class MapSort { // LinkedHashMap will keep the keys in the order they are inserted // which is currently sorted on natural ordering - final Map sortedMap = new LinkedHashMap(); + final Map sortedMap = new LinkedHashMap<>(); for (final Map.Entry entry : entries) { sortedMap.put(entry.getKey(), entry.getValue()); diff --git a/src/mightypork/utils/objects/ObjectUtils.java b/src/mightypork/utils/objects/ObjectUtils.java index c79588b..1bbd929 100644 --- a/src/mightypork/utils/objects/ObjectUtils.java +++ b/src/mightypork/utils/objects/ObjectUtils.java @@ -39,7 +39,7 @@ public class ObjectUtils { public static List arrayToList(T[] objs) { - final ArrayList list = new ArrayList(); + final ArrayList list = new ArrayList<>(); for (final T o : objs) { list.add(o); } diff --git a/src/mightypork/utils/objects/VarargsParser.java b/src/mightypork/utils/objects/VarargsParser.java index f20722f..2b31785 100644 --- a/src/mightypork/utils/objects/VarargsParser.java +++ b/src/mightypork/utils/objects/VarargsParser.java @@ -32,7 +32,7 @@ public class VarargsParser { @SuppressWarnings("unchecked") public Map parse(Object... args) throws ClassCastException, IllegalArgumentException { - final LinkedHashMap attrs = new LinkedHashMap(); + final LinkedHashMap attrs = new LinkedHashMap<>(); if (args.length % 2 != 0) { throw new IllegalArgumentException("Odd number of elements in varargs map!"); diff --git a/src/mightypork/utils/string/StringProvider.java b/src/mightypork/utils/string/StringProvider.java index 0e219d8..2751b98 100644 --- a/src/mightypork/utils/string/StringProvider.java +++ b/src/mightypork/utils/string/StringProvider.java @@ -18,12 +18,13 @@ public interface StringProvider { public static class StringWrapper implements StringProvider { private final String value; - + public StringWrapper(String value) { this.value = value; } + @Override public String getString() {