mirror of https://github.com/longtai-cn/hippo4j
* fix : global remove hutool(#725) * fix : update use for hutool(#725) * fix : Replenish toolkit(#725) * fix : merge branch conflicts(#725) * fix : modify code format(#725) * fix : config BeanUtil merged to common(#725) * fix : add IdUtil for generate UUID(#725) * fix : replace method for convert bean(#725) Co-authored-by: pizihao <hao3073liu@163.com>pull/764/head
parent
50746f41fe
commit
e03d464053
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
import cn.hippo4j.common.web.exception.IllegalException;
|
||||
import com.github.dozermapper.core.DozerBeanMapperBuilder;
|
||||
import com.github.dozermapper.core.Mapper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Bean util.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class BeanUtil {
|
||||
|
||||
protected static Mapper BEAN_MAPPER_BUILDER;
|
||||
|
||||
static {
|
||||
BEAN_MAPPER_BUILDER = DozerBeanMapperBuilder.buildDefault();
|
||||
}
|
||||
|
||||
public static <T, S> T convert(S source, Class<T> clazz) {
|
||||
return Optional.ofNullable(source)
|
||||
.map(each -> BEAN_MAPPER_BUILDER.map(each, clazz))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public static <T, S> T convert(S source, T target) {
|
||||
Optional.ofNullable(source)
|
||||
.ifPresent(each -> BEAN_MAPPER_BUILDER.map(each, target));
|
||||
return target;
|
||||
}
|
||||
|
||||
public static <T, S> List<T> convert(List<S> sources, Class<T> clazz) {
|
||||
return Optional.ofNullable(sources)
|
||||
.map(each -> {
|
||||
List<T> targetList = new ArrayList<T>(each.size());
|
||||
each.forEach(item -> targetList.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
|
||||
return targetList;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public static <T, S> Set<T> convert(Set<S> sources, Class<T> clazz) {
|
||||
return Optional.ofNullable(sources)
|
||||
.map(each -> {
|
||||
Set<T> targetSize = new HashSet<T>(each.size());
|
||||
each.forEach(item -> targetSize.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
|
||||
return targetSize;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz, boolean toCamelCase) {
|
||||
if (clazz == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
T newInstance = clazz.newInstance();
|
||||
return mapToBean(map, newInstance, toCamelCase);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new IllegalException("do not create instance for " + clazz.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* map to bean
|
||||
*
|
||||
* @param map map
|
||||
* @param bean obj bean
|
||||
* @param toCamelCase format to camel case
|
||||
* @param <T> bean type
|
||||
* @return T
|
||||
*/
|
||||
public static <T> T mapToBean(Map<String, Object> map, T bean, boolean toCamelCase) {
|
||||
if (bean == null) {
|
||||
return null;
|
||||
}
|
||||
if (map.isEmpty()) {
|
||||
return bean;
|
||||
}
|
||||
Class<?> clazz = bean.getClass();
|
||||
map.forEach((s, o) -> {
|
||||
String name = toCamelCase ? StringUtil.toCamelCase(s, StringUtil.UNDERLINE) : s;
|
||||
Method method = setter(clazz, name);
|
||||
if (method != null) {
|
||||
ReflectUtil.invoke(bean, method, o);
|
||||
}
|
||||
});
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for properties
|
||||
*
|
||||
* @param o obj
|
||||
* @param propertiesName name
|
||||
* @return Method for get
|
||||
*/
|
||||
public static Method getter(Class<?> o, String propertiesName) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
PropertyDescriptor descriptor = new PropertyDescriptor(propertiesName, o);
|
||||
return descriptor.getReadMethod();
|
||||
} catch (IntrospectionException e) {
|
||||
throw new IllegalException("not find getter for" + propertiesName + "in" + o.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for properties
|
||||
*
|
||||
* @param o obj
|
||||
* @param propertiesName name
|
||||
* @return Method for set
|
||||
*/
|
||||
public static Method setter(Class<?> o, String propertiesName) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
PropertyDescriptor descriptor = new PropertyDescriptor(propertiesName, o);
|
||||
return descriptor.getWriteMethod();
|
||||
} catch (IntrospectionException e) {
|
||||
throw new IllegalException("not find setter for" + propertiesName + "in" + o.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Date;
|
||||
import java.util.SimpleTimeZone;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* date and time util
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class DateUtil {
|
||||
|
||||
/**
|
||||
* get time zone for this JVM
|
||||
*/
|
||||
private static final TimeZone TIME_ZONE = TimeZone.getDefault();
|
||||
|
||||
public static final String NORM_DATE_PATTERN = "yyyy-MM-dd";
|
||||
|
||||
public static final String NORM_TIME_PATTERN = "HH:mm:ss";
|
||||
|
||||
public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/**
|
||||
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
|
||||
* represented by this <tt>Date</tt> object.
|
||||
*
|
||||
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
|
||||
* represented by this date.
|
||||
*/
|
||||
public static long getTime(LocalDateTime date) {
|
||||
return getTime(date, TIME_ZONE.toZoneId());
|
||||
}
|
||||
|
||||
public static long getTime(LocalDateTime date, ZoneId zoneId) {
|
||||
return date.atZone(zoneId).toInstant().toEpochMilli();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* modify format to date
|
||||
*
|
||||
* @param date date
|
||||
* @param normTimePattern PATTERN
|
||||
* @return String
|
||||
*/
|
||||
public static String format(Date date, String normTimePattern) {
|
||||
SimpleDateFormat zoneFormat = new SimpleDateFormat(normTimePattern);
|
||||
return zoneFormat.format(date);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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 cn.hippo4j.common.web.exception;
|
||||
|
||||
/**
|
||||
* A generic exception to handle illegal operations
|
||||
*/
|
||||
public class IllegalException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 8247610319171014183L;
|
||||
|
||||
public IllegalException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IllegalException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public IllegalException(Throwable e) {
|
||||
super(e.getMessage(), e);
|
||||
}
|
||||
|
||||
public IllegalException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
public IllegalException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, throwable, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IdUtilTest {
|
||||
|
||||
@Test
|
||||
public void randomUUIDTest() {
|
||||
String randomUUID = IdUtil.randomUUID();
|
||||
Assert.assertNotNull(randomUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleUUIDTest() {
|
||||
String simpleUUID = IdUtil.simpleUUID();
|
||||
Assert.assertNotNull(simpleUUID);
|
||||
Assert.assertFalse(simpleUUID.contains("-"));
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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 cn.hippo4j.config.toolkit;
|
||||
|
||||
import com.github.dozermapper.core.DozerBeanMapperBuilder;
|
||||
import com.github.dozermapper.core.Mapper;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Bean util.
|
||||
*/
|
||||
public class BeanUtil {
|
||||
|
||||
private BeanUtil() {
|
||||
|
||||
}
|
||||
|
||||
protected static Mapper BEAN_MAPPER_BUILDER;
|
||||
|
||||
static {
|
||||
BEAN_MAPPER_BUILDER = DozerBeanMapperBuilder.buildDefault();
|
||||
}
|
||||
|
||||
public static <T, S> T convert(S source, Class<T> clazz) {
|
||||
return Optional.ofNullable(source)
|
||||
.map(each -> BEAN_MAPPER_BUILDER.map(each, clazz))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public static <T, S> List<T> convert(List<S> sources, Class<T> clazz) {
|
||||
return Optional.ofNullable(sources)
|
||||
.map(each -> {
|
||||
List<T> targetList = new ArrayList<T>(each.size());
|
||||
each.stream()
|
||||
.forEach(item -> targetList.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
|
||||
return targetList;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public static <T, S> Set<T> convert(Set<S> sources, Class<T> clazz) {
|
||||
return Optional.ofNullable(sources)
|
||||
.map(each -> {
|
||||
Set<T> targetSize = new HashSet<T>(each.size());
|
||||
each.stream()
|
||||
.forEach(item -> targetSize.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
|
||||
return targetSize;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue