diff --git a/modules/utils/src/main/java/org/springside/modules/utils/math/Maths.java b/modules/utils/src/main/java/org/springside/modules/utils/base/Maths.java
similarity index 50%
rename from modules/utils/src/main/java/org/springside/modules/utils/math/Maths.java
rename to modules/utils/src/main/java/org/springside/modules/utils/base/Maths.java
index a3dde08c..b07a4430 100644
--- a/modules/utils/src/main/java/org/springside/modules/utils/math/Maths.java
+++ b/modules/utils/src/main/java/org/springside/modules/utils/base/Maths.java
@@ -1,21 +1,10 @@
-package org.springside.modules.utils.math;
+package org.springside.modules.utils.base;
public class Maths {
- /**
- * Fast method of finding the next power of 2 greater than or equal to the supplied value.
- *
- *
If the value is {@code <= 0} then 1 will be returned.
- * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30.
- *
- * @param value from which to search for next power of 2
- * @return The next power of 2 or the value itself if it is a power of 2
- */
- public static int findNextPositivePowerOfTwo(final int value) {
- assert value > Integer.MIN_VALUE && value < 0x40000000;
- return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
- }
/**
+ * 找出最接近的2的倍数,比如15返回16, 17返回32. From Netty
+ *
* Fast method of finding the next power of 2 greater than or equal to the supplied value.
*
This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a
* valid range.
@@ -27,7 +16,7 @@ public class Maths {
*
{@code >= 2^30} -> 2^30
*
*/
- public static int safeFindNextPositivePowerOfTwo(final int value) {
- return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value);
+ public static int findNextPositivePowerOfTwo(final int value) {
+ return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}
}
diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/Numbers.java b/modules/utils/src/main/java/org/springside/modules/utils/base/Numbers.java
index f3cdf1b9..b73d6b5b 100644
--- a/modules/utils/src/main/java/org/springside/modules/utils/base/Numbers.java
+++ b/modules/utils/src/main/java/org/springside/modules/utils/base/Numbers.java
@@ -1,30 +1,208 @@
package org.springside.modules.utils.base;
-import com.google.common.annotations.Beta;
+import org.apache.commons.lang3.math.NumberUtils;
+
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
/**
* 数字的工具类
*
- * 1.基于Guava的原始类型与byte[]的转换.
+ * 1.基于Guava的原始类型数字与byte[]的转换.
+ *
+ * 2.基于Common Lang判断字符串是否数字, 是否16进制字符串
+ *
+ * 3.基于Common Lang将字符串安全的转化为原始类型数字
+ *
+ * 4.将10进制字符串安全的转化为数字对象(Common Lang中处理不完善)
+ *
+ * 5.将16进制字符串安全的转化为数字对象
*/
-@Beta
public class Numbers {
- public static byte[] longToBytes(long value) {
- return Longs.toByteArray(value);
- }
+ ///////////// bytes[] 与原始类型数字转换 ///////
- public static long bytesToLong(byte[] bytes) {
- return Longs.fromByteArray(bytes);
- }
-
- public static byte[] intToBytes(int value) {
+ public static byte[] toBytes(int value) {
return Ints.toByteArray(value);
}
- public static int bytesToInt(byte[] bytes) {
+ public static byte[] toBytes(long value) {
+ return Longs.toByteArray(value);
+ }
+
+ public static int toInt(byte[] bytes) {
return Ints.fromByteArray(bytes);
}
+
+ public static long toLong(byte[] bytes) {
+ return Longs.fromByteArray(bytes);
+ }
+
+ /////// 判断字符串类型//////////
+ /**
+ * 判断字符串是否合法数字
+ */
+ public static boolean isNumber(String str) {
+ return NumberUtils.isNumber(str);
+ }
+
+ /**
+ * 判断字符串是否16进制
+ */
+ public static boolean isHexNumber(String value) {
+ int index = (value.startsWith("-") ? 1 : 0);
+ return (value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index));
+ }
+
+ /////////// 将字符串安全的转化为原始类型数字/////////
+
+ /**
+ * 将10进制的String安全的转化为int,当str为空或非数字字符串时,返回0
+ */
+ public static int toInt(String str) {
+ return NumberUtils.toInt(str, 0);
+ }
+
+ /**
+ * 将10进制的String安全的转化为int,当str为空或非数字字符串时,返回default值
+ */
+ public static int toInt(String str, int defaultValue) {
+ return NumberUtils.toInt(str, defaultValue);
+ }
+
+ /**
+ * 将10进制的String安全的转化为long,当str为空或非数字字符串时,返回0
+ */
+ public static long toLong(String str) {
+ return NumberUtils.toLong(str, 0L);
+ }
+
+ /**
+ * 将10进制的String安全的转化为long,当str为空或非数字字符串时,返回default值
+ */
+ public static long toLong(String str, long defaultValue) {
+ return NumberUtils.toLong(str, defaultValue);
+ }
+
+ /**
+ * 将10进制的String安全的转化为double,当str为空或非数字字符串时,返回0
+ */
+ public static double toDouble(String str) {
+ return NumberUtils.toDouble(str, 0L);
+ }
+
+ /**
+ * 将10进制的String安全的转化为double,当str为空或非数字字符串时,返回default值
+ */
+ public static double toDouble(String str, double defaultValue) {
+ return NumberUtils.toDouble(str, defaultValue);
+ }
+
+ ////////////// 10进制字符串 转换对象类型数字/////////////
+ /**
+ * 将10进制的String安全的转化为Integer,当str为空或非数字字符串时,返回null
+ */
+ public static Integer toIntObject(String str) {
+ return toIntObject(str, null);
+ }
+
+ /**
+ * 将10进制的String安全的转化为Integer,当str为空或非数字字符串时,返回default值
+ */
+ public static Integer toIntObject(String str, Integer defaultValue) {
+ if (str == null) {
+ return defaultValue;
+ }
+ try {
+ return Integer.valueOf(str);
+ } catch (final NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 将10进制的String安全的转化为Long,当str为空或非数字字符串时,返回null
+ */
+ public static Long toLongObject(String str) {
+ return toLongObject(str, null);
+ }
+
+ /**
+ * 将10进制的String安全的转化为Long,当str为空或非数字字符串时,返回default值
+ */
+ public static Long toLongObject(String str, Long defaultValue) {
+ if (str == null) {
+ return defaultValue;
+ }
+ try {
+ return Long.valueOf(str);
+ } catch (final NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 将10进制的String安全的转化为Double,当str为空或非数字字符串时,返回null
+ */
+ public static Double toDoubleObject(String str) {
+ return toDoubleObject(str, null);
+ }
+
+ /**
+ * 将10进制的String安全的转化为Long,当str为空或非数字字符串时,返回default值
+ */
+ public static Double toDoubleObject(String str, Double defaultValue) {
+ if (str == null) {
+ return defaultValue;
+ }
+ try {
+ return Double.valueOf(str);
+ } catch (final NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
+ //////////// 16进制 字符串转换为数字对象//////////
+
+ /**
+ * 将16进制的String转化为Integer,出错时返回null.
+ */
+ public static Integer hexToIntObject(String str) {
+ return hexToIntObject(str, null);
+ }
+
+ /**
+ * 将16进制的String转化为Integer,出错时返回默认值.
+ */
+ public static Integer hexToIntObject(String str, Integer defaultValue) {
+ if (str == null) {
+ return defaultValue;
+ }
+ try {
+ return Integer.decode(str);
+ } catch (final NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * 将16进制的String转化为Long,出错时返回null.
+ */
+ public static Long hexToLongObject(String str) {
+ return hexToLongObject(str, null);
+ }
+
+ /**
+ * 将16进制的String转化为Long,出错时返回默认值.
+ */
+ public static Long hexToLongObject(String str, Long defaultValue) {
+ if (str == null) {
+ return defaultValue;
+ }
+ try {
+ return Long.decode(str);
+ } catch (final NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
}
diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java b/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java
index e705f0be..e9fc1bdf 100644
--- a/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java
+++ b/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java
@@ -4,6 +4,11 @@ import java.io.File;
import org.apache.commons.lang3.SystemUtils;
+/**
+ * 关于系统设定,平台信息的变量
+ *
+ * @author calvin
+ */
public class Platforms {
// 文件路径分隔符
diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/Properties.java b/modules/utils/src/main/java/org/springside/modules/utils/base/Properties.java
new file mode 100644
index 00000000..58a686c0
--- /dev/null
+++ b/modules/utils/src/main/java/org/springside/modules/utils/base/Properties.java
@@ -0,0 +1,127 @@
+package org.springside.modules.utils.base;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * 关于Properties的工具类
+ *
+ * 1. Boolean.readBoolean(name) 不够用, 进行扩展. 其他Integer.read()等都没问题.
+ *
+ * 2. 简单的合并系统变量(-D),环境变量 和默认值,以系统变量优先,在未引入commons-config时使用.
+ *
+ * @author calvin
+ */
+public class Properties {
+
+ /////////// Boolean.readBoolean()扩展 ///////////////
+
+ /**
+ * 读取Boolean类型的系统变量,为空时返回null,而不是false.
+ */
+ public static Boolean readBoolean(String name) {
+ String stringResult = System.getProperty(name);
+ if (stringResult != null) {
+ return Boolean.valueOf(stringResult);
+ }
+ return null;
+ }
+
+ /**
+ * 读取Boolean类型的系统变量,为空时返回默认值,因为默认值为Boolean,返回的也是Boolean
+ */
+ public static Boolean readBoolean(String name, Boolean defaultVal) {
+ String stringResult = System.getProperty(name);
+ if (stringResult != null) {
+ return Boolean.parseBoolean(stringResult);
+ } else {
+ return defaultVal;
+ }
+ }
+
+ /**
+ * 读取Boolean类型的系统变量,为空时返回默认值。因为默认值为Boolean,返回的也是boolean
+ */
+ public static boolean readBoolean(String name, boolean defaultVal) {
+ String stringResult = System.getProperty(name);
+ if (stringResult != null) {
+ return Boolean.parseBoolean(stringResult);
+ } else {
+ return defaultVal;
+ }
+ }
+
+ /////////// 简单的合并系统变量(-D),环境变量 和默认值,以系统变量优先.///////////////
+
+ /**
+ * 合并系统变量(-D),环境变量 和默认值,以系统变量优先
+ */
+ public static String readString(String propertyName, String envName, String defaultValue) {
+ String propertyValue = System.getProperty(propertyName);
+ if (propertyValue != null) {
+ return propertyValue;
+ } else {
+ propertyValue = System.getenv(envName);
+ return propertyValue != null ? propertyValue : defaultValue;
+ }
+ }
+
+ /**
+ * 合并系统变量(-D),环境变量 和默认值,以系统变量优先
+ */
+ public static Integer readInt(String propertyName, String envName, Integer defaultValue) {
+ Integer propertyValue = Numbers.toIntObject(System.getProperty(propertyName));
+ if (propertyValue != null) {
+ return propertyValue;
+ } else {
+ propertyValue = Numbers.toIntObject(System.getenv(envName));
+ return propertyValue != null ? propertyValue : defaultValue;
+ }
+ }
+
+ /**
+ * 合并系统变量(-D),环境变量 和默认值,以系统变量优先
+ */
+ public static Long readLong(String propertyName, String envName, Long defaultValue) {
+ Long propertyValue = Numbers.toLongObject(System.getProperty(propertyName));
+ if (propertyValue != null) {
+ return propertyValue;
+ } else {
+ propertyValue = Numbers.toLongObject(System.getenv(envName));
+ return propertyValue != null ? propertyValue : defaultValue;
+ }
+ }
+
+ /**
+ * 合并系统变量(-D),环境变量 和默认值,以系统变量优先
+ */
+ public static Double readDouble(String propertyName, String envName, Double defaultValue) {
+ Double propertyValue = Numbers.toDoubleObject(System.getProperty(propertyName));
+ if (propertyValue != null) {
+ return propertyValue;
+ } else {
+ propertyValue = Numbers.toDoubleObject(System.getenv(envName));
+ return propertyValue != null ? propertyValue : defaultValue;
+ }
+ }
+
+ /**
+ * 合并系统变量(-D),环境变量 和默认值,以系统变量优先
+ */
+ public static Boolean readBoolean(String propertyName, String envName, Boolean defaultValue) {
+ Boolean propertyValue = toBooleanObject(System.getProperty(propertyName));
+ if (propertyValue != null) {
+ return propertyValue;
+ } else {
+ propertyValue = toBooleanObject(System.getenv(envName));
+ return propertyValue != null ? propertyValue : defaultValue;
+ }
+ }
+
+ private static Boolean toBooleanObject(String str) {
+ if (str == null) {
+ return null;
+ } else {
+ return Boolean.valueOf(str);
+ }
+ }
+}
diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/IntObjectHashMap.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/IntObjectHashMap.java
index ae1c809f..4d055789 100644
--- a/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/IntObjectHashMap.java
+++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/IntObjectHashMap.java
@@ -13,22 +13,6 @@
* the License.
*/
package org.springside.modules.utils.collection.primitive;
-/*
- * Copyright 2014 The Netty Project
- *
- * The Netty Project licenses this file to you 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.
- */
-import org.springside.modules.utils.math.Maths;
-
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Arrays;
@@ -38,6 +22,8 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
+import org.springside.modules.utils.base.Maths;
+
/**
* 移植Netty 4.1.6的Key为原子类型的集合类, 在数据结构上与HashMap不一样,空间占用与性能俱比原来更优
*
@@ -100,7 +86,7 @@ public class IntObjectHashMap implements IntObjectMap {
this.loadFactor = loadFactor;
// Adjust the initial capacity if necessary.
- int capacity = Maths.safeFindNextPositivePowerOfTwo(initialCapacity);
+ int capacity = Maths.findNextPositivePowerOfTwo(initialCapacity);
mask = capacity - 1;
// Allocate the arrays.
diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectHashMap.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectHashMap.java
index e151b5d8..d15c8567 100644
--- a/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectHashMap.java
+++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectHashMap.java
@@ -1,7 +1,19 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project licenses this file to you 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 org.springside.modules.utils.collection.primitive;
-import org.springside.modules.utils.math.Maths;
-
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Arrays;
@@ -11,6 +23,8 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
+import org.springside.modules.utils.base.Maths;
+
/**
* 移植Netty 4.1.6的Key为原子类型的集合类, 在数据结构上与HashMap不一样,空间占用与性能俱比原来更优
*
@@ -73,7 +87,7 @@ public class LongObjectHashMap implements LongObjectMap {
this.loadFactor = loadFactor;
// Adjust the initial capacity if necessary.
- int capacity = Maths.safeFindNextPositivePowerOfTwo(initialCapacity);
+ int capacity = Maths.findNextPositivePowerOfTwo(initialCapacity);
mask = capacity - 1;
// Allocate the arrays.
diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectMap.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectMap.java
index 68842045..4885f29c 100644
--- a/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectMap.java
+++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/primitive/LongObjectMap.java
@@ -14,11 +14,8 @@
*/
package org.springside.modules.utils.collection.primitive;
-
import java.util.Map;
-
-
/**
* Interface for a primitive map that uses {@code long}s as keys.
*