From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001 From: 田源 <tianyuan@vci-tech.com> Date: 星期四, 03 四月 2025 14:35:02 +0800 Subject: [PATCH] 添加非密字段显示 --- Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 210 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java new file mode 100644 index 0000000..3977967 --- /dev/null +++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2018-2028, DreamLu All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com) + */ +package org.springblade.core.tool.utils; + + +import org.springframework.lang.Nullable; + +/** + * 鏁板瓧绫诲瀷宸ュ叿绫� + * + * @author L.cm + */ +public class NumberUtil extends org.springframework.util.NumberUtils { + + //----------------------------------------------------------------------- + + /** + * <p>Convert a <code>String</code> to an <code>int</code>, returning + * <code>zero</code> if the conversion fails.</p> + * + * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> + * + * <pre> + * NumberUtil.toInt(null) = 0 + * NumberUtil.toInt("") = 0 + * NumberUtil.toInt("1") = 1 + * </pre> + * + * @param str the string to convert, may be null + * @return the int represented by the string, or <code>zero</code> if + * conversion fails + */ + public static int toInt(final String str) { + return toInt(str, -1); + } + + /** + * <p>Convert a <code>String</code> to an <code>int</code>, returning a + * default value if the conversion fails.</p> + * + * <p>If the string is <code>null</code>, the default value is returned.</p> + * + * <pre> + * NumberUtil.toInt(null, 1) = 1 + * NumberUtil.toInt("", 1) = 1 + * NumberUtil.toInt("1", 0) = 1 + * </pre> + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the int represented by the string, or the default if conversion fails + */ + public static int toInt(@Nullable final String str, final int defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Integer.valueOf(str); + } catch (final NumberFormatException nfe) { + return defaultValue; + } + } + + /** + * <p>Convert a <code>String</code> to a <code>long</code>, returning + * <code>zero</code> if the conversion fails.</p> + * + * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> + * + * <pre> + * NumberUtil.toLong(null) = 0L + * NumberUtil.toLong("") = 0L + * NumberUtil.toLong("1") = 1L + * </pre> + * + * @param str the string to convert, may be null + * @return the long represented by the string, or <code>0</code> if + * conversion fails + */ + public static long toLong(final String str) { + return toLong(str, 0L); + } + + /** + * <p>Convert a <code>String</code> to a <code>long</code>, returning a + * default value if the conversion fails.</p> + * + * <p>If the string is <code>null</code>, the default value is returned.</p> + * + * <pre> + * NumberUtil.toLong(null, 1L) = 1L + * NumberUtil.toLong("", 1L) = 1L + * NumberUtil.toLong("1", 0L) = 1L + * </pre> + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the long represented by the string, or the default if conversion fails + */ + public static long toLong(@Nullable final String str, final long defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Long.valueOf(str); + } catch (final NumberFormatException nfe) { + return defaultValue; + } + } + + /** + * <p>Convert a <code>String</code> to a <code>Double</code> + * + * @param value value + * @return double value + */ + public static Double toDouble(String value) { + return toDouble(value, null); + } + + /** + * <p>Convert a <code>String</code> to a <code>Double</code> + * + * @param value value + * @param defaultValue 榛樿鍊� + * @return double value + */ + public static Double toDouble(@Nullable String value, Double defaultValue) { + if (value != null) { + return Double.valueOf(value.trim()); + } + return defaultValue; + } + + /** + * <p>Convert a <code>String</code> to a <code>Double</code> + * + * @param value value + * @return double value + */ + public static Float toFloat(String value) { + return toFloat(value, null); + } + + /** + * <p>Convert a <code>String</code> to a <code>Double</code> + * + * @param value value + * @param defaultValue 榛樿鍊� + * @return double value + */ + public static Float toFloat(@Nullable String value, Float defaultValue) { + if (value != null) { + return Float.valueOf(value.trim()); + } + return defaultValue; + } + + /** + * All possible chars for representing a number as a String + */ + private final static char[] DIGITS = { + '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f', 'g', 'h', + 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', + 'G', 'H', 'I', 'J', 'K', 'L', + 'M', 'N', 'O', 'P', 'Q', 'R', + 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z' + }; + + /** + * 灏� long 杞煭瀛楃涓� 涓� 62 杩涘埗 + * + * @param i 鏁板瓧 + * @return 鐭瓧绗︿覆 + */ + public static String to62String(long i) { + int radix = DIGITS.length; + char[] buf = new char[65]; + int charPos = 64; + i = -i; + while (i <= -radix) { + buf[charPos--] = DIGITS[(int) (-(i % radix))]; + i = i / radix; + } + buf[charPos] = DIGITS[(int) (-i)]; + + return new String(buf, charPos, (65 - charPos)); + } + +} -- Gitblit v1.9.3