mirror of https://github.com/longtai-cn/hippo4j
fix : Replenish toolkit(#725)
parent
75cc66ec02
commit
2b99a47bcb
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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 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.Map;
|
||||
|
||||
/**
|
||||
* Bean util.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class BeanUtil {
|
||||
|
||||
/**
|
||||
* copyProperties
|
||||
*
|
||||
* @param source source obj
|
||||
* @param target target obj
|
||||
* @param <T> target type
|
||||
* @param <S> source type
|
||||
* @return T
|
||||
*/
|
||||
public static <T, S> T copyProperties(S source, T target) {
|
||||
return copyProperties(source, target, (String) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* copyProperties
|
||||
*
|
||||
* @param source source obj
|
||||
* @param target target obj
|
||||
* @param ignoreProperties ignore name
|
||||
* @param <T> target type
|
||||
* @param <S> source type
|
||||
* @return T
|
||||
*/
|
||||
public static <T, S> T copyProperties(S source, T target, String... ignoreProperties) {
|
||||
BeanUtils.copyProperties(source, target, ignoreProperties);
|
||||
return target;
|
||||
}
|
||||
|
||||
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.toUnderlineCase(s) : 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,73 @@
|
||||
/*
|
||||
* 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.Getter;
|
||||
import lombok.Setter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BeanUtilTest {
|
||||
|
||||
@Test
|
||||
public void testMapToBean() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", "Test");
|
||||
map.put("status", 12);
|
||||
Customer customer = BeanUtil.mapToBean(map, Customer.class, true);
|
||||
Assert.assertEquals("Test", customer.getName());
|
||||
Assert.assertEquals(Integer.valueOf(12), customer.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetter() {
|
||||
Method name = BeanUtil.getter(Customer.class, "name");
|
||||
Assert.assertEquals("getName", name.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetter() {
|
||||
Method name = BeanUtil.setter(Customer.class, "name");
|
||||
Assert.assertEquals("setName", name.getName());
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
static class Customer {
|
||||
|
||||
String name;
|
||||
|
||||
Integer status;
|
||||
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
static class PreCustomer {
|
||||
|
||||
String name;
|
||||
|
||||
Integer status;
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue