添加 StringTools#isNotBlank

dev
ZhouXY108 2024-10-09 18:46:18 +08:00
parent 5e450a9bdb
commit 304dccc658
8 changed files with 237 additions and 31 deletions

13
pom.xml
View File

@ -25,12 +25,6 @@
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
@ -40,6 +34,13 @@
<!-- Test dependencies --> <!-- Test dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>

View File

@ -17,8 +17,7 @@
package xyz.zhouxy.plusone.commons.base; package xyz.zhouxy.plusone.commons.base;
import org.apache.commons.lang3.StringUtils; import xyz.zhouxy.plusone.commons.util.StringTools;
import org.apache.commons.lang3.reflect.MethodUtils;
/** /**
* JRE version * JRE version
@ -35,23 +34,30 @@ public class JRE {
private static int getJre() { private static int getJre() {
String version = System.getProperty("java.version"); String version = System.getProperty("java.version");
boolean isBlank = StringUtils.isBlank(version); boolean isNotBlank = StringTools.isNotBlank(version);
if (!isBlank && version.startsWith("1.8")) { if (isNotBlank && version.startsWith("1.8")) {
return JAVA_8; return JAVA_8;
} }
// if JRE version is 9 or above, we can get version from // if JRE version is 9 or above, we can get version from
// java.lang.Runtime.version() // java.lang.Runtime.version()
try { try {
Object javaRunTimeVersion = MethodUtils.invokeMethod(Runtime.getRuntime(), "version"); return getMajorVersion(version);
return (int) MethodUtils.invokeMethod(javaRunTimeVersion, "major");
} catch (Exception e) { } catch (Exception e) {
// Can't determine current JRE version (maybe java.version is null),
// assuming that JRE version is 8. // assuming that JRE version is 8.
} }
// default java 8 // default java 8
return JAVA_8; return JAVA_8;
} }
private static int getMajorVersion(String version) {
if (version.startsWith("1.")) {
return Integer.parseInt(version.substring(2, 3));
} else {
int dotIndex = version.indexOf(".");
return (dotIndex != -1) ? Integer.parseInt(version.substring(0, dotIndex)) : Integer.parseInt(version);
}
}
private JRE() { private JRE() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

View File

@ -23,12 +23,11 @@ import java.util.stream.Collectors;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import xyz.zhouxy.plusone.commons.annotation.Virtual; import xyz.zhouxy.plusone.commons.annotation.Virtual;
import xyz.zhouxy.plusone.commons.util.StringTools;
/** /**
* *
@ -57,7 +56,7 @@ public class PagingAndSortingQueryParams {
Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(), Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
"Sortable properties can not be empty."); "Sortable properties can not be empty.");
sortableProperties.forEach((k, v) -> sortableProperties.forEach((k, v) ->
Preconditions.checkArgument(StringUtils.isNotBlank(k) && StringUtils.isNotBlank(v), Preconditions.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
"Property name must not be blank.")); "Property name must not be blank."));
this.sortableProperties = ImmutableMap.copyOf(sortableProperties); this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
} }

View File

@ -1,11 +1,10 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
public class BigDecimals { public class BigDecimals {
@ -37,7 +36,171 @@ public class BigDecimals {
} }
public static BigDecimal of(final String val) { public static BigDecimal of(final String val) {
return (StringUtils.isBlank(val)) ? ZERO : new BigDecimal(val); return (StringTools.isNotBlank(val)) ? new BigDecimal(val) : ZERO;
}
/**
*
*/
private static final int DEF_DIV_SCALE = 10;
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double add(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.add(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double subtract(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.subtract(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double multiply(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.multiply(b2).doubleValue();
}
/**
*
* 10
*
* @param v1
* @param v2
* @return
*/
public static double divide(double v1, double v2) {
return divide(v1, v2, DEF_DIV_SCALE);
}
/**
* scale
*
*
* @param v1
* @param v2
* @param scale
* @return
*/
public static double divide(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.divide(b2, scale, RoundingMode.HALF_EVEN).doubleValue();
}
/**
*
*
* @param v
* @param scale
* @return
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = BigDecimal.valueOf(v);
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
}
/**
* (Float)
*
* @param v
* @return
*/
public static float convertToFloat(double v) {
BigDecimal b = BigDecimal.valueOf(v);
return b.floatValue();
}
/**
* (Int)
*
* @param v
* @return
*/
public static int convertsToInt(double v) {
BigDecimal b = BigDecimal.valueOf(v);
return b.intValue();
}
/**
* (Long)
*
* @param v
* @return
*/
public static long convertsToLong(double v) {
BigDecimal b = BigDecimal.valueOf(v);
return b.longValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double returnMax(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.max(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double returnMin(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.min(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return 01-1
*/
public static int compareTo(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.compareTo(b2);
} }
private BigDecimals() { private BigDecimals() {

View File

@ -24,8 +24,6 @@ import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
@ -45,7 +43,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
protected final String name; protected final String name;
protected Enumeration(final int id, final String name) { protected Enumeration(final int id, final String name) {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name of enumeration must has text."); Preconditions.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
this.id = id; this.id = id;
this.name = name; this.name = name;
} }

View File

@ -0,0 +1,20 @@
package xyz.zhouxy.plusone.commons.util;
public class StringTools {
public static boolean isNotBlank(final String cs) {
if (cs == null || cs.isEmpty()) {
return false;
}
for (int i = 0; i < cs.length(); i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return true;
}
}
return false;
}
private StringTools() {
throw new IllegalStateException("Utility class");
}
}

View File

@ -6,6 +6,7 @@ import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -55,7 +56,6 @@ class DateTimeToolsTests {
log.info("jodaDateTime: {}", jodaDateTime); log.info("jodaDateTime: {}", jodaDateTime);
assertEquals("2008-01-08 10:23:50.108", f.print(jodaDateTime)); assertEquals("2008-01-08 10:23:50.108", f.print(jodaDateTime));
jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+02:00")); jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+02:00"));
log.info("jodaDateTime: {}", jodaDateTime); log.info("jodaDateTime: {}", jodaDateTime);
assertEquals("2008-01-08 04:23:50.108", f.print(jodaDateTime)); assertEquals("2008-01-08 04:23:50.108", f.print(jodaDateTime));
@ -65,8 +65,8 @@ class DateTimeToolsTests {
void test() { void test() {
java.time.Instant now = java.time.Instant.now(); java.time.Instant now = java.time.Instant.now();
org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York")); org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York"));
org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat
org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); .forPattern("yyyy-MM-dd HH:mm:ss.SSS");
log.info(formatter.print(jodaDateTime)); log.info(formatter.print(jodaDateTime));
log.info(jodaDateTime.getZone().toString()); log.info(jodaDateTime.getZone().toString());
log.info(jodaDateTime.toString()); log.info(jodaDateTime.toString());
@ -74,7 +74,8 @@ class DateTimeToolsTests {
org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000); org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000);
log.info(instant.toString()); log.info(instant.toString());
log.info(DateTimeTools.toJavaInstant(instant).toString()); log.info(DateTimeTools.toJavaInstant(instant).toString());
log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York")).toString()); log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York"))
.toString());
} }
@Test @Test
@ -84,5 +85,10 @@ class DateTimeToolsTests {
org.joda.time.Instant jodaInstant = DateTimeTools.toJodaInstant(javaInstant); org.joda.time.Instant jodaInstant = DateTimeTools.toJodaInstant(javaInstant);
log.info("jodaInstant: {}", jodaInstant); log.info("jodaInstant: {}", jodaInstant);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTimeFormatter formatter2 = formatter.withZone(ZoneId.systemDefault());
log.info("{}", formatter);
log.info("{}", formatter2);
} }
} }

