Skip to content

Commit

Permalink
Maths is fun...
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeballcode committed Feb 10, 2016
1 parent bde3ad1 commit 87c391a
Show file tree
Hide file tree
Showing 21 changed files with 233 additions and 75 deletions.
4 changes: 1 addition & 3 deletions src/com/jrutil/AWTHelper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.jrutil;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

/**
* A class for interacting with the system using UI events. Not restricted to only Java {@link javax.swing.JFrame}s.
Expand All @@ -23,6 +20,7 @@ public class AWTHelper {
} catch (AWTException ignored) {
}
}

/**
* Gets a screengrab of the screen.
*
Expand Down
3 changes: 1 addition & 2 deletions src/com/jrutil/ColorPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
public class ColorPrinter {
/**
* A {@link LockableHashMap} that contains all of the formats, and is locked.
*
* <p/>
* Do note that <code>italic</code> and <code>strike</code> may not be available on all systems. One such system would be IntelliJ.
*
*/
public static final LockableHashMap<String, Integer> formatMap = new LockableHashMap<String, Integer>();

Expand Down
21 changes: 11 additions & 10 deletions src/com/jrutil/DelayedCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@ private DelayedCode(ActionListener listener, long delay) {
start();
}

@Override
public void run() {
try {
Thread.sleep(d);
l.actionPerformed(null);
} catch (InterruptedException e) {
// JS's clearTimeout called.
}
}

/**
* Clears a delayed code before it's runned.
*
* @param code The {@link DelayedCode} to stop.
*/
public static void clearTimeout(DelayedCode code) {
Expand All @@ -47,4 +38,14 @@ public static DelayedCode setTimeout(ActionListener listener, long delay) {
return new DelayedCode(listener, delay);
}

@Override
public void run() {
try {
Thread.sleep(d);
l.actionPerformed(null);
} catch (InterruptedException e) {
// JS's clearTimeout called.
}
}

}
1 change: 0 additions & 1 deletion src/com/jrutil/LockableHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Map;

/**
*
* A {@link HashMap} that can be locked, but cannot be unlocked.
* Good for exposing HashMaps that should not be modified after class initialization
*
Expand Down
26 changes: 15 additions & 11 deletions src/com/jrutil/LockedHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@

/**
* A locked {@link HashMap} that may not be modified at all after instantiation.
*
* @param <Shadow> The {@link Shadow}
* @param <Wizard> The {@link Wizard}
*/
public class LockedHashMap<Shadow, Wizard> extends HashMap<Shadow, Wizard> {

/**
* Creates a new {@link LockedHashMap} that accepts an existing {@link Map}.
*
* @param map The existing {@link Map}
*/
public LockedHashMap(Map<Shadow, Wizard> map) {
for (Shadow shadow : map.keySet()) {
super.put(shadow, map.get(shadow));
}
}

/**
* This method does nothing.
* @param key The value
*
* @param key The value
* @param value The key
* @return The value
*/
Expand All @@ -23,20 +36,11 @@ public Wizard put(Shadow key, Wizard value) {

/**
* This method does nothing
*
* @param m The {@link Map} to add.
*/
@Override
public void putAll(Map<? extends Shadow, ? extends Wizard> m) {
}

/**
* Creates a new {@link LockedHashMap} that accepts an existing {@link Map}.
* @param map The existing {@link Map}
*/
public LockedHashMap(Map<Shadow, Wizard> map) {
for (Shadow shadow : map.keySet()) {
super.put(shadow, map.get(shadow));
}
}

}
27 changes: 14 additions & 13 deletions src/com/jrutil/RepeatedCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,9 @@ private RepeatedCode(ActionListener listener, long delay) {
start();
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(d);
l.actionPerformed(null);
} catch (InterruptedException e) {
// JS's clearInterval called.
return;
}
}
}

/**
* Stops a code loop before.
*
* @param code The {@link RepeatedCode} to stop.
*/
public static void clearInterval(RepeatedCode code) {
Expand All @@ -50,4 +38,17 @@ public static RepeatedCode setInterval(ActionListener listener, long delay) {
return new RepeatedCode(listener, delay);
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(d);
l.actionPerformed(null);
} catch (InterruptedException e) {
// JS's clearInterval called.
return;
}
}
}

}
26 changes: 26 additions & 0 deletions src/com/jrutil/TerminalHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ public static boolean readBoolean(String prefix) throws IOException, NumberForma
return Boolean.parseBoolean(read(prefix));
}


