mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
0b960768d8
commit
29bb4773b4
@ -311,17 +311,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> boolean hasEmptyVarargs(final T... args) {
|
||||
if (Arrays.stream(args).anyMatch(en -> isArray(en) || en instanceof Iterator || en instanceof Map)) {
|
||||
throw new IllegalArgumentException("request that input parameters cannot be arrays or collection or map!");
|
||||
}
|
||||
if (isNotEmpty(args)) {
|
||||
for (final T element : args) {
|
||||
if (ObjUtil.isEmpty(element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return hasEmpty(args);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -356,15 +346,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> boolean isAllEmptyVarargs(final T... args) {
|
||||
if (Arrays.stream(args).anyMatch(en -> isArray(en) || en instanceof Iterator || en instanceof Map)) {
|
||||
throw new IllegalArgumentException("request that input parameters cannot be arrays or collection or map!");
|
||||
}
|
||||
for (final T obj : args) {
|
||||
if (!ObjUtil.isEmpty(obj)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return isAllEmpty(args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,7 @@ import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.collection.iter.ArrayIter;
|
||||
import org.dromara.hutool.core.collection.iter.IterUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
@ -255,19 +256,19 @@ public class MapUtil extends MapGetUtil {
|
||||
public static <K, V> Map<K, V> createMap(final Class<?> mapType, final Supplier<Map<K, V>> defaultMap) {
|
||||
Map<K, V> result = null;
|
||||
if (null != mapType && !mapType.isAssignableFrom(AbstractMap.class)) {
|
||||
try{
|
||||
try {
|
||||
result = (Map<K, V>) ConstructorUtil.newInstanceIfPossible(mapType);
|
||||
} catch (final Exception ignore){
|
||||
} catch (final Exception ignore) {
|
||||
// JDK9+抛出java.lang.reflect.InaccessibleObjectException
|
||||
// 跳过
|
||||
}
|
||||
}
|
||||
|
||||
if(null == result){
|
||||
if (null == result) {
|
||||
result = defaultMap.get();
|
||||
}
|
||||
|
||||
if(!result.isEmpty()){
|
||||
if (!result.isEmpty()) {
|
||||
// issue#3162@Github,在构造中put值,会导致新建map带有值内容,此处清空
|
||||
result.clear();
|
||||
}
|
||||
@ -313,30 +314,33 @@ public class MapUtil extends MapGetUtil {
|
||||
* <p>偶数参数必须为value,可以为任意类型。</p>
|
||||
*
|
||||
* <pre>
|
||||
* LinkedHashMap map = MapUtil.of(true,
|
||||
* LinkedHashMap map = MapUtil.ofKvs(false,
|
||||
* "RED", "#FF0000",
|
||||
* "GREEN", "#00FF00",
|
||||
* "BLUE", "#0000FF"
|
||||
* );
|
||||
* </pre>
|
||||
* @see Dict#ofKvs(Object...)
|
||||
*
|
||||
* @param isLinked 是否使用{@link LinkedHashMap}
|
||||
* @param keysAndValues 键值对列表,必须奇数参数为key,偶数参数为value
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @return LinkedHashMap
|
||||
* @see Dict#ofKvs(Object...)
|
||||
* @see Dict#ofKvs(Object...)
|
||||
* @since 6.0.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <K, V> LinkedHashMap<K, V> ofKvs(final Object... keysAndValues) {
|
||||
final int initialCapacity = (int) (DEFAULT_INITIAL_CAPACITY / DEFAULT_LOAD_FACTOR) + 1;
|
||||
LinkedHashMap<K, V> map = new LinkedHashMap<>(initialCapacity);
|
||||
public static <K, V> Map<K, V> ofKvs(final boolean isLinked, final Object... keysAndValues) {
|
||||
if (ArrayUtil.isEmpty(keysAndValues)) {
|
||||
return newHashMap(0, isLinked);
|
||||
}
|
||||
|
||||
Object key = null;
|
||||
for (int i = 0; i < keysAndValues.length; i++) {
|
||||
// 偶数
|
||||
if ((i & 1) == 0) {
|
||||
key = keysAndValues[i];
|
||||
} else {
|
||||
map.put((K) key, (V) keysAndValues[i]);
|
||||
}
|
||||
Assert.isTrue(keysAndValues.length % 2 == 0, "keysAndValues not in pairs!");
|
||||
|
||||
final Map<K, V> map = newHashMap(keysAndValues.length / 2, isLinked);
|
||||
for (int i = 0; i < keysAndValues.length; i += 2) {
|
||||
map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
@ -707,7 +711,7 @@ public class MapUtil extends MapGetUtil {
|
||||
return map;
|
||||
}
|
||||
|
||||
final Map<K, V> map2 = createMap(map.getClass(), ()-> new HashMap<>(map.size(), 1f));
|
||||
final Map<K, V> map2 = createMap(map.getClass(), () -> new HashMap<>(map.size(), 1f));
|
||||
if (isEmpty(map)) {
|
||||
return map2;
|
||||
}
|
||||
@ -723,7 +727,6 @@ public class MapUtil extends MapGetUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 过滤<br>
|
||||
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Filter实现可以实现以下功能:
|
||||
@ -782,7 +785,7 @@ public class MapUtil extends MapGetUtil {
|
||||
return map;
|
||||
}
|
||||
|
||||
final Map<K, V> map2 = createMap(map.getClass(), ()-> new HashMap<>(map.size(), 1f));
|
||||
final Map<K, V> map2 = createMap(map.getClass(), () -> new HashMap<>(map.size(), 1f));
|
||||
if (isEmpty(map)) {
|
||||
return map2;
|
||||
}
|
||||
|
@ -30,11 +30,6 @@ public class JdkUtil {
|
||||
* 由于Hutool基于JDK8编译,当使用JDK版本低于8时,不支持。
|
||||
*/
|
||||
public static final boolean IS_JDK8;
|
||||
/**
|
||||
* 是否大于JDK8<br>
|
||||
* 由于大于jdk8的部分功能,jdk有变化,需要做版本判断
|
||||
*/
|
||||
public static final boolean IS_GT_JDK8;
|
||||
/**
|
||||
* 是否大于等于JDK17
|
||||
*/
|
||||
@ -49,7 +44,6 @@ public class JdkUtil {
|
||||
// JVM版本
|
||||
JVM_VERSION = _getJvmVersion();
|
||||
IS_JDK8 = 8 == JVM_VERSION;
|
||||
IS_GT_JDK8 = JVM_VERSION > 8;
|
||||
IS_AT_LEAST_JDK17 = JVM_VERSION >= 17;
|
||||
|
||||
// JVM名称
|
||||
|
@ -36,6 +36,7 @@ import java.util.function.Supplier;
|
||||
* 对象工具类,包括判空、克隆、序列化等操作
|
||||
* <br>
|
||||
* 原数组相关操作见:{@link ArrayUtil#hasBlank(CharSequence...)}、{@link ArrayUtil#isAllBlank(CharSequence...)}等等
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class ObjUtil {
|
||||
@ -363,19 +364,19 @@ public class ObjUtil {
|
||||
*/
|
||||
public static <T> T clone(final T obj) {
|
||||
final T result = ArrayUtil.clone(obj);
|
||||
if(null != result){
|
||||
if (null != result) {
|
||||
// 数组
|
||||
return result;
|
||||
}
|
||||
|
||||
if (obj instanceof Cloneable) {
|
||||
try{
|
||||
try {
|
||||
return MethodUtil.invoke(obj, "clone");
|
||||
} catch (final HutoolException e){
|
||||
if(e.getCause() instanceof IllegalAccessException){
|
||||
} catch (final HutoolException e) {
|
||||
if (e.getCause() instanceof IllegalAccessException) {
|
||||
// JDK9+下可能无权限
|
||||
return cloneByStream(obj);
|
||||
}else {
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
@ -26,29 +26,29 @@ public class CoordinateUtilTest {
|
||||
@Test
|
||||
public void wgs84ToGcj02Test() {
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToGcj02(116.404, 39.915);
|
||||
Assertions.assertEquals(116.41024449916938D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(39.91640428150164D, coordinate.getLat(), 0);
|
||||
Assertions.assertEquals(116.41024449916938D, coordinate.getLng(), 0.00000000000001D);
|
||||
Assertions.assertEquals(39.91640428150164D, coordinate.getLat(), 0.00000000000001D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gcj02ToWgs84Test() {
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.gcj02ToWgs84(116.404, 39.915);
|
||||
Assertions.assertEquals(116.39775550083061D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(39.91359571849836D, coordinate.getLat(), 0);
|
||||
Assertions.assertEquals(116.39775550083061D, coordinate.getLng(), 0.00000000000001D);
|
||||
Assertions.assertEquals(39.91359571849836D, coordinate.getLat(), 0.00000000000001D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wgs84toBd09Test() {
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToBd09(116.404, 39.915);
|
||||
Assertions.assertEquals(116.41662724378733D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(39.922699552216216D, coordinate.getLat(), 0);
|
||||
Assertions.assertEquals(116.41662724378733D, coordinate.getLng(), 0.00000000000001D);
|
||||
Assertions.assertEquals(39.922699552216216D, coordinate.getLat(), 0.00000000000001D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wgs84toBd09Test2() {
|
||||
// https://tool.lu/coordinate/
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToBd09(122.99395597D, 44.99804071D);
|
||||
Assertions.assertEquals(123.00636516028885D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(123.00636516028885D, coordinate.getLng(), 0.00000000000001D);
|
||||
// 不同jdk版本、不同架构jdk, 精度有差异,数值不完全相等,这里增加精度控制delta
|
||||
// 参考:从Java Math底层实现看Arm与x86的差异:https://yikun.github.io/2020/04/10/%E4%BB%8EJava-Math%E5%BA%95%E5%B1%82%E5%AE%9E%E7%8E%B0%E7%9C%8BArm%E4%B8%8Ex86%E7%9A%84%E5%B7%AE%E5%BC%82/
|
||||
Assertions.assertEquals(45.00636909189589D, coordinate.getLat(), 0.00000000000001D);
|
||||
@ -57,22 +57,22 @@ public class CoordinateUtilTest {
|
||||
@Test
|
||||
public void bd09toWgs84Test() {
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.bd09toWgs84(116.404, 39.915);
|
||||
Assertions.assertEquals(116.3913836995125D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(39.907253214522164D, coordinate.getLat(), 0);
|
||||
Assertions.assertEquals(116.3913836995125D, coordinate.getLng(), 0.00000000000001D);
|
||||
Assertions.assertEquals(39.907253214522164D, coordinate.getLat(), 0.00000000000001D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gcj02ToBd09Test() {
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.gcj02ToBd09(116.404, 39.915);
|
||||
Assertions.assertEquals(116.41036949371029D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(39.92133699351022D, coordinate.getLat(), 0);
|
||||
Assertions.assertEquals(116.41036949371029D, coordinate.getLng(), 0.00000000000001D);
|
||||
Assertions.assertEquals(39.92133699351022D, coordinate.getLat(), 0.00000000000001D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bd09toGcj02Test() {
|
||||
final CoordinateUtil.Coordinate coordinate = CoordinateUtil.bd09ToGcj02(116.404, 39.915);
|
||||
Assertions.assertEquals(116.39762729119315D, coordinate.getLng(), 0);
|
||||
Assertions.assertEquals(39.90865673957631D, coordinate.getLat(), 0);
|
||||
Assertions.assertEquals(116.39762729119315D, coordinate.getLng(), 0.00000000000001D);
|
||||
Assertions.assertEquals(39.90865673957631D, coordinate.getLat(), 0.00000000000001D);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -72,10 +72,10 @@ public class MapUtilTest {
|
||||
void mapTest() {
|
||||
// Add test like a foreigner
|
||||
final Map<Integer, String> adjectivesMap = MapUtil.<Integer, String>builder()
|
||||
.put(0, "lovely")
|
||||
.put(1, "friendly")
|
||||
.put(2, "happily")
|
||||
.build();
|
||||
.put(0, "lovely")
|
||||
.put(1, "friendly")
|
||||
.put(2, "happily")
|
||||
.build();
|
||||
|
||||
final Map<Integer, String> resultMap = MapUtil.map(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase());
|
||||
|
||||
@ -199,11 +199,11 @@ public class MapUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortJoinTest(){
|
||||
void sortJoinTest() {
|
||||
final Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||
.put("key1", "value1")
|
||||
.put("key3", "value3")
|
||||
.put("key2", "value2").build();
|
||||
.put("key1", "value1")
|
||||
.put("key3", "value3")
|
||||
.put("key2", "value2").build();
|
||||
|
||||
final String join1 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false);
|
||||
Assertions.assertEquals("key1value1key2value2key3value3", join1);
|
||||
@ -216,7 +216,7 @@ public class MapUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void ofEntriesTest(){
|
||||
void ofEntriesTest() {
|
||||
final Map<String, Integer> map = MapUtil.ofEntries(MapUtil.entry("a", 1), MapUtil.entry("b", 2));
|
||||
Assertions.assertEquals(2, map.size());
|
||||
|
||||
@ -225,8 +225,8 @@ public class MapUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getIntTest(){
|
||||
Assertions.assertThrows(NumberFormatException.class, ()->{
|
||||
void getIntTest() {
|
||||
Assertions.assertThrows(NumberFormatException.class, () -> {
|
||||
final Map<String, String> map = MapUtil.ofEntries(MapUtil.entry("a", "d"));
|
||||
final Integer a = MapUtil.getInt(map, "a");
|
||||
Assertions.assertNotNull(a);
|
||||
@ -234,7 +234,7 @@ public class MapUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getIntValueTest(){
|
||||
void getIntValueTest() {
|
||||
final Map<String, String> map = MapUtil.ofEntries(MapUtil.entry("a", "1"), MapUtil.entry("b", null));
|
||||
final int a = MapUtil.get(map, "a", int.class);
|
||||
Assertions.assertEquals(1, a);
|
||||
@ -270,9 +270,9 @@ public class MapUtilTest {
|
||||
@Test
|
||||
void computeIfAbsentForJdk8Test() {
|
||||
// https://github.com/apache/dubbo/issues/11986
|
||||
final ConcurrentHashMap<String,Integer> map=new ConcurrentHashMap<>();
|
||||
final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
|
||||
// // map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->42));
|
||||
MapUtil.computeIfAbsentForJdk8(map, "AaAa", key->map.computeIfAbsent("BBBB",key2->42));
|
||||
MapUtil.computeIfAbsentForJdk8(map, "AaAa", key -> map.computeIfAbsent("BBBB", key2 -> 42));
|
||||
|
||||
Assertions.assertEquals(2, map.size());
|
||||
Assertions.assertEquals(Integer.valueOf(42), map.get("AaAa"));
|
||||
@ -303,11 +303,13 @@ public class MapUtilTest {
|
||||
public void issue3162Test() {
|
||||
final Map<String, Object> map = new HashMap<String, Object>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
put("a", "1");
|
||||
put("b", "2");
|
||||
put("c", "3");
|
||||
}};
|
||||
}
|
||||
};
|
||||
final Map<String, Object> filtered = MapUtil.filter(map, "a", "b");
|
||||
Assertions.assertEquals(2, filtered.size());
|
||||
Assertions.assertEquals("1", filtered.get("a"));
|
||||
@ -316,7 +318,7 @@ public class MapUtilTest {
|
||||
|
||||
@Test
|
||||
public void ofKvsLinkTest() {
|
||||
LinkedHashMap<String,Long> map2 = MapUtil.ofKvs(
|
||||
final Map<String, Long> map2 = MapUtil.ofKvs(true,
|
||||
"RED", 0xFF0000,
|
||||
"GREEN", 0x00FF00,
|
||||
"BLUE", 0x0000FF);
|
||||
|
Loading…
x
Reference in New Issue
Block a user