View File

@ -1,7 +1,9 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -10,6 +12,7 @@ import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.Gson; import com.google.gson.Gson;
import cn.hutool.core.util.ObjectUtil;
import lombok.ToString; import lombok.ToString;
class TreeBuilderTests { class TreeBuilderTests {
@ -37,11 +40,15 @@ class TreeBuilderTests {
/**//**/MenuItem.of("C1", "C1002", "三级菜单C1002", "/c/c1/c1002", 2), /**//**/MenuItem.of("C1", "C1002", "三级菜单C1002", "/c/c1/c1002", 2),
/**/MenuItem.of("C", "C2", "二级菜单C2", "/c/c2", 1)); /**/MenuItem.of("C", "C2", "二级菜单C2", "/c/c2", 1));
List<Menu> menuTreeSortedByOrderNum = treeBuilder.buildTree(menus); List<Menu> clonedMenus;
clonedMenus = menus.stream().map(m -> ObjectUtil.clone(m)).collect(Collectors.toList());
List<Menu> menuTreeSortedByOrderNum = treeBuilder.buildTree(clonedMenus);
log.info("menuTreeSortedByOrderNum: {}", new Gson().toJson(menuTreeSortedByOrderNum)); log.info("menuTreeSortedByOrderNum: {}", new Gson().toJson(menuTreeSortedByOrderNum));
clonedMenus = menus.stream().map(m -> ObjectUtil.clone(m)).collect(Collectors.toList());
List<Menu> menuTreeSortedByMenuCode = treeBuilder.buildTree( List<Menu> menuTreeSortedByMenuCode = treeBuilder.buildTree(
menus, clonedMenus,
(a, b) -> a.getMenuCode().compareTo(b.getMenuCode()) (a, b) -> a.getMenuCode().compareTo(b.getMenuCode())
); );
log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode)); log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode));
@ -49,7 +56,7 @@ class TreeBuilderTests {
} }
@ToString @ToString
abstract class Menu { abstract class Menu implements Serializable {
protected final String parentMenuCode; protected final String parentMenuCode;
protected final String menuCode; protected final String menuCode;
protected final String title; protected final String title;
@ -77,6 +84,8 @@ abstract class Menu {
public int getOrderNum() { public int getOrderNum() {
return orderNum; return orderNum;
} }
private static final long serialVersionUID = 20240917181424L;
} }
@ToString(callSuper = true) @ToString(callSuper = true)
@ -100,6 +109,8 @@ class MenuItem extends Menu {
public String getUrl() { public String getUrl() {
return url; return url;
} }
private static final long serialVersionUID = 20240917181910L;
} }
@ToString(callSuper = true) @ToString(callSuper = true)
@ -135,4 +146,6 @@ class MenuList extends Menu {
} }
this.children.add(child); this.children.add(child);
} }
private static final long serialVersionUID = 20240917181917L;
} }