Java tutorial
/* * Copyright (C) 2008 feilong * * Licensed 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 com.feilong.core.bean; import static org.apache.commons.lang3.StringUtils.EMPTY; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.ResourceBundle; import java.util.TreeMap; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.converters.AbstractConverter; import org.apache.commons.beanutils.converters.BigDecimalConverter; import org.apache.commons.beanutils.converters.BooleanConverter; import org.apache.commons.beanutils.converters.IntegerConverter; import org.apache.commons.beanutils.converters.LongConverter; import org.apache.commons.beanutils.converters.NumberConverter; import org.apache.commons.collections4.EnumerationUtils; import org.apache.commons.collections4.IteratorUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.iterators.EnumerationIterator; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.LocaleUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.tuple.Pair; import com.feilong.core.lang.ArrayUtil; import com.feilong.core.lang.StringUtil; import static com.feilong.core.Validator.isNullOrEmpty; import static com.feilong.core.util.MapUtil.newLinkedHashMap; /** * ??. * * <h3>?:</h3> * * <blockquote> * <ul> * <li>{@link ConvertUtilsBean#registerPrimitives(boolean) registerPrimitives(boolean throwException)}</li> * <li>{@link ConvertUtilsBean#registerStandard(boolean,boolean) registerStandard(boolean throwException, boolean defaultNull);}</li> * <li>{@link ConvertUtilsBean#registerOther(boolean) registerOther(boolean throwException);}</li> * <li>{@link ConvertUtilsBean#registerArrays(boolean,int) registerArrays(boolean throwException, int defaultArraySize);}</li> * </ul> * </blockquote> * * <h3>{@link ConvertUtils} :</h3> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4" summary=""> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * <tr valign="top"> * <td>{@link ConvertUtils#convert(Object)}</td> * <td>value?string.<br> * valuearray,?string.<br> * <code>java.lang.String</code>{@link Converter},<br> * ? Object{@code ->}String conversions(? toString())<br> * see {@link ConvertUtilsBean#convert(Object)}</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link ConvertUtils#convert(String, Class)}</td> * <td>String value? Class (?),?string.<br> * see {@link ConvertUtilsBean#convert(String, Class)}</td> * </tr> * <tr valign="top"> * <td>{@link ConvertUtils#convert(String[], Class)}</td> * <td>?class. <br> * Class,.?.<br> * see {@link ConvertUtilsBean#convert(String[], Class)} <br> * see {@link #toArray(String[], Class)}</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link ConvertUtils#convert(Object, Class)}</td> * <td>value?Class,Class?,value.<br> * see {@link ConvertUtilsBean#convert(Object, Class)}<br> * see {@link #convert(Object, Class)}</td> * </tr> * </table> * </blockquote> * * <p> * standard {@link Converter} instances are provided for all of the following destination Classes: * </p> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4" summary=""> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * <tr valign="top"> * <td>{@link java.math.BigDecimal}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link java.math.BigInteger}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top"> * <td>boolean and {@link java.lang.Boolean}</td> * <td>default to false</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>byte and {@link java.lang.Byte}</td> * <td>default to zero</td> * </tr> * <tr valign="top"> * <td>char and {@link java.lang.Character}</td> * <td>default to a space</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link java.lang.Class}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top"> * <td>double and {@link java.lang.Double}</td> * <td>default to zero</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>float and {@link java.lang.Float}</td> * <td>default to zero</td> * </tr> * <tr valign="top"> * <td>int and {@link java.lang.Integer}</td> * <td>default to zero</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>long and {@link java.lang.Long}</td> * <td>default to zero</td> * </tr> * <tr valign="top"> * <td>short and {@link java.lang.Short}</td> * <td>default to zero</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link java.lang.String}</td> * <td>default to null</td> * </tr> * <tr valign="top"> * <td>{@link java.io.File}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link java.net.URL}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top"> * <td>{@link java.sql.Date}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link java.sql.Time}</td> * <td><span style="color:red">no default value</span></td> * </tr> * <tr valign="top"> * <td>{@link java.sql.Timestamp}</td> * <td><span style="color:red">no default value</span></td> * </tr> * </table> * </blockquote> * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> * @see org.apache.commons.beanutils.ConvertUtils * @see org.apache.commons.beanutils.converters.AbstractConverter#handleMissing(Class) * @see org.apache.commons.beanutils.locale.LocaleConvertUtils * @since 1.3.0 */ public final class ConvertUtil { /** Don't let anyone instantiate this class. */ private ConvertUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * <code>toBeConvertedValue</code> ?? {@link Boolean}. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * ConvertUtil.toBoolean(1L) = true * ConvertUtil.toBoolean("1") = true * ConvertUtil.toBoolean("9") = false * ConvertUtil.toBoolean(null) = null * ConvertUtil.toBoolean("1,2,3") = false * </pre> * * </blockquote> * * <h3>:</h3> * * <blockquote> * * <ul> * <li> "true", "yes", "y", "on", "1", true</li> * <li> "false", "no", "n", "off", "0", false</li> * <li> conversionException, * {@link org.apache.commons.beanutils.converters.AbstractConverter#handleError(Class, Object, Throwable) handleError(Class, Object, * Throwable)} ?, {@link BooleanConverter} ,?? * {@link org.apache.commons.beanutils.ConvertUtilsBean#registerStandard(boolean, boolean) registerStandard(boolean, boolean)}</li> * </ul> * * <p> * ? {@link org.apache.commons.beanutils.converters.BooleanConverter#BooleanConverter(String[], String[], Object) * BooleanConverter(String[], String[], Object)} trueStrings falseStrings * </p> * </blockquote> * * <h3> {@link Boolean#parseBoolean(String)}:</h3> * * <blockquote> * <p> * {@link Boolean#parseBoolean(String)}, <code>(String != null) String.equalsIgnoreCase("true")</code> true * </p> * </blockquote> * * @param toBeConvertedValue * object * @return <code>toBeConvertedValue</code> null,null<br> * @see #convert(Object, Class) * @see org.apache.commons.beanutils.converters.BooleanConverter * @see org.apache.commons.lang3.BooleanUtils * @see java.lang.Boolean#parseBoolean(String) */ public static Boolean toBoolean(Object toBeConvertedValue) { return convert(toBeConvertedValue, Boolean.class); } /** * <code>toBeConvertedValue</code> ?? {@link Integer}. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * ConvertUtil.toInteger(null) = null * ConvertUtil.toInteger("aaaa") = null * ConvertUtil.toInteger(8L) = 8 * ConvertUtil.toInteger("8") = 8 * ConvertUtil.toInteger(new BigDecimal("8")) = 8 * </pre> * * </blockquote> * * <p> * ??? * </p> * * <h3>:</h3> * * <blockquote> * * ?: * * <pre class="code"> * * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){ * String pageNoString = RequestUtil.getParameter(request, pageParamName); * try{ * int pageNo = Integer.parseInt(pageNoString); * return pageNo; * }catch (Exception e){ * LOGGER.error(e.getClass().getName(), e); * } * return 1; // ?? ? 1 * } * * </pre> * * ??: * * <pre class="code"> * * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){ * String pageNoString = RequestUtil.getParameter(request, pageParamName); * Integer pageNo = ConvertUtil.toInteger(pageNoString); * return null == pageNo ? 1 : pageNo; * } * </pre> * * </blockquote> * * @param toBeConvertedValue * * @return <code>toBeConvertedValue</code> null, null<br> * ???, null * @see org.apache.commons.beanutils.converters.IntegerConverter * @see org.apache.commons.lang3.math.NumberUtils#toInt(String) * @see #toInteger(Object, Integer) */ public static Integer toInteger(Object toBeConvertedValue) { return toInteger(toBeConvertedValue, null); } /** * <code>toBeConvertedValue</code> ?? {@link Integer},?? <code>defaultValue</code>. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * ConvertUtil.toInteger(null,1) = 1 * ConvertUtil.toInteger("aaaa",1) = 1 * ConvertUtil.toInteger(8L,1) = 8 * ConvertUtil.toInteger("8",1) = 8 * ConvertUtil.toInteger(new BigDecimal("8"),1) = 8 * </pre> * * </blockquote> * * <p> * ??? * </p> * * <h3>:</h3> * * <blockquote> * * ?: * * <pre class="code"> * * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){ * String pageNoString = RequestUtil.getParameter(request, pageParamName); * try{ * int pageNo = Integer.parseInt(pageNoString); * return pageNo; * }catch (Exception e){ * LOGGER.error(e.getClass().getName(), e); * } * return 1; // ?? ? 1 * } * * </pre> * * ??: * * <pre class="code"> * * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){ * String pageNoString = RequestUtil.getParameter(request, pageParamName); * return ConvertUtil.toInteger(pageNoString, 1); * } * </pre> * * </blockquote> * * @param toBeConvertedValue * * @param defaultValue * * @return <code>toBeConvertedValue</code> null, <code>defaultValue</code> <br> * ???, <code>defaultValue</code> * @see org.apache.commons.beanutils.converters.IntegerConverter * @see org.apache.commons.lang3.ObjectUtils#defaultIfNull(Object, Object) * @since 1.6.1 */ public static Integer toInteger(Object toBeConvertedValue, Integer defaultValue) { return new IntegerConverter(defaultValue).convert(Integer.class, toBeConvertedValue); } /** * <code>toBeConvertedValue</code> ?? {@link java.math.BigDecimal}. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * ConvertUtil.toBigDecimal(1111) = BigDecimal.valueOf(1111) * </pre> * * </blockquote> * * <h3>{@link java.lang.Double} ? {@link java.math.BigDecimal}?:</h3> * * <blockquote> * * <p> * <span style="color:red">?? {@link BigDecimal#valueOf(double)}</span>,? <code>new BigDecimal(double)</code>,?? JDK API<br> * </p> * * <ul> * <li>{@code new BigDecimal(0.1) ====> 0.1000000000000000055511151231257827021181583404541015625}</li> * <li>{@code BigDecimal.valueOf(0.1) ====> 0.1}</li> * </ul> * * {@link NumberConverter#toNumber(Class, Class, Number) * NumberConverter#toNumber(Class, Class, Number)},? {@link java.lang.Double} ? {@link java.math.BigDecimal} </blockquote> * * @param toBeConvertedValue * * @return <code>toBeConvertedValue</code> null, null<br> * ???, null * @see #convert(Object, Class) * @see org.apache.commons.beanutils.converters.NumberConverter#toNumber(Class, Class, Number) * @see org.apache.commons.beanutils.converters.BigDecimalConverter */ public static BigDecimal toBigDecimal(Object toBeConvertedValue) { return new BigDecimalConverter(null).convert(BigDecimal.class, toBeConvertedValue); } /** * ?{@link Long}. * * <p> * converted is missing or an error occurs converting the value,<span style="color:red"> null</span> * </p> * * @param toBeConvertedValue * ?. * @return <code>toBeConvertedValue</code> null, null<br> * ???, null * @see #convert(Object, Class) * @see org.apache.commons.beanutils.converters.LongConverter * @see org.apache.commons.lang3.math.NumberUtils#toLong(String) */ public static Long toLong(Object toBeConvertedValue) { return new LongConverter(null).convert(Long.class, toBeConvertedValue); } /** * ??{@link Integer} . * * <h3>:</h3> * <blockquote> * * <pre class="code"> * ConvertUtil.toIntegers("1,2,3") = [1,2,3] * ConvertUtil.toIntegers(new String[] { "1", "2", "3" }) = [1,2,3] * ConvertUtil.toIntegers(toList("1", "2", "3")) = [1,2,3] * </pre> * * </blockquote> * * <h3> <code>toBeConvertedValue</code> <code>value.getClass().isArray()</code> {@link Collection}</h3> * <blockquote> * * <p> * ?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) ArrayConverter#convertToType(Class, * Object)} targetType <code>Integer</code>, ? <code>toBeConvertedValue</code>?, ? * <code>toBeConvertedValue</code> * ?? * </p> * * </blockquote> * * @param toBeConvertedValue * the to be converted value * @return <code>toBeConvertedValue</code> null, null<br> * @see #convert(Object, Class) * @see org.apache.commons.beanutils.converters.ArrayConverter */ public static Integer[] toIntegers(Object toBeConvertedValue) { return convert(toBeConvertedValue, Integer[].class); } /** * <code>toBeConvertedValue</code> ?Long . * * <h3>:</h3> * <blockquote> * * <pre class="code"> * toLongs("1,2,3") = [1,2,3] * toLongs(new String[] { "1", "2", "3" }) = [1,2,3] * toLongs(toList("1", "2", "3")) = [1,2,3] * </pre> * * </blockquote> * * <h3>???:</h3> * * <blockquote> * * <pre class="code"> * * protected long[] getOrderIdLongs(String orderIds){ * // ??,? * String[] orderIdArray = orderIds.split(","); * int orderLength = orderIdArray.length; * long[] ids = new long[orderLength]; * for (int i = 0, j = orderLength; i {@code <} j; ++i){ * ids[i] = Long.parseLong(orderIdArray[i]); * } * return ids; * } * * </pre> * * ???: * * <pre class="code"> * * protected long[] getOrderIdLongs(String orderIds){ * return toLongs(orderIds); * } * </pre> * * </blockquote> * * <h3> <code>toBeConvertedValue</code> <code>value.getClass().isArray()</code> {@link Collection}</h3> * <blockquote> * * <p> * ?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) ArrayConverter#convertToType(Class, * Object)} targetType <code>Long</code>, ? <code>toBeConvertedValue</code>?, ? <code>toBeConvertedValue</code> * ?? * </p> * * </blockquote> * * @param toBeConvertedValue * the to be converted value * @return <code>toBeConvertedValue</code> null, null<br> * @see org.apache.commons.beanutils.ConvertUtils#convert(Object, Class) * @see org.apache.commons.beanutils.converters.ArrayConverter * @see #convert(Object, Class) */ public static Long[] toLongs(Object toBeConvertedValue) { return convert(toBeConvertedValue, Long[].class); } //************************************************************************************************* /** * <code>toBeConvertedValue</code> ??. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * assertEquals("1", ConvertUtil.toString(1)); * assertEquals("1.0", ConvertUtil.toString(toBigDecimal(1.0))); * assertEquals("8", ConvertUtil.toString(toLong(8L))); * </pre> * * </blockquote> * * <h3>?:</h3> * * <blockquote> * * <p> * <b>??</b> list??,: * </p> * * <pre class="code"> * ConvertUtil.toString(toList("", "", "", "")) = "" * </pre> * * <p> * , {@link #toString(ToStringConfig, Collection)} * </p> * * <hr> * * <p> * <b>??</b> array ??,: * </p> * * <pre class="code"> * Integer[] int1 = { 2, null, 1, null }; * LOGGER.debug(ConvertUtil.toString(int1)); = 2 * </pre> * * <p> * {@link #toString(ToStringConfig, Object...)} * </p> * * </blockquote> * * <h3> Array ? String</h3> * * <blockquote> * * <p> * ?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToString(Object) ArrayConverter#convertToString(Object)} <br> * * ?, ? object, {@link java.lang.reflect.Array#get(Object, int) Array#get(Object, int)} ??,<br> * ??, object?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToCollection(Class, Object) * ArrayConverter#convertToCollection(Class, Object)} ?? {@link java.util.Collection#iterator() Collection.iterator()} * </p> * * <p> * object?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToCollection(Class, Object) * ArrayConverter#convertToCollection(Class, Object)},: * </p> * * <ul> * <li>The string is expected to be a comma-separated list of values.</li> * <li>?'{' and '}'.</li> * <li>??.</li> * <li>Elements in the list may be delimited by single or double quotes. Within a quoted elements, the normal Java escape sequences are * valid.</li> * </ul> * * <p> * : * </p> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4" summary=""> * * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * * <tr valign="top"> * <td>int defaultSize</td> * <td>? or if less than zero indicates that a <code>null</code> default value should be used.</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>char delimiter = ','</td> * <td>,?string</td> * </tr> * * <tr valign="top"> * <td>char[] allowedChars = new char[] {'.', '-'}</td> * <td>{@link java.io.StreamTokenizer}</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>boolean onlyFirstToString = true;</td> * <td>?</td> * </tr> * * </table> * </blockquote> * </blockquote> * * @param toBeConvertedValue * ? * @return the string * @see org.apache.commons.beanutils.converters.ArrayConverter#convertToString(Object) * @see org.apache.commons.beanutils.ConvertUtils#convert(Object) * @see org.apache.commons.beanutils.ConvertUtilsBean#convert(Object) * @see org.apache.commons.beanutils.converters.StringConverter * * @see java.util.Arrays#toString(Object[]) */ public static String toString(Object toBeConvertedValue) { return convert(toBeConvertedValue, String.class); } /** * ? <code>collection</code>??. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * List{@code <String>} list = new ArrayList{@code <String>}(); * list.add("feilong"); * list.add(""); * list.add("xinge"); * * ToStringConfig toStringConfig = new ToStringConfig(","); * toStringConfig.setIsJoinNullOrEmpty(false); * * ConvertUtil.toString(toStringConfig, list); * </pre> * * <b>:</b> * * <pre class="code"> * feilong,xinge * </pre> * * </blockquote> * * @param toStringConfig * * @param collection * ?, ?,collection * @return <code>collection</code> isNullOrEmpty,null<br> * <code>toStringConfig</code> null, {@link ToStringConfig#DEFAULT_CONNECTOR} <br> * ?null,,toStringConfig.getConnector() * @see #toString(ToStringConfig, Object...) * @see "org.springframework.util.StringUtils#collectionToDelimitedString(Collection, String, String, String)" * @see org.apache.commons.collections4.IteratorUtils#toString(Iterator) * @since 1.4.0 */ public static String toString(ToStringConfig toStringConfig, final Collection<?> collection) { return isNullOrEmpty(collection) ? EMPTY : toString(toStringConfig, collection.toArray()); } /** * {@link ToStringConfig} ?. * * <p style="color:green"> * ??, Integer [] arrays int []arrays * </p> * * <pre class="code"> * Example 1: * ConvertUtil.toString(new ToStringConfig(),"a","b") = "a,b" * * Example 2: * ToStringConfig toStringConfig=new ToStringConfig(","); * toStringConfig.setIsJoinNullOrEmpty(false); * ConvertUtil.toString(new ToStringConfig(),"a","b",null) = "a,b" * * Example 3: * int[] ints = { 2, 1 }; * ConvertUtil.toString(new ToStringConfig(),ints) = "2,1" * </pre> * * @param toStringConfig * the join string entity * @param arrays * <span style="color:red">??, Integer []arrays ? int []arrays</span> * @return <code>arrays</code> null Empty, {@link StringUtils#EMPTY}<br> * ?, {@link ToStringConfig#getConnector()} * @see org.apache.commons.lang3.builder.ToStringStyle * @see org.apache.commons.lang3.StringUtils#join(Iterable, String) * @see org.apache.commons.lang3.StringUtils#join(Object[], String) * @since 1.4.0 */ public static String toString(ToStringConfig toStringConfig, Object... arrays) { if (isNullOrEmpty(arrays)) { return EMPTY; } ToStringConfig useToStringConfig = ObjectUtils.defaultIfNull(toStringConfig, new ToStringConfig()); return join(toObjects(arrays), useToStringConfig.getConnector(), useToStringConfig.getIsJoinNullOrEmpty()); } /** * Join. * * @param operateArray * the operate array * @param connector * the connector * @param isJoinNullOrEmpty * the is join nullempty * @return the string * @see org.apache.commons.lang3.StringUtils#join(Iterable, String) * @see org.apache.commons.lang3.StringUtils#join(Object[], String) * @since 1.6.3 */ private static String join(Object[] operateArray, String connector, boolean isJoinNullOrEmpty) { StringBuilder sb = new StringBuilder(); for (Object obj : operateArray) { //nullempty,??,, if (isNullOrEmpty(obj) && !isJoinNullOrEmpty) { continue; } //value?,?: valuenull,StringBuilder "null" ,? java.lang.AbstractStringBuilder#append(String) sb.append(ObjectUtils.defaultIfNull(obj, EMPTY)); //see StringUtils.defaultString(t) if (null != connector) {//???? ?Validator.isNullOrEmpty? sb.append(connector);// connector, ???,?? } } return StringUtil.substringWithoutLast(sb, connector); } //********************************************************************************************** /** * ? <code>collection</code> ?. * * @param <T> * the generic type * @param collection * ? * @return <code>collection</code> null, {@link Collections#emptyEnumeration()}<br> * ?{@link Collections#enumeration(Collection)} * @see Collections#enumeration(Collection) * @since 1.4.0 */ public static <T> Enumeration<T> toEnumeration(final Collection<T> collection) { return null == collection ? Collections.<T>emptyEnumeration() : Collections.enumeration(collection); } /** * <code>mapEntryCollection</code> ?map ({@link LinkedHashMap}). * * <p> * ?, {@link LinkedHashMap},??? <code>mapEntryCollection</code>,key {@link java.util.Map.Entry#getKey()},value * {@link java.util.Map.Entry#getValue()} * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * Map{@code <String, String>} map = ConvertUtil.toMap( * toList( * new SimpleEntry{@code <>}("", ""), * new SimpleEntry{@code <>}("", "??"), * new SimpleEntry{@code <>}("", ""), * new SimpleEntry{@code <>}("", "?"))); * LOGGER.debug(JsonUtil.format(map)); * * </pre> * * <b>:</b> * * <pre class="code"> * { * "": "", * "": "??", * "": "", * "": "?" * } * * </pre> * * </blockquote> * * @param <V> * the value type * @param <K> * the key type * @param <E> * the element type * @param mapEntryCollection * the map entry collection * @return <code>mapEntryCollection</code> null, {@link Collections#emptyMap()}<br> * @see org.apache.commons.lang3.ArrayUtils#toMap(Object[]) * @since 1.7.1 */ public static <V, K, E extends Map.Entry<K, V>> Map<K, V> toMap(Collection<E> mapEntryCollection) { if (null == mapEntryCollection) { return Collections.emptyMap(); } Map<K, V> map = newLinkedHashMap(mapEntryCollection.size()); for (Map.Entry<K, V> entry : mapEntryCollection) { map.put(entry.getKey(), entry.getValue()); } return map; } /** * {@link java.util.Map.Entry}?map ({@link LinkedHashMap}). * * <p> * ?, {@link LinkedHashMap},??? {@link java.util.Map.Entry}?,key {@link java.util.Map.Entry#getKey()},value * {@link java.util.Map.Entry#getValue()} * </p> * * <h3>{@link java.util.Map.Entry} </h3> * * <blockquote> * <p> * ? {@link Pair}, {@link java.util.AbstractMap.SimpleEntry} * </p> * </blockquote> * * <h3>{@link Pair} :</h3> * * <blockquote> * * <pre class="code"> * * Map{@code <String, String>} map = ConvertUtil.toMap( * * Pair.of("", ""), * Pair.of("", "??"), * Pair.of("", ""), * Pair.of("", "?")); * LOGGER.debug(JsonUtil.format(map)); * * </pre> * * <b>:</b> * * <pre class="code"> * { * "": "", * "": "??", * "": "", * "": "?" * } * * </pre> * * </blockquote> * * <h3>{@link java.util.AbstractMap.SimpleEntry} :</h3> * * <blockquote> * * <pre class="code"> * * Map{@code <String, String>} map = ConvertUtil.toMap( * new SimpleEntry{@code <>}("", ""), * new SimpleEntry{@code <>}("", "??"), * new SimpleEntry{@code <>}("", ""), * new SimpleEntry{@code <>}("", "?")); * LOGGER.debug(JsonUtil.format(map)); * * </pre> * * <b>:</b> * * <pre class="code"> * { * "": "", * "": "??", * "": "", * "": "?" * } * * </pre> * * </blockquote> * * * <h3>?:</h3> * * ??map,? * * <blockquote> * * <pre class="code"> * * // ??map,? ?. * private static final Map{@code <Long, String>} DIVISOR_AND_UNIT_MAP = new LinkedHashMap{@code <>}(); * * static{ * DIVISOR_AND_UNIT_MAP.put(FileUtils.ONE_TB, "TB");//(Terabyte,,)=1024GB,1024=2^10 ( 2 10) * DIVISOR_AND_UNIT_MAP.put(FileUtils.ONE_GB, "GB");//(Gigabyte,?,???)=1024MB, * DIVISOR_AND_UNIT_MAP.put(FileUtils.ONE_MB, "MB");//(Megabyte,,?)=1024KB, * DIVISOR_AND_UNIT_MAP.put(FileUtils.ONE_KB, "KB");//(Kilobyte ?)=1024B * } * * </pre> * * ??: * * <pre class="code"> * * // ??map,? ?. * private static final Map{@code <Long, String>} DIVISOR_AND_UNIT_MAP = ConvertUtil.toMap( * Pair.of(FileUtils.ONE_TB, "TB"), //(Terabyte,,)=1024GB,1024=2^10 ( 2 10) * Pair.of(FileUtils.ONE_GB, "GB"), //(Gigabyte,?,???)=1024MB, * Pair.of(FileUtils.ONE_MB, "MB"), //(Megabyte,,?)=1024KB, * Pair.of(FileUtils.ONE_KB, "KB")); //(Kilobyte ?)=1024B * * </pre> * * </blockquote> * * @param <V> * the value type * @param <K> * the key type * @param mapEntrys * the entrys * @return <code>entrys</code> null, {@link Collections#emptyMap()}<br> * @see org.apache.commons.lang3.tuple.ImmutablePair#ImmutablePair(Object, Object) * @see org.apache.commons.lang3.tuple.Pair#of(Object, Object) * @since 1.7.1 */ @SafeVarargs public static <V, K> Map<K, V> toMap(Map.Entry<K, V>... mapEntrys) { if (null == mapEntrys) { return Collections.emptyMap(); } Map<K, V> map = newLinkedHashMap(mapEntrys.length); for (Map.Entry<K, V> entry : mapEntrys) { map.put(entry.getKey(), entry.getValue()); } return map; } /** * <code>key</code> <code>value</code> ?map. * * <p> * ?, {@link LinkedHashMap}} * </p> * * <p> * ???key, * </p> * * <pre class="code"> * Map{@code <String, String>} paramMap = new HashMap{@code <String, String>}(); * paramMap.put("name", "jinxin"); * request.setParamMap(paramMap); * </pre> * * ?3???? * * <pre class="code"> * request.setParamMap(toMap("name", "jinxin")); * </pre> * * <p> * ??,?,~~ * </p> * * <p> * : * </p> * * <pre class="code"> * private List{@code <ShopCommand>} loadShopCommandList(){ * Map{@code <String, Object>} paraMap = new HashMap{@code <String, Object>}(); * paraMap.put("orgTypeId", OrgType.ID_SHOP_TYPE); * * return shopCommandDao.findShopListByOrgaTypeId(paraMap); * } * </pre> * * ?? : * * <pre class="code"> * private List{@code <ShopCommand>} loadShopCommandList(){ * return shopCommandDao.findShopListByOrgaTypeId(ConvertUtil.toMap("orgTypeId", (Object) OrgType.ID_SHOP_TYPE)); * } * </pre> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * LOGGER.debug(JsonUtil.format(ConvertUtil.toMap("", ""))); * </pre> * * <b>:</b> * * <pre class="code"> * {"": ""} * </pre> * * </blockquote> * * @param <K> * the key type * @param <V> * the value type * @param key * the key * @param value * the value * @return <code>key</code> <code>value</code> ?map * @see org.apache.commons.lang3.ArrayUtils#toMap(Object[]) * @see java.util.Collections#singletonMap(Object, Object) * @since 1.7.1 */ public static <K, V> Map<K, V> toMap(K key, V value) { Map<K, V> map = new LinkedHashMap<K, V>();//?? ,??PUT map.put(key, value); return map; } /** * ??map. * * <p> * ?, {@link TreeMap} * </p> * * <h3> <code>Properties</code></h3> * * <blockquote> * <p> * Create a new HashMap and pass an instance of Properties.<br> * Properties is an implementation of a Map which keys and values stored as in a string. * </p> * * <p> * Properties ???map * </p> * </blockquote> * * @param properties * the properties * @return <code>properties</code> null, {@link NullPointerException}<br> * @see org.apache.commons.collections4.MapUtils#toProperties(Map) * @since 1.7.1 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map<String, String> toMap(Properties properties) { Validate.notEmpty(properties, "properties can't be null/empty!"); return new TreeMap<String, String>((Map) properties);//log, treeMap } /** * ??,k/v ?map. * * <p> * ?:JDK{@link java.util.PropertyResourceBundle}, hashmap??,<br> * log?,<span style="color:red"> TreeMap</span> * </p> * * @param resourceBundle * the resource bundle * @return <code>resourceBundle</code> null, {@link NullPointerException}<br> * <code>resourceBundle</code> key,{@link java.util.Collections#emptyMap()}<br> * ?,?keyvalue? {@link TreeMap}<br> * @see MapUtils#toMap(ResourceBundle) * @since 1.7.3 */ public static Map<String, String> toMap(ResourceBundle resourceBundle) { Validate.notNull(resourceBundle, "resourceBundle can't be null!"); Enumeration<String> keysEnumeration = resourceBundle.getKeys(); if (isNullOrEmpty(keysEnumeration)) { return Collections.emptyMap(); } Map<String, String> map = new TreeMap<String, String>();//log, treeMap while (keysEnumeration.hasMoreElements()) { String key = keysEnumeration.nextElement(); map.put(key, resourceBundle.getString(key)); } return map; } /** * map? {@link Properties}. * * <p> * Properties ?? ? keyvalue,map key valuenull, {@link NullPointerException} * </p> * * @param map * the map * @return <code>map</code> null, empty Properties<br> * @see org.apache.commons.collections4.MapUtils#toProperties(Map) * @since 1.7.3 */ public static Properties toProperties(final Map<String, String> map) { return MapUtils.toProperties(map); } //*************************************toList********************************************************* /** * <code>enumeration</code> ??. * * @param <T> * the generic type * @param enumeration * the enumeration * @return <code>enumeration</code> null, {@link Collections#emptyList()}<br> * ? {@link Collections#list(Enumeration)} * @see Collections#emptyList() * @see Collections#EMPTY_LIST * @see Collections#list(Enumeration) * @see EnumerationUtils#toList(Enumeration) * @since 1.0.7 */ public static <T> List<T> toList(final Enumeration<T> enumeration) { return null == enumeration ? Collections.<T>emptyList() : Collections.list(enumeration); } /** * ? <code>collection</code> ? list. * * <p> * ? set?list? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * Set{@code <String>} set = new HashSet{@code <String>}(); * Collections.addAll(set, "a", "a", "b", "b"); * LOGGER.debug("{}", toList(set)); * </pre> * * <b>:</b> * * <pre class="code"> * [b, a] * </pre> * * </blockquote> * * @param <T> * the generic type * @param collection * the collection * @return <code>collection</code> null, {@link Collections#emptyList()}<br> * <code>collection instanceof List</code>,? list<br> * ? <code>new ArrayList(collection)</code> * @see org.apache.commons.collections4.IterableUtils#toList(Iterable) * @see org.apache.commons.collections4.IteratorUtils#toList(Iterator) * @since 1.6.1 */ public static <T> List<T> toList(final Collection<T> collection) { return null == collection ? Collections.<T>emptyList() : (collection instanceof List ? (List<T>) collection : new ArrayList<T>(collection)); } /** * ? ({@link java.util.ArrayList ArrayList}),list?add?. * * <h3>?:</h3> * * <blockquote> * * <p> * ???list: * </p> * * <pre class="code"> * * List{@code <String>} list = new ArrayList{@code <String>}(); * list.add("feilong1"); * list.add("feilong2"); * list.add("feilong2"); * list.add("feilong3"); * * </pre> * * ?: * * <pre class="code"> * * List{@code <String>} list = toList("feilong1", "feilong2", "feilong2", "feilong3"); * </pre> * * <p> * ?? * </p> * </blockquote> * * <h3>:</h3> * * <blockquote> * * <p> * ,??list,,? * </p> * * <pre class="code"> * * List{@code <UserAddress>} userAddresseList = new ArrayList{@code <UserAddress>}(); * UserAddress userAddress = new UserAddress(); * userAddress.setAddress(""); * userAddresseList.add(userAddress); * * </pre> * * ???: * * <pre class="code"> * UserAddress userAddress = new UserAddress(); * userAddress.setAddress(""); * List{@code <UserAddress>} userAddresseList = toList(userAddress); * </pre> * * </blockquote> * * <h3>? :</h3> * * <blockquote> * <p> * {@link java.util.Arrays#asList(Object...) Arrays#asList(Object...)}list {@link java.util.Collection#add(Object) * Collection#add(Object)},<code>list.add("c")</code>;??!<br> * * {@link ArrayList#ArrayList(java.util.Collection)} ???,?list? * </p> * </blockquote> * * @param <T> * the generic type * @param arrays * T * @return <code>arrays</code> nullempty, {@link Collections#emptyList()}<br> * ? {@code new ArrayList<T>(Arrays.asList(arrays));} * @see java.util.Arrays#asList(Object...) * @see java.util.Collections#singleton(Object) * @see java.util.Collections#addAll(Collection, Object...) * @see java.util.Collections#singletonList(Object) * @see "org.springframework.util.CollectionUtils#arrayToList(Object)" */ @SafeVarargs public static <T> List<T> toList(T... arrays) { return isNullOrEmpty(arrays) ? Collections.<T>emptyList() : new ArrayList<T>(Arrays.asList(arrays)); } //*************************************toArray********************************************************* /** * ??. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * String[] array = ConvertUtil.toArray("1", "2"); = ["1", "2"]; * String[] emptyArray = ConvertUtil.{@code <String>}toArray(); = [] ; //= new String[] {}; * Integer[] emptyArray = ConvertUtil.{@code <Integer>}toArray(); = [] ; //= new Integer[] {}; * String[] nullArray = ConvertUtil.toArray(null) = null; * * </pre> * * </blockquote> * * <h3>?:</h3> * * <blockquote> * <p> * (reified),?(erasure)<br> * ???,??,?,?,?? * </p> * * <b>:</b> * * <ol> * <li>????raw type; List{@code <E>}?List{@code <List<E>>}? List</li> * <li>????; List{@code <E>}[],? List[]</li> * <li>Raw type ,?, List{@code <E>} ? List</li> * <li>int,String wrapper ?</li> * <li>? E,?,? Object</li> * <li>??{@code <? Extends E>}?{@code <X extends E>} ? E</li> * <li>?,?,{@code <T extends Object & E>},? Object</li> * </ol> * * <p> * ??: * </p> * * <pre class="code"> * * public static {@code <K, V>} Map<K, V[]> toArrayValueMap(Map{@code <K, V>} singleValueMap){ * Map{@code <K, V[]>} arrayValueMap = newLinkedHashMap(singleValueMap.size());//????singleValueMap?? * for (Map.Entry{@code <K, V>} entry : singleValueMap.entrySet()){ * arrayValueMap.put(entry.getKey(), toArray(entry.getValue()));//?Value???V,???Object * } * return arrayValueMap; * } * </pre> * * , * * <pre class="code"> * Map{@code <String, String>} singleValueMap = MapUtil.newLinkedHashMap(2); * singleValueMap.put("province", "??"); * singleValueMap.put("city", "?"); * * Map{@code <String, String[]>} arrayValueMap = MapUtil.toArrayValueMap(singleValueMap); * String[] strings = arrayValueMap.get("province");// Object[] * </pre> * * * * <pre class="code"> * java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; * </pre> * * </blockquote> * * @param <T> * the generic type * @param arrays * the arrays * @return <code>arrays</code> null,null<br> * @see org.apache.commons.lang3.ArrayUtils#toArray(Object...) * @since commons-lang 3 * @since 1.6.0 */ @SafeVarargs public static <T> T[] toArray(T... arrays) { return ArrayUtils.toArray(arrays); } /** * ? <code>collection</code> ?. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * List{@code <String>} list = new ArrayList{@code <String>}(); * list.add("xinge"); * list.add("feilong"); * </pre> * * ???: * * <pre class="code"> * list.toArray(new String[list.size()]); * </pre> * * ???: * * <pre class="code"> * String[] array = ConvertUtil.toArray(list, String.class); * LOGGER.info(JsonUtil.format(array)); * </pre> * * <b>:</b> * * <pre class="code"> * [ * "xinge", * "feilong" * ] * </pre> * * </blockquote> * * @param <T> * the generic type * @param collection * collection * @param arrayComponentType * Class * @return <code>collection</code> null, {@link NullPointerException}<br> * <code>arrayComponentType</code> null, {@link NullPointerException}<br> * @see java.lang.reflect.Array#newInstance(Class, int) * @see java.lang.reflect.Array#newInstance(Class, int...) * @see java.util.Collection#toArray() * @see java.util.Collection#toArray(Object[]) * @see java.util.List#toArray() * @see java.util.List#toArray(Object[]) * @see java.util.Vector#toArray() * @see java.util.Vector#toArray(Object[]) * @see java.util.LinkedList#toArray() * @see java.util.LinkedList#toArray(Object[]) * @see java.util.ArrayList#toArray() * @see java.util.ArrayList#toArray(Object[]) * * @see org.apache.commons.collections4.IteratorUtils#toArray(Iterator,Class) * @see "org.springframework.util.StringUtils#toStringArray(Collection)" * @since 1.2.2 */ public static <T> T[] toArray(Collection<T> collection, Class<T> arrayComponentType) { Validate.notNull(collection, "collection must not be null"); Validate.notNull(arrayComponentType, "arrayComponentType must not be null"); // alength0,???API??size,?. // ?alengthCollectionsize????API?. T[] array = ArrayUtil.newArray(arrayComponentType, collection.size()); //?,toArray(new Object[0]) toArray() ?. return collection.toArray(array); } /** * Convert an array of specified values to an array of objects of the specified class (if possible). * * <p> * If the specified Java class is itself an array class, this class will be the type of the returned value.<br> * Otherwise, an array will be constructed whose component type is the specified class. * </p> * * @param <T> * the generic type * @param toBeConvertedValue * the values * @param targetType * ?? * @return <code>toBeConvertedValue</code> null,null<br> * <code>targetType</code> null, {@link NullPointerException}<br> * ? {@link ConvertUtils#convert(String[], Class)} * @see org.apache.commons.beanutils.ConvertUtils#convert(String[], Class) * @see org.apache.commons.beanutils.ConvertUtilsBean#convert(String[], Class) * @since 1.6.0 */ @SuppressWarnings("unchecked") public static <T> T[] toArray(String[] toBeConvertedValue, Class<T> targetType) { return null == toBeConvertedValue ? null : (T[]) ConvertUtils.convert(toBeConvertedValue, targetType); } //******************************************************************************** /** * ?. * * <p> * <code>arrays</code>,?. * </p> * * @param <T> * the generic type * @param arrays * the arrays * @return the object[] * @since 1.4.0 */ @SafeVarargs private static <T> Object[] toObjects(T... arrays) { if (isNullOrEmpty(arrays)) { return ArrayUtils.EMPTY_STRING_ARRAY; } if (arrays.length > 1) { return arrays; } Object o = arrays[0]; return isPrimitiveArray(o) ? primitiveArrayToObjectArray(o) : arrays; } /** * ? Primitive. * * @param o * the o * @return true, if checks if is primitive array * * @since 1.4.0 */ private static boolean isPrimitiveArray(Object o) { // Allocate a new Array Class<? extends Object> klass = o.getClass(); return !klass.isArray() ? false : klass.getComponentType().isPrimitive();// } /** * To objects. * * @param primitiveArray * the o * @return the object[] * @since 1.4.0 */ private static Object[] primitiveArrayToObjectArray(Object primitiveArray) { int length = ArrayUtils.getLength(primitiveArray); Object[] returnStringArray = new Object[length]; for (int i = 0; i < length; ++i) { returnStringArray[i] = ArrayUtil.getElement(primitiveArray, i); } return returnStringArray; } /** * ?{@link String}. * * <h3>:</h3> * * <blockquote> * <p> * ? <b>?</b> ?? <b></b>, * </p> * * <pre class="code"> * URL[] urls = { * URLUtil.newURL("http://www.exiaoshuo.com/jinyiyexing0/"), * URLUtil.newURL("http://www.exiaoshuo.com/jinyiyexing1/"), * URLUtil.newURL("http://www.exiaoshuo.com/jinyiyexing2/"), * null }; * * LOGGER.debug(JsonUtil.format(ConvertUtil.toStrings(urls))); * </pre> * * <b>:</b> * * <pre class="code"> * [ * "http://www.exiaoshuo.com/jinyiyexing0/", * "http://www.exiaoshuo.com/jinyiyexing1/", * "http://www.exiaoshuo.com/jinyiyexing2/", * null * ] * </pre> * * </blockquote> * * <p> * ? ?? in the Java language into a <code>List</code> individual Strings for each element, ?: * </p> * * <h3>?:</h3> * * <blockquote> * * <p> * ? {@link org.apache.commons.beanutils.converters.AbstractArrayConverter#parseElements(String) parseElements} * </p> * * <ul> * <li>The string is expected to be a comma-separated list of values.</li> * <li> <b>'{'</b> ?<b>'}'</b>.</li> * <li>???<b></b>.</li> * <li>Elements in the list may be delimited by single or double quotes. * Within a quoted elements, the normal Java escape sequences are valid.</li> * </ul> * * <p> * : * </p> * * <pre class="code"> * LOGGER.debug(JsonUtil.format(ConvertUtil.toStrings("{5,4, 8,2;8 9_5@3`a}"))); * </pre> * * <b>:</b> * * <pre class="code"> * ["5","4","8","2","8","9","5","3","a"] * </pre> * * </blockquote> * * @param toBeConvertedValue * the to be converted value * @return <code>toBeConvertedValue</code> null,null<br> * @see org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) * @see org.apache.commons.beanutils.converters.ArrayConverter#parseElements(Class, String) * @see #convert(Object, Class) * @since 1.4.0 */ public static String[] toStrings(Object toBeConvertedValue) { return convert(toBeConvertedValue, String[].class); } /** * ?Iterator. * * <h3>?:</h3> * * <blockquote> * <ul> * <li>?,{@link ConvertUtil#toStrings(Object)} ?</li> * <li></li> * <li>{@link java.util.Map}, {@link java.util.Map#values()} ?{@link java.util.Iterator}</li> * <li>{@link java.util.Collection}</li> * <li>{@link java.util.Iterator}</li> * <li>{@link java.util.Enumeration}</li> * <li>{@link java.util.Dictionary}</li> * <li>{@link org.w3c.dom.Node}</li> * <li>{@link org.w3c.dom.NodeList}</li> * </ul> * </blockquote> * * @param <T> * the generic type * @param toBeConvertedValue * <ul> * <li>?,{@link ConvertUtil#toStrings(Object)} ?</li> * <li></li> * <li>{@link java.util.Map}, {@link java.util.Map#values()} ?{@link java.util.Iterator}</li> * <li>{@link java.util.Collection}</li> * <li>{@link java.util.Iterator}</li> * <li>{@link java.util.Enumeration}</li> * <li>{@link java.util.Dictionary}</li> * <li>{@link org.w3c.dom.Node}</li> * <li>{@link org.w3c.dom.NodeList}</li> * </ul> * @return <code>toBeConvertedValue</code> null,null<br> * <code>toBeConvertedValue</code> ,?,??<br> * ?? {@link IteratorUtils#getIterator(Object)} * @see Collection#iterator() * @see EnumerationIterator#EnumerationIterator(Enumeration) * @see IteratorUtils#asIterator(Enumeration) * @see IteratorUtils#getIterator(Object) * @see "org.apache.taglibs.standard.tag.common.core.ForEachSupport#supportedTypeForEachIterator(Object)" * @since Commons Collections4.0 */ @SuppressWarnings("unchecked") public static <T> Iterator<T> toIterator(Object toBeConvertedValue) { if (null == toBeConvertedValue) { return null; } // ? if (toBeConvertedValue instanceof String) { return toIterator(toStrings(toBeConvertedValue)); } return (Iterator<T>) IteratorUtils.getIterator(toBeConvertedValue); } /** * <code>toBeConvertedValue</code> ? <code>targetType</code> . * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * ConvertUtil.convert("1", Integer.class) =1 * ConvertUtil.convert("", Integer.class) =0 * ConvertUtil.convert("1", Long.class) =1 * </pre> * * </blockquote> * * <h3>,??, Type[] ? Class []:</h3> * * <blockquote> * * ?: * * <pre class="code"> * * Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); * int length = actualTypeArguments.length; * Class{@code <?>}[] klasses = new Class{@code <?>}[length]; * for (int i = 0, j = length; i {@code <} j; ++i){ * klasses[i] = (Class{@code <?>}) actualTypeArguments[i]; * } * * return klasses; * * </pre> * * ???: * * <pre class="code"> * Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); * return convert(actualTypeArguments, Class[].class); * </pre> * * </blockquote> * * <h3>?:</h3> * * <blockquote> * * <ol> * * <li><code>targetType</code>?,<b>value</b>,<br> * <code>ConvertUtil.convert("zh_CN", Locale.class)</code> ?converter,"zh_CN". * </li> * * <li>??,</li> * * <li> <code>toBeConvertedValue</code> <code>toBeConvertedValue.getClass().isArray()</code> {@link Collection} * <blockquote> * * <dl> * <dt> <code>targetType</code> ?</dt> * <dd> * <p> * <span style="color:red">?</span>?,<br> * ??{@link AbstractConverter#convert(Class, Object)}, {@link AbstractConverter#convertArray(Object)} * </p> * </dd> * * <dt> <code>targetType</code> </dt> * <dd>?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) ArrayConverter#convertToType(Class, * Object)} targetType ,? <code>toBeConvertedValue</code>?, ? <code>toBeConvertedValue</code> ??</dd> * * </dl> * * </blockquote> * </li> * </ol> * </blockquote> * * @param <T> * the generic type * @param toBeConvertedValue * ??/ * @param targetType * ?? * @return <code>toBeConvertedValue</code> null,null<br> * <code>targetType</code> null, {@link NullPointerException}<br> * ? {@link org.apache.commons.beanutils.ConvertUtils#convert(Object, Class)} * @see org.apache.commons.beanutils.ConvertUtils#convert(Object, Class) * @see org.apache.commons.beanutils.converters.AbstractConverter#convert(Class, Object) * @see org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) */ @SuppressWarnings("unchecked") public static <T> T convert(Object toBeConvertedValue, Class<T> targetType) { Validate.notNull(targetType, "targetType can't be null!"); return null == toBeConvertedValue ? null : (T) ConvertUtils.convert(toBeConvertedValue, targetType); } /** * ? {@link Locale}. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * ConvertUtil.toLocale(null) = null * ConvertUtil.toLocale("zh_CN") = Locale.CHINA * </pre> * * </blockquote> * * @param locale * ? <b>null</b> ,<b></b> {@link Locale} * @return <code>locale</code> null, null<br> * <code>locale instanceof <span style="color:green">Locale</span></code>, <code>(Locale) locale</code><br> * <code>locale instanceof <span style="color:green">String</span></code>, {@link LocaleUtils#toLocale(String)}<br> * , {@link UnsupportedOperationException} * @see org.apache.commons.lang3.LocaleUtils#toLocale(String) * @since 1.7.2 */ public static Locale toLocale(Object locale) { if (null == locale) { return null; } if (locale instanceof Locale) { return (Locale) locale; } if (locale instanceof String) { return LocaleUtils.toLocale((String) locale); } throw new UnsupportedOperationException( "input param [locale] type is:[" + locale.getClass().getName() + "] not support!"); } }