/* * This program is free software; you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software * Foundation. * * You should have received a copy of the GNU Lesser General Public License along with this * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html * or from the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * Copyright (c) 2015 Desheng (Dennis) Kang. All rights reserved. */ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.StringTokenizer; public class Utl { private static double DOUBLE_COMPARE_DELTA = 0.000000001; public static void check(boolean condition, String errMsg) throws NFException { if (condition) throw new NFException(errMsg); } public static void check(boolean condition, String errMsg, ServiceResult result) throws NFException { if (condition) { result.setRetCode(ServiceResult.FAILED_CODE); result.setRetMsg(errMsg); throw new NFException(errMsg); } } public static void check(boolean condition, int errCode, String errMsg, ServiceResult result) throws NFException { if (condition) { result.setRetCode(errCode); result.setRetMsg(errMsg); throw new NFException(errMsg); } } public static String stackTrack(Exception e) { ByteArrayOutputStream os = new ByteArrayOutputStream(1000); e.printStackTrace(new PrintStream(os)); return os.toString(); } public static int toInt(String value) { return Integer.parseInt(value); } public static int toInt(String value, int defaultValue) { if (value == null) return defaultValue; else return Integer.parseInt(value); } public static long toLong(String value) { return Long.parseLong(value); } public static long toLong(String value, long defaultValue) { if (value == null) return defaultValue; else return toLong(value); } public static long[] toLongArray(String value, long[] defaultValue) { if (value == null) return defaultValue; else return toLongArray(value); } public static long[] toLongArray(String value) { ArrayList<Long> values = new ArrayList<Long>(); StringTokenizer st = new StringTokenizer(value, ","); while(st.hasMoreTokens()) { String lvStr = st.nextToken(); Long lv = Long.parseLong(lvStr); values.add(lv); } long[] retL = new long[values.size()]; int i = 0; for (Long lv : values) { retL[i++] = lv.longValue(); } return retL; } public static double toDouble(String value) { return Double.parseDouble(value); } public static double toDouble(String value, double defaultValue) { if (value == null) return 0.0; else return Double.parseDouble(value); } /** * Compare two double variables under a threshold value. If absolute value of the difference is * smaller than default threshold, the two values are considered to be identical. This is to * eliminate very small difference due to double value precision * @param a * @param b * @return 0 if a = b, 1 if a > b, -1 if a < b */ public static int compare(double a, double b) { double delta = a - b; double absDelta = Math.abs(delta); if (a == 0) { if (absDelta < DOUBLE_COMPARE_DELTA) return 0; else return (delta > 0? 1 : -1); } else { double relativeDelta = absDelta / Math.abs(a); if (relativeDelta < DOUBLE_COMPARE_DELTA) return 0; else return (delta > 0? 1 : -1); } } public static boolean gt(double a, double b) { return compare(a, b) > 0? true : false; } public static boolean ge(double a, double b) { return compare(a, b) >= 0? true : false; } public static boolean eq(double a, double b) { return compare(a, b) == 0? true : false; } public static boolean lt(double a, double b) { return compare(a, b) < 0? true : false; } public static boolean le(double a, double b) { return compare(a, b) <= 0? true : false; } public static <T> Method getMethod(Class<T> c, String methodName) { Method m = null; try { m = c.getMethod(methodName, (Class<?>[])null); } catch (NoSuchMethodException | SecurityException e) {} return m; } }
实用工具类
猜你喜欢
转载自zhongmin2012.iteye.com/blog/2312400
今日推荐
周排行