/**
* Reads a double from the console.
*
* @return The double
* @throws IOException If an error occurred while reading from {@link System#in}.
* @throws NumberFormatException If the input is not a valid double.
*/
public static double readDouble() throws IOException, NumberFormatException {
return readDouble("");
}


/**
* Reads a double from the console, with a custom prefix.
*
* @param prefix The prefix
* @return The double
* @throws IOException If an error occurred while reading from {@link System#in}.
* @throws NumberFormatException If the input is not a valid double.
*/
public static double readDouble(String prefix) throws IOException, NumberFormatException {
return Double.parseDouble(read(prefix));
}


/**
* Reads a float from the console.
*
Expand Down
6 changes: 0 additions & 6 deletions src/com/jrutil/datatransfer/ClipboardHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@

public class ClipboardHelper {

// public static native void setStringClipboardContents(String contents);
//
// public static native void setImageClipboardContents(BufferedImage contents);
//
// public static native void setFileClipboardContents(File contents);

/**
* Gets the string contents of the clipboard
*
Expand Down
38 changes: 38 additions & 0 deletions src/com/jrutil/io/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.jrutil.io;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class FileUtils {

public static void removeRecursive(Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw exc;
}
}
});
}
}
72 changes: 72 additions & 0 deletions src/com/jrutil/math/MathHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.jrutil.math;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Random;

public class MathHelper {

/**
* Generates a random int from the range provided.
*
* @param start The start range.
* @param end The end range.
* @return The random number
*/
public static int randomInt(int start, int end) {
return new Random().nextInt(end - start + 1) + start;
}

/**
* Generates a random float from the range provided.
*
* @param start The start range.
* @param end The end range.
* @return The random number.
*/
public static float randomFloat(float start, float end) {
return start + (end - start) * new Random().nextFloat();
}

/**
* Generates a random double from the range provided.
*
* @param start The start range.
* @param end The end range.
* @return The random number.
*/
public static double randomDouble(double start, double end) {
return start + (end - start) * new Random().nextDouble();
}

/**
* Rounds the given float to the provided number of decimal places.
*
* @param in The float given in.
* @param numberOfDP The number of decimal places expected.
* @return The new float.
*/
public static String roundFloatToXDP(float in, int numberOfDP) {
StringBuilder n = new StringBuilder("0.");
for (int i = 0; i < numberOfDP; i++) n.append("0");
DecimalFormat df = new DecimalFormat(n.toString());
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(in);
}

/**
* Rounds the given double to the provided number of decimal places.
*
* @param in The double given in.
* @param numberOfDP The number of decimal places expected.
* @return The new double.
*/
public static String roundDoubleToXDP(double in, int numberOfDP) {
StringBuilder n = new StringBuilder("0.");
for (int i = 0; i < numberOfDP; i++) n.append("0");
DecimalFormat df = new DecimalFormat(n.toString());
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(in);
}

}
5 changes: 3 additions & 2 deletions src/com/jrutil/reflect/ReflectedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ protected ReflectedClass(Class<T> clazz) {

/**
* Gets all methods under the class.
* @return An array of methods that this class has.
*
* @return An array of methods that this class has.
* @see ReflectedMethod
*/
public ReflectedMethod[] getAllMethods() {
Expand All @@ -35,9 +35,9 @@ public ReflectedMethod[] getAllMethods() {

/**
* Gets a method in this class by it's bytecode signature
*
* @param signature The bytecode signature
* @return The method, or null if not found.
*
* @see ReflectedMethod
*/
public ReflectedMethod getMethodByBytecodeSignature(String signature) {
Expand All @@ -50,6 +50,7 @@ public ReflectedMethod getMethodByBytecodeSignature(String signature) {

/**
* Gets a method in this class by it's argument types.
*
* @param types The argument types.
* @return The method, or null if not found.
*/
Expand Down

0 comments on commit 87c391a

Please sign in to comment.