001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.utils; 017 018import java.time.LocalDate; 019import java.time.LocalDateTime; 020import java.time.LocalTime; 021import java.util.Collection; 022import java.util.Date; 023import java.util.Objects; 024 025/** 026 * @author michael yang (fuhai999@gmail.com) 027 */ 028public class ObjectUtil { 029 030 031 /** 032 * 判断 某个 objects 集合里是否包含了某个 object 033 * 034 * @param objects object 集合 035 * @param compareObject 是否被包 集合 含的对比 object 036 * @param compareAttrGetters 需要对比的 getter 037 * @param <T> 038 * @return true 包含,false 不包含 039 */ 040 public static <T> boolean isContainsObject(Collection<T> objects, T compareObject, ObjectFunc<T>... compareAttrGetters) { 041 if (objects == null || objects.isEmpty() || compareObject == null) { 042 return false; 043 } 044 045 if (compareAttrGetters == null || compareAttrGetters.length == 0) { 046 throw new IllegalArgumentException("compareAttrGetters must not be null"); 047 } 048 049 050 for (T object : objects) { 051 if (isSameObject(object, compareObject, compareAttrGetters)) { 052 return true; 053 } 054 } 055 056 return false; 057 } 058 059 060 /** 061 * 获取 某个 objects 集合里包含的 object 062 * 063 * @param objects object 集合 064 * @param compareObject 是否被包 集合 含的对比 object 065 * @param compareAttrGetters 需要对比的 getter 066 * @param <T> 067 * @return 返回 objects 结合中对比成功的 object 068 */ 069 public static <T> T getContainsObject(Collection<T> objects, T compareObject, ObjectFunc<T>... compareAttrGetters) { 070 if (objects == null || objects.isEmpty() || compareObject == null) { 071 return null; 072 } 073 074 if (compareAttrGetters == null || compareAttrGetters.length == 0) { 075 throw new IllegalArgumentException("compareAttrGetters must not be null"); 076 } 077 078 079 for (T object : objects) { 080 if (isSameObject(object, compareObject, compareAttrGetters)) { 081 return object; 082 } 083 } 084 085 return null; 086 } 087 088 089 /** 090 * 判断两个 Object 是否是同一个 Object,根据传入的 getter 来进行对比 091 * 092 * @param object1 093 * @param object2 094 * @param compareAttrGetters 095 * @param <T> 096 * @return 097 */ 098 public static <T> boolean isSameObject(T object1, T object2, ObjectFunc<T>... compareAttrGetters) { 099 if (object1 == null || object2 == null) { 100 return object1 == object2; 101 } 102 103 if (compareAttrGetters == null || compareAttrGetters.length == 0) { 104 throw new IllegalArgumentException("compareAttrGetters must not be null"); 105 } 106 107 108 for (ObjectFunc getter : compareAttrGetters) { 109 110 if (getter == null) { 111 throw new IllegalArgumentException("compareAttrGetter must not be null"); 112 } 113 114 115 if (!Objects.equals(getter.get(object1), getter.get(object2))) { 116 return false; 117 } 118 } 119 120 return true; 121 } 122 123 124 /** 125 * 判断两个 Object 是否是同一个 Object,根据传入的 getter 来进行对比 126 * 127 * @param object1 128 * @param object2 129 * @param compareAttrGetters 130 * @param <T> 131 * @return 132 */ 133 public static <T> boolean notSameObject(T object1, T object2, ObjectFunc<T>... compareAttrGetters) { 134 return !isSameObject(object1, object2, compareAttrGetters); 135 } 136 137 138 public static Object convert(Object value, Class<?> targetClass) { 139 if (value == null || (value.getClass() == String.class && StrUtil.isBlank((String) value) 140 && targetClass != String.class)) { 141 return null; 142 } 143 144 if (value.getClass().isAssignableFrom(targetClass)) { 145 return value; 146 } 147 if (targetClass == String.class) { 148 return value.toString(); 149 } else if (targetClass == Integer.class || targetClass == int.class) { 150 if (value instanceof Number) { 151 return ((Number) value).intValue(); 152 } 153 return Integer.parseInt(value.toString()); 154 } else if (targetClass == Long.class || targetClass == long.class) { 155 if (value instanceof Number) { 156 return ((Number) value).longValue(); 157 } 158 return Long.parseLong(value.toString()); 159 } else if (targetClass == Double.class || targetClass == double.class) { 160 if (value instanceof Number) { 161 return ((Number) value).doubleValue(); 162 } 163 return Double.parseDouble(value.toString()); 164 } else if (targetClass == Float.class || targetClass == float.class) { 165 if (value instanceof Number) { 166 return ((Number) value).floatValue(); 167 } 168 return Float.parseFloat(value.toString()); 169 } else if (targetClass == Boolean.class || targetClass == boolean.class) { 170 String v = value.toString().toLowerCase(); 171 if ("1".equals(v) || "true".equalsIgnoreCase(v)) { 172 return Boolean.TRUE; 173 } else if ("0".equals(v) || "false".equalsIgnoreCase(v)) { 174 return Boolean.FALSE; 175 } else { 176 throw new RuntimeException("Can not parse to boolean type of value: \"" + value + "\""); 177 } 178 } else if (targetClass == java.math.BigDecimal.class) { 179 return new java.math.BigDecimal(value.toString()); 180 } else if (targetClass == java.math.BigInteger.class) { 181 return new java.math.BigInteger(value.toString()); 182 } else if (targetClass == byte[].class) { 183 return value.toString().getBytes(); 184 } else if (targetClass == Date.class) { 185 return DateUtil.parseDate(value); 186 } else if (targetClass == LocalDateTime.class) { 187 return DateUtil.toLocalDateTime(DateUtil.parseDate(value)); 188 } else if (targetClass == LocalDate.class) { 189 return DateUtil.toLocalDate(DateUtil.parseDate(value)); 190 } else if (targetClass == LocalTime.class) { 191 return DateUtil.toLocalTime(DateUtil.parseDate(value)); 192 } else if (targetClass == Short.class || targetClass == short.class) { 193 if (value instanceof Number) { 194 return ((Number) value).shortValue(); 195 } 196 return Short.parseShort(value.toString()); 197 } 198 199 throw new RuntimeException("\"" + targetClass.getName() + "\" can not be parsed."); 200 } 201 202 203 public static Object getPrimitiveDefaultValue(Class<?> paraClass) { 204 if (paraClass == int.class || paraClass == long.class || paraClass == float.class || paraClass == double.class) { 205 return 0; 206 } else if (paraClass == boolean.class) { 207 return Boolean.FALSE; 208 } else if (paraClass == short.class) { 209 return (short) 0; 210 } else if (paraClass == byte.class) { 211 return (byte) 0; 212 } else if (paraClass == char.class) { 213 return '\u0000'; 214 } else { 215 //不存在这种类型 216 return null; 217 } 218 } 219 220 221 public static <T> T obtainNotNull(T... ts) { 222 if (ts == null || ts.length == 0) { 223 throw new IllegalArgumentException("Arguments is null or empty."); 224 } 225 226 for (T t : ts) { 227 if (t != null) { 228 return t; 229 } 230 } 231 232 return null; 233 } 234 235}