Fixed null pointer with FileUtil.parseDir(), and issues with MathUtil.random. Added more logging info to Storage

This commit is contained in:
nathan 2014-08-25 14:40:13 +02:00
parent 6851975826
commit c66c1d43df
3 changed files with 35 additions and 17 deletions

View File

@ -589,16 +589,18 @@ public class FileUtil {
while (directories.peek() != null) { while (directories.peek() != null) {
File dir = directories.poll(); File dir = directories.poll();
File[] listFiles = dir.listFiles(); File[] listFiles = dir.listFiles();
for (File file : listFiles) { if (listFiles != null) {
if (file.isDirectory()) { for (File file : listFiles) {
directories.add(file); if (file.isDirectory()) {
} else { directories.add(file);
if (extensionsToMatch == null) {
jarList.add(file);
} else { } else {
for (String e : extensionsToMatch) { if (extensionsToMatch == null || extensionsToMatch.length == 0 || extensionsToMatch[0] == null) {
if (file.getAbsolutePath().endsWith(e)) { jarList.add(file);
jarList.add(file); } else {
for (String e : extensionsToMatch) {
if (file.getAbsolutePath().endsWith(e)) {
jarList.add(file);
}
} }
} }
} }

View File

@ -25,39 +25,55 @@ public class MathUtils {
private static ThreadLocal<MersenneTwisterFast> random = new ThreadLocal<MersenneTwisterFast>(); private static ThreadLocal<MersenneTwisterFast> random = new ThreadLocal<MersenneTwisterFast>();
/**
* Creates the thread local MersenneTwister (as it's not thread safe), if necessary
* @return the MersenneTwister.
*/
public static MersenneTwisterFast random() {
MersenneTwisterFast mersenneTwisterFast = random.get();
if (mersenneTwisterFast == null) {
mersenneTwisterFast = new MersenneTwisterFast();
random.set(mersenneTwisterFast);
return mersenneTwisterFast;
} else {
return mersenneTwisterFast;
}
}
/** Returns a random integer */ /** Returns a random integer */
static public final int randomInt () { static public final int randomInt () {
return random.get().nextInt(); return random().nextInt();
} }
/** Returns a random number between 0 (inclusive) and the specified value (inclusive). */ /** Returns a random number between 0 (inclusive) and the specified value (inclusive). */
static public final int randomInt (int range) { static public final int randomInt (int range) {
return random.get().nextInt(range + 1); return random().nextInt(range + 1);
} }
/** Returns a random number between start (inclusive) and end (inclusive). */ /** Returns a random number between start (inclusive) and end (inclusive). */
static public final int randomInt (int start, int end) { static public final int randomInt (int start, int end) {
return start + random.get().nextInt(end - start + 1); return start + random().nextInt(end - start + 1);
} }
/** Returns a random boolean value. */ /** Returns a random boolean value. */
static public final boolean randomBoolean () { static public final boolean randomBoolean () {
return random.get().nextBoolean(); return random().nextBoolean();
} }
/** Returns random number between 0.0 (inclusive) and 1.0 (exclusive). */ /** Returns random number between 0.0 (inclusive) and 1.0 (exclusive). */
static public final float randomFloat () { static public final float randomFloat () {
return random.get().nextFloat(); return random().nextFloat();
} }
/** Returns a random number between 0 (inclusive) and the specified value (exclusive). */ /** Returns a random number between 0 (inclusive) and the specified value (exclusive). */
static public final float randomFloat (float range) { static public final float randomFloat (float range) {
return random.get().nextFloat() * range; return random().nextFloat() * range;
} }
/** Returns a random number between start (inclusive) and end (exclusive). */ /** Returns a random number between start (inclusive) and end (exclusive). */
static public final float randomFloat (float start, float end) { static public final float randomFloat (float start, float end) {
return start + random.get().nextFloat() * (end - start); return start + random().nextFloat() * (end - start);
} }
// --- // ---

View File

@ -235,7 +235,7 @@ public class Storage {
Object readObject = this.kryo.readObject(input, clazz); Object readObject = this.kryo.readObject(input, clazz);
return (T) readObject; return (T) readObject;
} catch (Exception e) { } catch (Exception e) {
logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath()); logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath(), e);
return null; return null;
} finally { } finally {
if (input != null) { if (input != null) {