From 36f6000b4287f01dc0487cf2454e857102f72c14 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 10 Mar 2017 15:54:22 +0100 Subject: [PATCH] Split out random utils into their own class, added number checks --- src/dorkbox/util/MathUtil.java | 228 ++++++++++++++++++++++--------- src/dorkbox/util/RandomUtil.java | 93 +++++++++++++ 2 files changed, 255 insertions(+), 66 deletions(-) create mode 100644 src/dorkbox/util/RandomUtil.java diff --git a/src/dorkbox/util/MathUtil.java b/src/dorkbox/util/MathUtil.java index 1fb1b84..98bf00f 100644 --- a/src/dorkbox/util/MathUtil.java +++ b/src/dorkbox/util/MathUtil.java @@ -19,81 +19,177 @@ package dorkbox.util; public class MathUtil { - private static final FastThreadLocal random = new FastThreadLocal() { - @Override - public - MersenneTwisterFast initialValue() { - return new MersenneTwisterFast(); + /** + * Checks to see if the string is an integer + * + * @return true if it's an integer, false otherwise + */ + public static + boolean isInteger(final String string) { + return isNumber(string, 10); + } + + /** + * Checks to see if the string is a long + * + * @return true if it's a long, false otherwise + */ + public static + boolean isLong(final String string) { + return isNumber(string, 19); + } + + /** + * Checks to see if the string is a number + * + * @return true if it's a number, false otherwise + */ + public static + boolean isNumber(final String string, long sizeLimit) { + if (string == null) { + return false; + } + if (sizeLimit <= 0) { + return false; } - }; - /** - * Creates the thread local MersenneTwister (as it's not thread safe), if necessary - */ - public static - MersenneTwisterFast random() { - return random.get(); + int length = string.length(); + if (length == 0) { + return false; + } + + int i = 0; + if (string.charAt(0) == '-') { + if (length == 1) { + return false; + } + i = 1; + } + + if (length - i > sizeLimit) { + return false; + } + + for (; i < length; i++) { + char c = string.charAt(i); + if (c < '0' || c > '9') { + return false; + } + } + + return true; } /** - * Returns a random integer + * Gets the number of digits represented by the specified number + * + * @param number the number to check, negative numbers are also acceptable but the sign is not counted + * + * @return the number of digits of the number, from 1-19. */ public static - int randomInt() { - return random.get().nextInt(); + int numberOfDigits(long number) { + // have to make it always positive for the following checks to pass. + if (number < 0L) { + number = -number; + } + + // Guessing 4 digit numbers will be more probable. + // They are set in the first branch. + if (number < 10000L) { // from 1 to 4 + if (number < 100L) { // 1 or 2 + if (number < 10L) { + return 1; + } + else { + return 2; + } + } + else { // 3 or 4 + if (number < 1000L) { + return 3; + } + else { + return 4; + } + } + } + else { // from 5 to 20 (albeit longs can't have more than 18 or 19) + if (number < 1000000000000L) { // from 5 to 12 + if (number < 100000000L) { // from 5 to 8 + if (number < 1000000L) { // 5 or 6 + if (number < 100000L) { + return 5; + } + else { + return 6; + } + } + else { // 7 u 8 + if (number < 10000000L) { + return 7; + } + else { + return 8; + } + } + } + else { // from 9 to 12 + if (number < 10000000000L) { // 9 or 10 + if (number < 1000000000L) { + return 9; + } + else { + return 10; + } + } + else { // 11 or 12 + if (number < 100000000000L) { + return 11; + } + else { + return 12; + } + } + } + } + else { // from 13 to ... (18 or 20) + if (number < 10000000000000000L) { // from 13 to 16 + if (number < 100000000000000L) { // 13 or 14 + if (number < 10000000000000L) { + return 13; + } + else { + return 14; + } + } + else { // 15 or 16 + if (number < 1000000000000000L) { + return 15; + } + else { + return 16; + } + } + } + else { // from 17 to ... 20? + if (number < 1000000000000000000L) { // 17 or 18 + if (number < 100000000000000000L) { + return 17; + } + else { + return 18; + } + } + else { // 19? Can it be? + // 10000000000000000000L isn't a valid long. + return 19; + } + } + } + } } - /** - * Returns a random number between 0 (inclusive) and the specified value (inclusive). - */ - public static - int randomInt(int range) { - return random.get().nextInt(range + 1); - } - - /** - * Returns a random number between start (inclusive) and end (inclusive). - */ - public static - int randomInt(int start, int end) { - return start + random.get().nextInt(end - start + 1); - } - - /** - * Returns a random boolean value. - */ - public static - boolean randomBoolean() { - return random.get().nextBoolean(); - } - - /** - * Returns random number between 0.0 (inclusive) and 1.0 (exclusive). - */ - public static - float randomFloat() { - return random.get().nextFloat(); - } - - /** - * Returns a random number between 0 (inclusive) and the specified value (exclusive). - */ - public static - float randomFloat(float range) { - return random.get().nextFloat() * range; - } - - /** - * Returns a random number between start (inclusive) and end (exclusive). - */ - public static - float randomFloat(float start, float end) { - return start + random.get().nextFloat() * (end - start); - } - - // --- - - public static boolean isEven(int value) { return (value & 1) == 0; diff --git a/src/dorkbox/util/RandomUtil.java b/src/dorkbox/util/RandomUtil.java new file mode 100644 index 0000000..3d6388d --- /dev/null +++ b/src/dorkbox/util/RandomUtil.java @@ -0,0 +1,93 @@ +/* + * Copyright 2010 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.util; + + +public +class RandomUtil { + + private static final FastThreadLocal random = new FastThreadLocal() { + @Override + public + MersenneTwisterFast initialValue() { + return new MersenneTwisterFast(); + } + }; + + /** + * Creates the thread local MersenneTwister (as it's not thread safe), if necessary + */ + public static + MersenneTwisterFast get() { + return random.get(); + } + + /** + * Returns a get integer + */ + public static + int int_() { + return get().nextInt(); + } + + /** + * Returns a get number between 0 (inclusive) and the specified value (inclusive). + */ + public static + int int_(int range) { + return get().nextInt(range + 1); + } + + /** + * Returns a get number between start (inclusive) and end (inclusive). + */ + public static + int int_(int start, int end) { + return start + get().nextInt(end - start + 1); + } + + /** + * Returns a get boolean value. + */ + public static + boolean bool() { + return get().nextBoolean(); + } + + /** + * Returns get number between 0.0 (inclusive) and 1.0 (exclusive). + */ + public static + float float_() { + return get().nextFloat(); + } + + /** + * Returns a get number between 0 (inclusive) and the specified value (exclusive). + */ + public static + float float_(float range) { + return get().nextFloat() * range; + } + + /** + * Returns a get number between start (inclusive) and end (exclusive). + */ + public static + float float_(float start, float end) { + return start + get().nextFloat() * (end - start); + } +}