As a general rule for runtime-retained annotations (e.g. for transaction control, authorization or service
- * exposure), always use the lookup methods on this class (e.g., {@link #findAnnotation(Method, Class)}, {@link
- * #getAnnotation(Method, Class)}, and {@link #getAnnotations(Method)}) instead of the plain annotation lookup
- * methods in the JDK. You can still explicitly choose between lookup on the given class level only ({@link
- * #getAnnotation(Method, Class)}) and lookup in the entire inheritance hierarchy of the given method ({@link
- * #findAnnotation(Method, Class)}).
- *
- * @author Rob Harrop
- * @author Juergen Hoeller
- * @author Sam Brannen
- * @author Mark Fisher
- * @author Chris Beams
- * @since 2.0
- * @see java.lang.reflect.Method#getAnnotations()
- * @see java.lang.reflect.Method#getAnnotation(Class)
- */
-public abstract class AnnotationUtils {
-
- /** The attribute name for annotations with a single element */
- static final String VALUE = "value";
-
- private static final Map Correctly handles bridge {@link Method Methods} generated by the compiler.
- * @param method the method to look for annotations on
- * @return the annotations found
- * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
- */
- public static Annotation[] getAnnotations(Method method) {
- return BridgeMethodResolver.findBridgedMethod(method).getAnnotations();
- }
-
- /**
- * Get a single {@link Annotation} of Correctly handles bridge {@link Method Methods} generated by the compiler.
- * @param method the method to look for annotations on
- * @param annotationType the annotation class to look for
- * @return the annotations found
- * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
- */
- public static A getAnnotation(Method method, Class annotationType) {
- Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
- A ann = resolvedMethod.getAnnotation(annotationType);
- if (ann == null) {
- for (Annotation metaAnn : resolvedMethod.getAnnotations()) {
- ann = metaAnn.annotationType().getAnnotation(annotationType);
- if (ann != null) {
- break;
- }
- }
- }
- return ann;
- }
-
- /**
- * Get a single {@link Annotation} of Annotations on methods are not inherited by default, so we need to handle this explicitly.
- * @param method the method to look for annotations on
- * @param annotationType the annotation class to look for
- * @return the annotation found, or This method explicitly handles class-level annotations which are not declared as
- * {@link java.lang.annotation.Inherited inherited} as well as annotations on interfaces.
- * The algorithm operates as follows: Searches for an annotation on the given class and returns
- * it if found. Else searches all interfaces that the given class declares, returning the annotation
- * from the first matching candidate, if any. Else proceeds with introspection of the superclass
- * of the given class, checking the superclass itself; if no annotation found there, proceeds
- * with the interfaces that the superclass declares. Recursing up through the entire superclass
- * hierarchy if no match is found.
- * @param clazz the class to look for annotations on
- * @param annotationType the annotation class to look for
- * @return the annotation found, or If the supplied The standard {@link Class} API does not provide a mechanism for determining which class
- * in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle
- * this explicitly.
- * @param annotationType the Class object corresponding to the annotation type
- * @param clazz the Class object corresponding to the class on which to check for the annotation,
- * or Note: This method does not determine if the annotation is
- * {@link java.lang.annotation.Inherited inherited}. For greater clarity regarding inherited
- * annotations, consider using {@link #isAnnotationInherited(Class, Class)} instead.
- * @param annotationType the Class object corresponding to the annotation type
- * @param clazz the Class object corresponding to the class on which to check for the annotation
- * @return If the supplied Note: As of Spring 3.1.1, the returned map is actually an
- * {@link AnnotationAttributes} instance, however the Map signature of this method has
- * been preserved for binary compatibility.
- * @param annotation the annotation to retrieve the attributes for
- * @return the Map of annotation attributes, with attribute names as keys and
- * corresponding attribute values as values
- */
- public static Map Note: As of Spring 3.1.1, the returned map is actually an
- * {@link AnnotationAttributes} instance, however the Map signature of this method has
- * been preserved for binary compatibility.
- * @param annotation the annotation to retrieve the attributes for
- * @param classValuesAsString whether to turn Class references into Strings (for
- * compatibility with {@link org.springframework.core.type.AnnotationMetadata} or to
- * preserve them as Class references
- * @return the Map of annotation attributes, with attribute names as keys and
- * corresponding attribute values as values
- */
- public static Map
- * 此方法比
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 如果未找到或数组为
- * 起始索引小于
- * 如果未找到或数组为
- * 起始索引小于
- * 如果数组为
- * 如果数组为
- * 例如:
- *
- * 例如:
- *
- * 此方法可以用来确定指定类型的参数
- * 对于
- * 此方法可以用来确定指定类型的参数
- * 按照如下规则:
- * If the class itself is an interface, it gets returned as sole interface.
- * @param clazz the class to analyze for interfaces
- * @return all interfaces that the given object implements as array
- */
- public static Class>[] getAllInterfacesForClass(Class> clazz) {
- return getAllInterfacesForClass(clazz, null);
- }
-
- /**
- * Return all interfaces that the given class implements as array,
- * including ones implemented by superclasses.
- * If the class itself is an interface, it gets returned as sole interface.
- * @param clazz the class to analyze for interfaces
- * @param classLoader the ClassLoader that the interfaces need to be visible in
- * (may be If the class itself is an interface, it gets returned as sole interface.
- * @param clazz the class to analyze for interfaces
- * @return all interfaces that the given object implements as Set
- */
- public static Set If the class itself is an interface, it gets returned as sole interface.
- * @param clazz the class to analyze for interfaces
- * @param classLoader the ClassLoader that the interfaces need to be visible in
- * (may be The goal of this class is to avoid runtime dependencies on a specific
- * Java version, while nevertheless using the best collection implementation
- * that is available at runtime.
- *
- * @author Juergen Hoeller
- * @author Arjen Poutsma
- * @since 1.1.1
- */
-public abstract class CollectionFactory {
-
- private static Class navigableSetClass = null;
-
- private static Class navigableMapClass = null;
-
- private static final Set Creates an ArrayList, TreeSet or linked Set for a List, SortedSet
- * or Set, respectively.
- * @param collection the original Collection object
- * @param initialCapacity the initial capacity
- * @return the new Collection instance
- * @see java.util.ArrayList
- * @see java.util.TreeSet
- * @see java.util.LinkedHashSet
- */
- @SuppressWarnings("unchecked")
- public static Collection createApproximateCollection(Object collection, int initialCapacity) {
- if (collection instanceof LinkedList) {
- return new LinkedList();
- }
- else if (collection instanceof List) {
- return new ArrayList(initialCapacity);
- }
- else if (collection instanceof SortedSet) {
- return new TreeSet(((SortedSet) collection).comparator());
- }
- else {
- return new LinkedHashSet(initialCapacity);
- }
- }
-
- /**
- * Create the most appropriate collection for the given collection type.
- * Creates an ArrayList, TreeSet or linked Set for a List, SortedSet
- * or Set, respectively.
- * @param collectionType the desired type of the target Collection
- * @param initialCapacity the initial capacity
- * @return the new Collection instance
- * @see java.util.ArrayList
- * @see java.util.TreeSet
- * @see java.util.LinkedHashSet
- */
- public static Collection createCollection(Class> collectionType, int initialCapacity) {
- if (collectionType.isInterface()) {
- if (List.class.equals(collectionType)) {
- return new ArrayList(initialCapacity);
- }
- else if (SortedSet.class.equals(collectionType) || collectionType.equals(navigableSetClass)) {
- return new TreeSet();
- }
- else if (Set.class.equals(collectionType) || Collection.class.equals(collectionType)) {
- return new LinkedHashSet(initialCapacity);
- }
- else {
- throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName());
- }
- }
- else {
- if (!Collection.class.isAssignableFrom(collectionType)) {
- throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
- }
- try {
- return (Collection) collectionType.newInstance();
- }
- catch (Exception ex) {
- throw new IllegalArgumentException("Could not instantiate Collection type: " + collectionType.getName());
- }
- }
- }
-
- /**
- * Determine whether the given map type is an approximable type,
- * i.e. a type that {@link #createApproximateMap} can approximate.
- * @param mapType the map type to check
- * @return Creates a TreeMap or linked Map for a SortedMap or Map, respectively.
- * @param map the original Map object
- * @param initialCapacity the initial capacity
- * @return the new Map instance
- * @see java.util.TreeMap
- * @see java.util.LinkedHashMap
- */
- @SuppressWarnings("unchecked")
- public static Map createApproximateMap(Object map, int initialCapacity) {
- if (map instanceof SortedMap) {
- return new TreeMap(((SortedMap) map).comparator());
- }
- else {
- return new LinkedHashMap(initialCapacity);
- }
- }
-
- /**
- * Create the most approximate map for the given map.
- * Creates a TreeMap or linked Map for a SortedMap or Map, respectively.
- * @param collectionType the desired type of the target Map
- * @param initialCapacity the initial capacity
- * @return the new Map instance
- * @see java.util.TreeMap
- * @see java.util.LinkedHashMap
- */
- public static Map createMap(Class> mapType, int initialCapacity) {
- if (mapType.isInterface()) {
- if (Map.class.equals(mapType)) {
- return new LinkedHashMap(initialCapacity);
- }
- else if (SortedMap.class.equals(mapType) || mapType.equals(navigableMapClass)) {
- return new TreeMap();
- }
- else {
- throw new IllegalArgumentException("Unsupported Map interface: " + mapType.getName());
- }
- }
- else {
- if (!Map.class.isAssignableFrom(mapType)) {
- throw new IllegalArgumentException("Unsupported Map type: " + mapType.getName());
- }
- try {
- return (Map) mapType.newInstance();
- }
- catch (Exception ex) {
- throw new IllegalArgumentException("Could not instantiate Map type: " + mapType.getName());
- }
- }
- }
-
-
- /**
- * ConcurrentMap adapter for the JDK ConcurrentHashMap class.
- */
- @Deprecated
- private static class JdkConcurrentHashMap extends ConcurrentHashMap implements ConcurrentMap {
-
- private JdkConcurrentHashMap(int initialCapacity) {
- super(initialCapacity);
- }
- }
-
-}
diff --git a/src/main/java/com/zero/utils/CollectionUtil.java b/src/main/java/com/zero/utils/CollectionUtil.java
deleted file mode 100644
index 8cce738..0000000
--- a/src/main/java/com/zero/utils/CollectionUtil.java
+++ /dev/null
@@ -1,297 +0,0 @@
-/**
- * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
- *
- * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
- *
- * 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.zero.utils;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.TreeSet;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * 方便创建容器对象的工具。
- *
- * @author renhui
- */
-public final class CollectionUtil {
- /** 创建一个
- * 和{@code createArrayList(args)}不同,本方法会返回一个不可变长度的列表,且性能高于
- * {@code createArrayList(args)}。
- *
- * 该方法返回以“
- * 该方法返回以“
- * 该方法返回不以“
- * 该方法返回不以“annotationType from the supplied {@link Method}.
- * annotationType from the supplied {@link Method},
- * traversing its super methods if no annotation can be found on the given method itself.
- * null if none found
- */
- public static A findAnnotation(Method method, Class annotationType) {
- A annotation = getAnnotation(method, annotationType);
- Class> cl = method.getDeclaringClass();
- if (annotation == null) {
- annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
- }
- while (annotation == null) {
- cl = cl.getSuperclass();
- if (cl == null || cl == Object.class) {
- break;
- }
- try {
- Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
- annotation = getAnnotation(equivalentMethod, annotationType);
- if (annotation == null) {
- annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
- }
- }
- catch (NoSuchMethodException ex) {
- // We're done...
- }
- }
- return annotation;
- }
-
- private static A searchOnInterfaces(Method method, Class annotationType, Class>[] ifcs) {
- A annotation = null;
- for (Class> iface : ifcs) {
- if (isInterfaceWithAnnotatedMethods(iface)) {
- try {
- Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
- annotation = getAnnotation(equivalentMethod, annotationType);
- }
- catch (NoSuchMethodException ex) {
- // Skip this interface - it doesn't have the method...
- }
- if (annotation != null) {
- break;
- }
- }
- }
- return annotation;
- }
-
- private static boolean isInterfaceWithAnnotatedMethods(Class> iface) {
- synchronized (annotatedInterfaceCache) {
- Boolean flag = annotatedInterfaceCache.get(iface);
- if (flag != null) {
- return flag;
- }
- boolean found = false;
- for (Method ifcMethod : iface.getMethods()) {
- if (ifcMethod.getAnnotations().length > 0) {
- found = true;
- break;
- }
- }
- annotatedInterfaceCache.put(iface, found);
- return found;
- }
- }
-
- /**
- * Find a single {@link Annotation} of annotationType from the supplied {@link Class},
- * traversing its interfaces and superclasses if no annotation can be found on the given class itself.
- * null if none found
- */
- public static A findAnnotation(Class> clazz, Class annotationType) {
- Assert.assertNotNull(clazz, "Class must not be null");
- A annotation = clazz.getAnnotation(annotationType);
- if (annotation != null) {
- return annotation;
- }
- for (Class> ifc : clazz.getInterfaces()) {
- annotation = findAnnotation(ifc, annotationType);
- if (annotation != null) {
- return annotation;
- }
- }
- if (!Annotation.class.isAssignableFrom(clazz)) {
- for (Annotation ann : clazz.getAnnotations()) {
- annotation = findAnnotation(ann.annotationType(), annotationType);
- if (annotation != null) {
- return annotation;
- }
- }
- }
- Class> superClass = clazz.getSuperclass();
- if (superClass == null || superClass == Object.class) {
- return null;
- }
- return findAnnotation(superClass, annotationType);
- }
-
- /**
- * Find the first {@link Class} in the inheritance hierarchy of the specified clazz
- * (including the specified clazz itself) which declares an annotation for the
- * specified annotationType, or null if not found. If the supplied
- * clazz is null, null will be returned.
- * clazz is an interface, only the interface itself will be checked;
- * the inheritance hierarchy for interfaces will not be traversed.
- * null
- * @return the first {@link Class} in the inheritance hierarchy of the specified clazz
- * which declares an annotation for the specified annotationType, or null
- * if not found
- * @see Class#isAnnotationPresent(Class)
- * @see Class#getDeclaredAnnotations()
- */
- public static Class> findAnnotationDeclaringClass(Class extends Annotation> annotationType, Class> clazz) {
- Assert.assertNotNull(annotationType, "Annotation type must not be null");
- if (clazz == null || clazz.equals(Object.class)) {
- return null;
- }
- return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz :
- findAnnotationDeclaringClass(annotationType, clazz.getSuperclass());
- }
-
- /**
- * Determine whether an annotation for the specified annotationType is
- * declared locally on the supplied clazz. The supplied {@link Class}
- * may represent any type.
- * true if an annotation for the specified annotationType
- * is declared locally on the supplied clazz
- * @see Class#getDeclaredAnnotations()
- * @see #isAnnotationInherited(Class, Class)
- */
- public static boolean isAnnotationDeclaredLocally(Class extends Annotation> annotationType, Class> clazz) {
- Assert.assertNotNull(annotationType, "Annotation type must not be null");
- Assert.assertNotNull(clazz, "Class must not be null");
- boolean declaredLocally = false;
- for (Annotation annotation : Arrays.asList(clazz.getDeclaredAnnotations())) {
- if (annotation.annotationType().equals(annotationType)) {
- declaredLocally = true;
- break;
- }
- }
- return declaredLocally;
- }
-
- /**
- * Determine whether an annotation for the specified annotationType is present
- * on the supplied clazz and is {@link java.lang.annotation.Inherited inherited}
- * i.e., not declared locally for the class).
- * clazz is an interface, only the interface itself will be checked.
- * In accordance with standard meta-annotation semantics, the inheritance hierarchy for interfaces
- * will not be traversed. See the {@link java.lang.annotation.Inherited JavaDoc} for the
- * @Inherited meta-annotation for further details regarding annotation inheritance.
- * @param annotationType the Class object corresponding to the annotation type
- * @param clazz the Class object corresponding to the class on which to check for the annotation
- * @return true if an annotation for the specified annotationType is present
- * on the supplied clazz and is {@link java.lang.annotation.Inherited inherited}
- * @see Class#isAnnotationPresent(Class)
- * @see #isAnnotationDeclaredLocally(Class, Class)
- */
- public static boolean isAnnotationInherited(Class extends Annotation> annotationType, Class> clazz) {
- Assert.assertNotNull(annotationType, "Annotation type must not be null");
- Assert.assertNotNull(clazz, "Class must not be null");
- return (clazz.isAnnotationPresent(annotationType) && !isAnnotationDeclaredLocally(annotationType, clazz));
- }
-
- /**
- * Retrieve the given annotation's attributes as a Map, preserving all attribute types
- * as-is.
- * "value" attribute of a
- * single-element Annotation, given an annotation instance.
- * @param annotation the annotation instance from which to retrieve the value
- * @return the attribute value, or null if not found
- * @see #getValue(Annotation, String)
- */
- public static Object getValue(Annotation annotation) {
- return getValue(annotation, VALUE);
- }
-
- /**
- * Retrieve the value of a named Annotation attribute, given an annotation instance.
- * @param annotation the annotation instance from which to retrieve the value
- * @param attributeName the name of the attribute value to retrieve
- * @return the attribute value, or null if not found
- * @see #getValue(Annotation)
- */
- public static Object getValue(Annotation annotation, String attributeName) {
- try {
- Method method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]);
- return method.invoke(annotation);
- }
- catch (Exception ex) {
- return null;
- }
- }
-
- /**
- * Retrieve the default value of the "value" attribute
- * of a single-element Annotation, given an annotation instance.
- * @param annotation the annotation instance from which to retrieve the default value
- * @return the default value, or null if not found
- * @see #getDefaultValue(Annotation, String)
- */
- public static Object getDefaultValue(Annotation annotation) {
- return getDefaultValue(annotation, VALUE);
- }
-
- /**
- * Retrieve the default value of a named Annotation attribute, given an annotation instance.
- * @param annotation the annotation instance from which to retrieve the default value
- * @param attributeName the name of the attribute value to retrieve
- * @return the default value of the named attribute, or null if not found
- * @see #getDefaultValue(Class, String)
- */
- public static Object getDefaultValue(Annotation annotation, String attributeName) {
- return getDefaultValue(annotation.annotationType(), attributeName);
- }
-
- /**
- * Retrieve the default value of the "value" attribute
- * of a single-element Annotation, given the {@link Class annotation type}.
- * @param annotationType the annotation type for which the default value should be retrieved
- * @return the default value, or null if not found
- * @see #getDefaultValue(Class, String)
- */
- public static Object getDefaultValue(Class extends Annotation> annotationType) {
- return getDefaultValue(annotationType, VALUE);
- }
-
- /**
- * Retrieve the default value of a named Annotation attribute, given the {@link Class annotation type}.
- * @param annotationType the annotation type for which the default value should be retrieved
- * @param attributeName the name of the attribute value to retrieve.
- * @return the default value of the named attribute, or null if not found
- * @see #getDefaultValue(Annotation, String)
- */
- public static Object getDefaultValue(Class extends Annotation> annotationType, String attributeName) {
- try {
- Method method = annotationType.getDeclaredMethod(attributeName, new Class[0]);
- return method.getDefaultValue();
- }
- catch (Exception ex) {
- return null;
- }
- }
-
-
-}
diff --git a/src/main/java/com/zero/utils/ArrayUtil.java b/src/main/java/com/zero/utils/ArrayUtil.java
deleted file mode 100644
index 7bf09da..0000000
--- a/src/main/java/com/zero/utils/ArrayUtil.java
+++ /dev/null
@@ -1,3287 +0,0 @@
-/**
- * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
- *
- * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
- *
- * 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.zero.utils;
-
-import java.util.List;
-
-
-/**
- * 有关数组处理的工具类。
- */
-public class ArrayUtil {
-
- private static final Object[] EMPTY_ARRAY=new Object[0];
- // ==========================================================================
- // 取得数组长度。
- // ==========================================================================
-
- /**
- * 取得数组的长度。
- * Array.getLength()要快得多。
- * 0。
- */
- public static int arrayLength(Object array) {
- return arrayLength(array, 0, 0);
- }
-
- private static int arrayLength(Object array, int defaultIfNull, int defaultIfNotArray) {
- if (array == null) {
- return defaultIfNull; // null
- } else if (array instanceof Object[]) {
- return ((Object[]) array).length;
- } else if (array instanceof long[]) {
- return ((long[]) array).length;
- } else if (array instanceof int[]) {
- return ((int[]) array).length;
- } else if (array instanceof short[]) {
- return ((short[]) array).length;
- } else if (array instanceof byte[]) {
- return ((byte[]) array).length;
- } else if (array instanceof double[]) {
- return ((double[]) array).length;
- } else if (array instanceof float[]) {
- return ((float[]) array).length;
- } else if (array instanceof boolean[]) {
- return ((boolean[]) array).length;
- } else if (array instanceof char[]) {
- return ((char[]) array).length;
- } else {
- return defaultIfNotArray; // not an array
- }
- }
-
- // ==========================================================================
- // 判空函数。
- //
- // 判断一个数组是否为null或包含0个元素。
- // ==========================================================================
-
- /**
- * 检查数组是否为null或空数组[]。
- *
- *
- * ArrayUtil.isEmptyArray(null) = true
- * ArrayUtil.isEmptyArray(new int[0]) = true
- * ArrayUtil.isEmptyArray(new int[10]) = false
- *
- *
- * @param array 要检查的数组
- * @return 如果为空, 则返回true
- */
- public static boolean isEmptyArray(Object array) {
- return arrayLength(array, 0, -1) == 0;
- }
-
- // ==========================================================================
- // 默认值函数。
- //
- // 当数组为空时,取得默认数组值。
- // 注:判断数组为null时,可用更通用的ObjectUtil.defaultIfNull。
- // ==========================================================================
-
- /**
- * 如果数组是null或空数组[],则返回指定数组默认值。
- *
- *
- * ArrayUtil.defaultIfEmpty(null, defaultArray) = defaultArray
- * ArrayUtil.defaultIfEmpty(new String[0], defaultArray) = 数组本身
- * ArrayUtil.defaultIfEmpty(new String[10], defaultArray) = 数组本身
- *
- *
- * @param array 要转换的数组
- * @param defaultArray 默认数组
- * @return 数组本身或默认数组
- */
- public static null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(Object[] array1, Object[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(long[] array1, long[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(int[] array1, int[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(short[] array1, short[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(byte[] array1, byte[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(double[] array1, double[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(float[] array1, float[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(boolean[] array1, boolean[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- /**
- * 判断两个数组是否具有相同的长度。如果数组为null则被看作长度为0。
- *
- * @param array1 数组1
- * @param array2 数组2
- * @return 如果两个数组长度相同,则返回true
- */
- public static boolean isArraySameLength(char[] array1, char[] array2) {
- int length1 = array1 == null ? 0 : array1.length;
- int length2 = array2 == null ? 0 : array2.length;
-
- return length1 == length2;
- }
-
- // ==========================================================================
- // 反转数组的元素顺序。
- // ==========================================================================
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(Object[] array) {
- if (array == null) {
- return;
- }
-
- Object tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(long[] array) {
- if (array == null) {
- return;
- }
-
- long tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(int[] array) {
- if (array == null) {
- return;
- }
-
- int tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(short[] array) {
- if (array == null) {
- return;
- }
-
- short tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(byte[] array) {
- if (array == null) {
- return;
- }
-
- byte tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(double[] array) {
- if (array == null) {
- return;
- }
-
- double tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(float[] array) {
- if (array == null) {
- return;
- }
-
- float tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(boolean[] array) {
- if (array == null) {
- return;
- }
-
- boolean tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- /**
- * 反转数组的元素顺序。如果数组为null,则什么也不做。
- *
- * @param array 要反转的数组
- */
- public static void arrayReverse(char[] array) {
- if (array == null) {
- return;
- }
-
- char tmp;
-
- for (int i = 0, j = array.length - 1; j > i; i++, j--) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:Object[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(Object[] array, Object objectToFind) {
- return arrayIndexOf(array, objectToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(Object[] array, Object[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(Object[] array, Object objectToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (objectToFind == null) {
- for (int i = startIndex; i < array.length; i++) {
- if (array[i] == null) {
- return i;
- }
- }
- } else {
- for (int i = startIndex; i < array.length; i++) {
- if (objectToFind.equals(array[i])) {
- return i;
- }
- }
- }
-
- return -1;
- }
-
-
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(Object[] array, Object objectToFind) {
- return arrayLastIndexOf(array, objectToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(Object[] array, Object[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(Object[] array, Object objectToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- if (objectToFind == null) {
- for (int i = startIndex; i >= 0; i--) {
- if (array[i] == null) {
- return i;
- }
- }
- } else {
- for (int i = startIndex; i >= 0; i--) {
- if (objectToFind.equals(array[i])) {
- return i;
- }
- }
- }
-
- return -1;
- }
-
-
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(Object[] array, Object objectToFind) {
- return arrayIndexOf(array, objectToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(Object[] array, Object[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:long[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(long[] array, long longToFind) {
- return arrayIndexOf(array, longToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(long[] array, long[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(long[] array, long longToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- for (int i = startIndex; i < array.length; i++) {
- if (longToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(long[] array, long[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- long first = arrayToFind[0];
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && array[i] != first) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (array[j++] != arrayToFind[k++]) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(long[] array, long longToFind) {
- return arrayLastIndexOf(array, longToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(long[] array, long[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(long[] array, long longToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- for (int i = startIndex; i >= 0; i--) {
- if (longToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(long[] array, long[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- long last = arrayToFind[lastIndex];
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && array[i] != last) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (array[j--] != arrayToFind[k--]) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(long[] array, long longToFind) {
- return arrayIndexOf(array, longToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(long[] array, long[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:int[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(int[] array, int intToFind) {
- return arrayIndexOf(array, intToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(int[] array, int[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(int[] array, int intToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- for (int i = startIndex; i < array.length; i++) {
- if (intToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(int[] array, int[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int first = arrayToFind[0];
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && array[i] != first) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (array[j++] != arrayToFind[k++]) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(int[] array, int intToFind) {
- return arrayLastIndexOf(array, intToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(int[] array, int[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(int[] array, int intToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- for (int i = startIndex; i >= 0; i--) {
- if (intToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(int[] array, int[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- int last = arrayToFind[lastIndex];
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && array[i] != last) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (array[j--] != arrayToFind[k--]) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(int[] array, int intToFind) {
- return arrayIndexOf(array, intToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(int[] array, int[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:short[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(short[] array, short shortToFind) {
- return arrayIndexOf(array, shortToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(short[] array, short[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(short[] array, short shortToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- for (int i = startIndex; i < array.length; i++) {
- if (shortToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(short[] array, short[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- short first = arrayToFind[0];
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && array[i] != first) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (array[j++] != arrayToFind[k++]) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(short[] array, short shortToFind) {
- return arrayLastIndexOf(array, shortToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(short[] array, short[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(short[] array, short shortToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- for (int i = startIndex; i >= 0; i--) {
- if (shortToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(short[] array, short[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- short last = arrayToFind[lastIndex];
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && array[i] != last) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (array[j--] != arrayToFind[k--]) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(short[] array, short shortToFind) {
- return arrayIndexOf(array, shortToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(short[] array, short[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:byte[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(byte[] array, byte byteToFind) {
- return arrayIndexOf(array, byteToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(byte[] array, byte[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(byte[] array, byte byteToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- for (int i = startIndex; i < array.length; i++) {
- if (byteToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(byte[] array, byte[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- byte first = arrayToFind[0];
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && array[i] != first) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (array[j++] != arrayToFind[k++]) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(byte[] array, byte byteToFind) {
- return arrayLastIndexOf(array, byteToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(byte[] array, byte[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(byte[] array, byte byteToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- for (int i = startIndex; i >= 0; i--) {
- if (byteToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(byte[] array, byte[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- byte last = arrayToFind[lastIndex];
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && array[i] != last) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (array[j--] != arrayToFind[k--]) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(byte[] array, byte byteToFind) {
- return arrayIndexOf(array, byteToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(byte[] array, byte[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:double[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double doubleToFind) {
- return arrayIndexOf(array, doubleToFind, 0, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double doubleToFind, double tolerance) {
- return arrayIndexOf(array, doubleToFind, 0, tolerance);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double[] arrayToFind, double tolerance) {
- return arrayIndexOf(array, arrayToFind, 0, tolerance);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double doubleToFind, int startIndex) {
- return arrayIndexOf(array, doubleToFind, startIndex, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double doubleToFind, int startIndex, double tolerance) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- double min = doubleToFind - tolerance;
- double max = doubleToFind + tolerance;
-
- for (int i = startIndex; i < array.length; i++) {
- if (array[i] >= min && array[i] <= max) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double[] arrayToFind, int startIndex) {
- return arrayIndexOf(array, arrayToFind, startIndex, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(double[] array, double[] arrayToFind, int startIndex, double tolerance) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- double firstMin = arrayToFind[0] - tolerance;
- double firstMax = arrayToFind[0] + tolerance;
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && (array[i] < firstMin || array[i] > firstMax)) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (Math.abs(array[j++] - arrayToFind[k++]) > tolerance) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double doubleToFind) {
- return arrayLastIndexOf(array, doubleToFind, Integer.MAX_VALUE, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double doubleToFind, double tolerance) {
- return arrayLastIndexOf(array, doubleToFind, Integer.MAX_VALUE, tolerance);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double[] arrayToFind, double tolerance) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE, tolerance);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double doubleToFind, int startIndex) {
- return arrayLastIndexOf(array, doubleToFind, startIndex, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double doubleToFind, int startIndex, double tolerance) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- double min = doubleToFind - tolerance;
- double max = doubleToFind + tolerance;
-
- for (int i = startIndex; i >= 0; i--) {
- if (array[i] >= min && array[i] <= max) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double[] arrayToFind, int startIndex) {
- return arrayLastIndexOf(array, arrayToFind, startIndex, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(double[] array, double[] arrayToFind, int startIndex, double tolerance) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- double lastMin = arrayToFind[lastIndex] - tolerance;
- double lastMax = arrayToFind[lastIndex] + tolerance;
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && (array[i] < lastMin || array[i] > lastMax)) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (Math.abs(array[j--] - arrayToFind[k--]) > tolerance) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(double[] array, double doubleToFind) {
- return arrayIndexOf(array, doubleToFind) != -1;
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(double[] array, double doubleToFind, double tolerance) {
- return arrayIndexOf(array, doubleToFind, tolerance) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(double[] array, double[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(double[] array, double[] arrayToFind, double tolerance) {
- return arrayIndexOf(array, arrayToFind, tolerance) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:float[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float floatToFind) {
- return arrayIndexOf(array, floatToFind, 0, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float floatToFind, float tolerance) {
- return arrayIndexOf(array, floatToFind, 0, tolerance);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float[] arrayToFind, float tolerance) {
- return arrayIndexOf(array, arrayToFind, 0, tolerance);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float floatToFind, int startIndex) {
- return arrayIndexOf(array, floatToFind, startIndex, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float floatToFind, int startIndex, float tolerance) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- float min = floatToFind - tolerance;
- float max = floatToFind + tolerance;
-
- for (int i = startIndex; i < array.length; i++) {
- if (array[i] >= min && array[i] <= max) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float[] arrayToFind, int startIndex) {
- return arrayIndexOf(array, arrayToFind, startIndex, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(float[] array, float[] arrayToFind, int startIndex, float tolerance) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- float firstMin = arrayToFind[0] - tolerance;
- float firstMax = arrayToFind[0] + tolerance;
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && (array[i] < firstMin || array[i] > firstMax)) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (Math.abs(array[j++] - arrayToFind[k++]) > tolerance) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float floatToFind) {
- return arrayLastIndexOf(array, floatToFind, Integer.MAX_VALUE, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float floatToFind, float tolerance) {
- return arrayLastIndexOf(array, floatToFind, Integer.MAX_VALUE, tolerance);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float[] arrayToFind, float tolerance) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE, tolerance);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float floatToFind, int startIndex) {
- return arrayLastIndexOf(array, floatToFind, startIndex, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float floatToFind, int startIndex, float tolerance) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- float min = floatToFind - tolerance;
- float max = floatToFind + tolerance;
-
- for (int i = startIndex; i >= 0; i--) {
- if (array[i] >= min && array[i] <= max) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float[] arrayToFind, int startIndex) {
- return arrayLastIndexOf(array, arrayToFind, startIndex, 0);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(float[] array, float[] arrayToFind, int startIndex, float tolerance) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- float lastMin = arrayToFind[lastIndex] - tolerance;
- float lastMax = arrayToFind[lastIndex] + tolerance;
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && (array[i] < lastMin || array[i] > lastMax)) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (Math.abs(array[j--] - arrayToFind[k--]) > tolerance) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(float[] array, float floatToFind) {
- return arrayIndexOf(array, floatToFind) != -1;
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(float[] array, float floatToFind, float tolerance) {
- return arrayIndexOf(array, floatToFind, tolerance) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(float[] array, float[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(float[] array, float[] arrayToFind, float tolerance) {
- return arrayIndexOf(array, arrayToFind, tolerance) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:boolean[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(boolean[] array, boolean booleanToFind) {
- return arrayIndexOf(array, booleanToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(boolean[] array, boolean[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(boolean[] array, boolean booleanToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- for (int i = startIndex; i < array.length; i++) {
- if (booleanToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(boolean[] array, boolean[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- boolean first = arrayToFind[0];
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && array[i] != first) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (array[j++] != arrayToFind[k++]) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(boolean[] array, boolean booleanToFind) {
- return arrayLastIndexOf(array, booleanToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(boolean[] array, boolean[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(boolean[] array, boolean booleanToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- for (int i = startIndex; i >= 0; i--) {
- if (booleanToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(boolean[] array, boolean[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- boolean last = arrayToFind[lastIndex];
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && array[i] != last) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (array[j--] != arrayToFind[k--]) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(boolean[] array, boolean booleanToFind) {
- return arrayIndexOf(array, booleanToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(boolean[] array, boolean[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- // ==========================================================================
- // 在数组中查找一个元素或一个元素序列。
- //
- // 类型:char[]
- // ==========================================================================
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(char[] array, char charToFind) {
- return arrayIndexOf(array, charToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(char[] array, char[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind, 0);
- }
-
- /**
- * 在数组中查找一个元素。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(char[] array, char charToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- for (int i = startIndex; i < array.length; i++) {
- if (charToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中查找一个元素序列。
- * null则返回-1。
- * 0则看作0,超出数组长度的起始索引则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayIndexOf(char[] array, char[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- if (startIndex >= sourceLength) {
- return targetLength == 0 ? sourceLength : -1;
- }
-
- if (startIndex < 0) {
- startIndex = 0;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- char first = arrayToFind[0];
- int i = startIndex;
- int max = sourceLength - targetLength;
-
- startSearchForFirst:
- while (true) {
- // 查找第一个元素
- while (i <= max && array[i] != first) {
- i++;
- }
-
- if (i > max) {
- return -1;
- }
-
- // 已经找到第一个元素,接着找
- int j = i + 1;
- int end = j + targetLength - 1;
- int k = 1;
-
- while (j < end) {
- if (array[j++] != arrayToFind[k++]) {
- i++;
-
- // 重新查找第一个元素
- continue startSearchForFirst;
- }
- }
-
- // 找到了
- return i;
- }
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(char[] array, char charToFind) {
- return arrayLastIndexOf(array, charToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(char[] array, char[] arrayToFind) {
- return arrayLastIndexOf(array, arrayToFind, Integer.MAX_VALUE);
- }
-
- /**
- * 在数组中从末尾开始查找一个元素。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(char[] array, char charToFind, int startIndex) {
- if (array == null) {
- return -1;
- }
-
- if (startIndex < 0) {
- return -1;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
-
- for (int i = startIndex; i >= 0; i--) {
- if (charToFind == array[i]) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 在数组中从末尾开始查找一个元素序列。
- * null则返回-1。
- * 0则返回-1,超出数组长度的起始索引则从数组末尾开始找。
- * null或未找到,则返回-1。
- */
- public static int arrayLastIndexOf(char[] array, char[] arrayToFind, int startIndex) {
- if (array == null || arrayToFind == null) {
- return -1;
- }
-
- int sourceLength = array.length;
- int targetLength = arrayToFind.length;
-
- int rightIndex = sourceLength - targetLength;
-
- if (startIndex < 0) {
- return -1;
- }
-
- if (startIndex > rightIndex) {
- startIndex = rightIndex;
- }
-
- if (targetLength == 0) {
- return startIndex;
- }
-
- int lastIndex = targetLength - 1;
- char last = arrayToFind[lastIndex];
- int min = targetLength - 1;
- int i = min + startIndex;
-
- startSearchForLast:
- while (true) {
- while (i >= min && array[i] != last) {
- i--;
- }
-
- if (i < min) {
- return -1;
- }
-
- int j = i - 1;
- int start = j - (targetLength - 1);
- int k = lastIndex - 1;
-
- while (j > start) {
- if (array[j--] != arrayToFind[k--]) {
- i--;
- continue startSearchForLast;
- }
- }
-
- return start + 1;
- }
- }
-
- /**
- * 判断指定对象是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(char[] array, char charToFind) {
- return arrayIndexOf(array, charToFind) != -1;
- }
-
- /**
- * 判断指定元素序列是否存在于指定数组中。
- * null则返回false。
- * true
- */
- public static boolean arrayContains(char[] array, char[] arrayToFind) {
- return arrayIndexOf(array, arrayToFind) != -1;
- }
-
- @SuppressWarnings("unchecked")
- public static Object[] toArray(Object value){
- if(value==null){
- return EMPTY_ARRAY;
- }
- Object[] values;
- if(value.getClass().isArray()){
- values=(Object[])value;
- }else if(List.class.isInstance(value)){
- List
- * ClassUtil.getPrimitiveWrapperType(int.class) = Integer.class;
- * ClassUtil.getPrimitiveWrapperType(int[].class) = int[].class;
- * ClassUtil.getPrimitiveWrapperType(int[][].class) = int[][].class;
- * ClassUtil.getPrimitiveWrapperType(String[][].class) = String[][].class;
- *
- *
- * null。
- *
- * ClassUtil.getPrimitiveDefaultValue(int.class) = 0;
- * ClassUtil.getPrimitiveDefaultValue(boolean.class) = false;
- * ClassUtil.getPrimitiveDefaultValue(char.class) = '\0';
- *
- *
- * fromClasses 的对象是否可以赋值给另一组类型 classes。
- * object1, object2, ... 是否可以用来调用确定参数类型为
- * class1, class2,
- * ... 的方法。
- * fromClasses 的每个元素 fromClass 和
- * classes 的每个元素 clazz, 按照如下规则:
- *
- *
- * clazz 为 null ,总是返回 false
- * 。fromClass 为 null ,并且目标类型
- * clazz 为非原子类型,则返回 true。 因为 null
- * 可以被赋给任何引用类型。Class.isAssignableFrom 方法来确定目标类 clazz
- * 是否和参数类 fromClass 相同或是其父类、接口,如果是,则返回 true。clazz 为原子类型,那么根据 The Java Language
- * Specification ,sections 5.1.1, 5.1.2, 5.1.4定义的Widening Primitive
- * Conversion规则,参数类型 fromClass 可以是任何能扩展成该目标类型的原子类型及其包装类。 例如,
- * clazz 为 long ,那么参数类型可以是 byte、
- * short、int、long、char
- * 及其包装类 java.lang.Byte、java.lang.Short、
- * java.lang.Integer、 java.lang.Long 和
- * java.lang.Character 。如果满足这个条件,则返回 true。false。null 总是返回 false
- * @param fromClasses 参数类型列表, null 表示可赋值给任意非原子类型
- * @return 如果可以被赋值,则返回 true
- */
- public static boolean isAssignable(Class>[] classes, Class>[] fromClasses) {
- if (!isArraySameLength(fromClasses, classes)) {
- return false;
- }
-
- if (fromClasses == null) {
- fromClasses = EMPTY_CLASS_ARRAY;
- }
-
- if (classes == null) {
- classes = EMPTY_CLASS_ARRAY;
- }
-
- for (int i = 0; i < fromClasses.length; i++) {
- if (isAssignable(classes[i], fromClasses[i]) == false) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * 检查指定类型 fromClass 的对象是否可以赋值给另一种类型 clazz。
- * object1, object2, ... 是否可以用来调用确定参数类型
- * class1, class2,
- * ... 的方法。
- *
- *
- * clazz 为 null ,总是返回 false
- * 。fromClass 为 null ,并且目标类型
- * clazz 为非原子类型,则返回 true。 因为 null
- * 可以被赋给任何引用类型。Class.isAssignableFrom 方法来确定目标类 clazz
- * 是否和参数类 fromClass 相同或是其父类、接口,如果是,则返回 true。clazz 为原子类型,那么根据 The Java Language
- * Specification ,sections 5.1.1, 5.1.2, 5.1.4定义的Widening Primitive
- * Conversion规则,参数类型 fromClass 可以是任何能扩展成该目标类型的原子类型及其包装类。 例如,
- * clazz 为 long ,那么参数类型可以是 byte、
- * short、int、long、char
- * 及其包装类 java.lang.Byte、java.lang.Short、
- * java.lang.Integer、 java.lang.Long 和
- * java.lang.Character 。如果满足这个条件,则返回 true。false。null 总是返回 false
- * @param fromClass 参数类型, null 表示可赋值给任意非原子类型
- * @return 如果可以被赋值,则返回 null
- */
- public static boolean isAssignable(Class> clazz, Class> fromClass) {
- if (clazz == null) {
- return false;
- }
-
- // 如果fromClass是null,只要clazz不是原子类型如int,就一定可以赋值
- if (fromClass == null) {
- return !clazz.isPrimitive();
- }
-
- // 如果类相同或有父子关系,当然可以赋值
- if (clazz.isAssignableFrom(fromClass)) {
- return true;
- }
-
- // 对于原子类型,根据JLS的规则进行扩展
- // 目标class为原子类型时,fromClass可以为原子类型和原子类型的包装类型。
- if (clazz.isPrimitive()) {
- return assignmentTable.get(clazz).contains(fromClass);
- }
-
- return false;
- }
-
- private final static Mapnull when accepting all declared interfaces)
- * @return all interfaces that the given object implements as array
- */
- public static Class>[] getAllInterfacesForClass(Class> clazz, ClassLoader classLoader) {
- Setnull when accepting all declared interfaces)
- * @return all interfaces that the given object implements as Set
- */
- public static Setnull,
- * in which case this method will always return true)
- */
- public static boolean isVisible(Class> clazz, ClassLoader classLoader) {
- if (classLoader == null) {
- return true;
- }
- try {
- Class> actualClass = classLoader.loadClass(clazz.getName());
- return (clazz == actualClass);
- // Else: different interface class found...
- }
- catch (ClassNotFoundException ex) {
- // No interface class found...
- return false;
- }
- }
-
- /**
- * Determine the name of the class file, relative to the containing
- * package: e.g. "String.class"
- * @param clazz the class
- * @return the file name of the ".class" file
- */
- public static String getClassFileName(Class clazz) {
- String className = clazz.getName();
- int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
- return className.substring(lastDotIndex + 1) + CLASS_FILE_SUFFIX;
- }
-}
diff --git a/src/main/java/com/zero/utils/CollectionFactory.java b/src/main/java/com/zero/utils/CollectionFactory.java
deleted file mode 100644
index 85871eb..0000000
--- a/src/main/java/com/zero/utils/CollectionFactory.java
+++ /dev/null
@@ -1,324 +0,0 @@
-/**
- * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
- *
- * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
- *
- * 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.zero.utils;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.IdentityHashMap;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedMap;
-import java.util.SortedSet;
-import java.util.TreeMap;
-import java.util.TreeSet;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.CopyOnWriteArraySet;
-
-/**
- * Factory for collections, being aware of Java 5 and Java 6 collections.
- * Mainly for internal use within the framework.
- *
- * true if the type is approximable,
- * false if it is not
- */
- public static boolean isApproximableCollectionType(Class> collectionType) {
- return (collectionType != null && approximableCollectionTypes.contains(collectionType));
- }
-
- /**
- * Create the most approximate collection for the given collection.
- * true if the type is approximable,
- * false if it is not
- */
- public static boolean isApproximableMapType(Class> mapType) {
- return (mapType != null && approximableMapTypes.contains(mapType));
- }
-
- /**
- * Create the most approximate map for the given map.
- * ArrayList。 */
- public static ArrayList。 */
- public static ArrayList。 */
- public static ArrayList。 */
- public static LinkedList。 */
- public static LinkedList。 */
- public static LinkedList。 */
- public static List。
- * HashMap。 */
- public static HashMap。 */
- public static LinkedHashMap。 */
- public static LinkedHashMap。 */
- public static TreeMap。 */
- public static TreeMap。 */
- public static ConcurrentHashMap。 */
- public static HashSet。 */
- public static HashSet。 */
- public static HashSet。 */
- public static LinkedHashSet。 */
- public static LinkedHashSet。 */
- public static LinkedHashSet。 */
- public static TreeSet。 */
- public static TreeSet。 */
- @SuppressWarnings("unchecked")
- public static TreeSet。 */
- public static TreeSet。 */
- public static TreeSet。 */
- public static TreeSet。 */
- public static true if the supplied Collection is null
- * or empty. Otherwise, return false.
- * @param collection the Collection to check
- * @return whether the given Collection is empty
- */
- public static boolean isEmpty(Collection collection) {
- return (collection == null || collection.isEmpty());
- }
-
- /**
- * Return true if the supplied Map is null
- * or empty. Otherwise, return false.
- * @param map the Map to check
- * @return whether the given Map is empty
- */
- public static boolean isEmpty(Map map) {
- return (map == null || map.isEmpty());
- }
-
-
-}
diff --git a/src/main/java/com/zero/utils/DateUtils.java b/src/main/java/com/zero/utils/DateUtils.java
deleted file mode 100644
index e45b5e3..0000000
--- a/src/main/java/com/zero/utils/DateUtils.java
+++ /dev/null
@@ -1,377 +0,0 @@
-package com.zero.utils;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-
-public class DateUtils {
-
- public static enum Type {
- Year, Month, Week, Day, Hour, Minutes, Seconds;
- }
-
- /**
- * 获取当前时间
- * y 年 M 月 d 日 H 24小时制 h 12小时制 m 分 s 秒
- *
- * @param format
- * 日期格式
- * @return String
- */
- public static String getCurrentDate(String format) {
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- return sdf.format(new Date());
- }
-
- /**
- * 获取制定日期的格式化字符串
- *
- * @param date
- * Date 日期
- * @param format
- * String 格式
- * @return String
- */
- public static String getFormatedDate(Date date, String format) {
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- return sdf.format(date);
- }
-
- /**
- * 判断哪个日期在前 如果日期一在日期二之前,返回true,否则返回false
- *
- * @param date1
- * 日期一
- * @param date2
- * 日期二
- * @return boolean
- */
- public static boolean isBefore(Date date1, Date date2) {
- Calendar c1 = Calendar.getInstance();
- c1.setTime(date1);
-
- Calendar c2 = Calendar.getInstance();
- c2.setTime(date2);
-
- if (c1.before(c2))
- return true;
-
- return false;
- }
-
- /**
- * 将字符串转换成日期
- *
- * @param date
- * String 日期字符串
- * @return Date
- * @throws ParseException
- */
- public static Date parseDateFromString(String date) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.parse(date);
- }
-
- /**
- * 获取指定日期当月的最后一天
- *
- * @param date
- * @return
- */
- public static Date lastDayOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MONTH, 1);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- cal.add(Calendar.DAY_OF_MONTH, -1);
- return cal.getTime();
- }
-
- /**
- * 获取指定日期当月的第一天
- *
- * @param date
- * @return
- */
- public static Date firstDayOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- return cal.getTime();
- }
-
- /**
- * 是否是闰年
- *
- * @param year
- * 年份
- * @return boolean
- */
- public static boolean isLeapYear(int year) {
- GregorianCalendar calendar = new GregorianCalendar();
- return calendar.isLeapYear(year);
- }
-
- /**
- * 获取指定日期之前或者之后多少天的日期
- *
- * @param day
- * 指定的时间
- * @param offset
- * 日期偏移量,正数表示延后,负数表示天前
- * @return Date
- */
- public static Date getDateByOffset(Date day, int offset) {
- Calendar c = Calendar.getInstance();
- c.setTime(day);
- c.add(Calendar.DAY_OF_MONTH, offset);
- return c.getTime();
- }
-
- /**
- * 获取一天开始时间 如 2014-12-12 00:00:00
- *
- * @return
- */
- public static Date getDayStart() {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(new Date());
- calendar.set(Calendar.HOUR_OF_DAY, 0);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- return calendar.getTime();
- }
-
- /**
- * 获取一天结束时间 如 2014-12-12 23:59:59
- *
- * @return
- */
- public static Date getDayEnd() {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(new Date());
- calendar.set(Calendar.HOUR_OF_DAY, 23);
- calendar.set(Calendar.MINUTE, 59);
- calendar.set(Calendar.SECOND, 59);
- return calendar.getTime();
- }
-
- /**
- * 时间分段 比如:2014-12-12 10:00:00 ~ 2014-12-12 14:00:00 分成两段就是 2014-12-12
- * 10:00:00 ~ 2014-12-12 12:00:00 和2014-12-12 12:00:00 ~ 2014-12-12 14:00:00
- *
- * @param start
- * 起始日期
- * @param end
- * 结束日期
- * @param pieces
- * 分成几段
- */
- public static Date[] getDatePieces(Date start, Date end, int pieces) {
-
- Long sl = start.getTime();
- Long el = end.getTime();
-
- Long diff = el - sl;
-
- Long segment = diff / pieces;
-
- Date[] dateArray = new Date[pieces + 1];
-
- for (int i = 1; i <= pieces + 1; i++) {
- dateArray[i - 1] = new Date(sl + (i - 1) * segment);
- }
-
- // 校正最后结束日期的误差,可能会出现偏差,比如14:00:00 ,会变成13:59:59之类的
- dateArray[pieces] = end;
-
- return dateArray;
- }
-
- /**
- * 获取某个日期的当月第一天
- *
- * @return
- */
- public static Date getFirstDayOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- return cal.getTime();
- }
-
- /**
- * 获取某个日期的当月最后一天
- *
- * @return
- */
- public static Date getLastDayOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.add(Calendar.MONTH, 1);
- cal.set(Calendar.DAY_OF_MONTH, 0);
- return cal.getTime();
- }
-
- /**
- * 获取某个日期的当月第一天
- *
- * @return
- */
- public static Date getFirstDayOfMonth(int year, int month) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month - 1);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- return cal.getTime();
- }
-
- /**
- * 获取某个日期的当月最后一天
- *
- * @return
- */
- public static Date getLastDayOfMonth(int year, int month) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month);
- cal.set(Calendar.DAY_OF_MONTH, 0);
- return cal.getTime();
- }
-
- /**
- * 获取两个日期的时间差,可以指定年,月,周,日,时,分,秒
- * @param date1 第一个日期
- * @param date2 第二个日期此日期必须在date1之后
- * @param type DateUtils.Type.X的枚举类型
- * @return long值
- * @throws Exception
- */
- public static long getDiff(Date date1, Date date2, Type type) throws Exception {
-
- if (!isBefore(date1, date2))
- throw new Exception("第二个日期必须在第一个日期之后");
-
- long d = Math.abs(date1.getTime() - date2.getTime());
- switch (type) {
- case Year: {
- Calendar cal1 = Calendar.getInstance();
- Calendar cal2 = Calendar.getInstance();
-
- cal1.setTime(date1);
- int year1 = cal1.get(Calendar.YEAR);
- int month1 = cal1.get(Calendar.MONTH);
- int day1 = cal1.get(Calendar.DAY_OF_MONTH);
- int hour1 = cal1.get(Calendar.HOUR_OF_DAY);
- int minute1 = cal1.get(Calendar.MINUTE);
- int second1 = cal1.get(Calendar.SECOND);
-
- cal2.setTime(date2);
- int year2 = cal2.get(Calendar.YEAR);
- int month2 = cal2.get(Calendar.MONTH);
- int day2 = cal2.get(Calendar.DAY_OF_MONTH);
- int hour2 = cal2.get(Calendar.HOUR_OF_DAY);
- int minute2 = cal2.get(Calendar.MINUTE);
- int second2 = cal2.get(Calendar.SECOND);
-
- int yd = year2 - year1;
-
- if (month1 > month2) {
- yd -= 1;
- } else {
- if (day1 > day2) {
- yd -= 1;
- } else {
- if (hour1 > hour2) {
- yd -= 1;
- } else {
- if (minute1 > minute2) {
- yd -= 1;
- } else {
- if (second1 > second2) {
- yd -= 1;
- }
- }
- }
- }
- }
- return (long)yd;
- }
- case Month: {
- // 获取年份差
- long year = getDiff(date1, date2, Type.Year);
-
- Calendar cal1 = Calendar.getInstance();
- Calendar cal2 = Calendar.getInstance();
-
- cal1.setTime(date1);
- int month1 = cal1.get(Calendar.MONTH);
- int day1 = cal1.get(Calendar.DAY_OF_MONTH);
- int hour1 = cal1.get(Calendar.HOUR_OF_DAY);
- int minute1 = cal1.get(Calendar.MINUTE);
- int second1 = cal1.get(Calendar.SECOND);
-
- cal2.setTime(date2);
- int month2 = cal2.get(Calendar.MONTH);
- int day2 = cal2.get(Calendar.DAY_OF_MONTH);
- int hour2 = cal2.get(Calendar.HOUR_OF_DAY);
- int minute2 = cal2.get(Calendar.MINUTE);
- int second2 = cal2.get(Calendar.SECOND);
-
- int md = (month2 + 12) - month1;
-
- if (day1 > day2) {
- md -= 1;
- } else {
- if (hour1 > hour2) {
- md -= 1;
- } else {
- if (minute1 > minute2) {
- md -= 1;
- } else {
- if (second1 > second2) {
- md -= 1;
- }
- }
- }
- }
- return (long)md + year * 12;
- }
- case Week: {
- return getDiff(date1, date2, Type.Day) / 7;
- }
- case Day: {
- long d1 = date1.getTime();
- long d2 = date2.getTime();
- return (int) ((d2 - d1) / (24 * 60 * 60 * 1000));
- }
- case Hour: {
- long d1 = date1.getTime();
- long d2 = date2.getTime();
- return (int) ((d2 - d1) / (60 * 60 * 1000));
- }
- case Minutes: {
- long d1 = date1.getTime();
- long d2 = date2.getTime();
- return (int) ((d2 - d1) / (60 * 1000));
- }
- case Seconds: {
- long d1 = date1.getTime();
- long d2 = date2.getTime();
- return (int) ((d2 - d1) / 1000);
- }
- default:
- throw new Exception("请指定要获取的时间差的类型:年,月,天,周,时,分,秒");
- }
- }
-
- public static void main(String[] args) throws Exception {
- Date d = new Date();
- Calendar cal = Calendar.getInstance();
- cal.setTime(d);
- cal.add(Calendar.MONTH,1);
- System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
- System.out.println(getDiff(d, cal.getTime(), DateUtils.Type.Week));
- }
-}
diff --git a/src/main/java/com/zero/utils/Enumerator.java b/src/main/java/com/zero/utils/Enumerator.java
deleted file mode 100644
index bfa128a..0000000
--- a/src/main/java/com/zero/utils/Enumerator.java
+++ /dev/null
@@ -1,158 +0,0 @@
-/**
- * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
- *
- * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
- *
- * 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.zero.utils;
-
-
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Map;
-import java.util.NoSuchElementException;
-
-public class Enumerator implements Enumeration {
-
- // ----------------------------------------------------------- Constructors
-
- /**
- * Return an Enumeration over the values of the specified Collection.
- *
- * @param collection
- * Collection whose values should be enumerated
- */
- public Enumerator(Collection collection) {
-
- this(collection.iterator());
-
- }
-
- /**
- * Return an Enumeration over the values of the specified Collection.
- *
- * @param collection
- * Collection whose values should be enumerated
- * @param clone
- * true to clone iterator
- */
- public Enumerator(Collection collection, boolean clone) {
-
- this(collection.iterator(), clone);
-
- }
-
- /**
- * Return an Enumeration over the values returned by the specified Iterator.
- *
- * @param iterator
- * Iterator to be wrapped
- */
- public Enumerator(Iterator iterator) {
-
- super();
- this.iterator = iterator;
-
- }
-
- /**
- * Return an Enumeration over the values returned by the specified Iterator.
- *
- * @param iterator
- * Iterator to be wrapped
- * @param clone
- * true to clone iterator
- */
- @SuppressWarnings("unchecked")
- public Enumerator(Iterator iterator, boolean clone) {
-
- super();
- if (!clone) {
- this.iterator = iterator;
- } else {
- List list = new ArrayList();
- while (iterator.hasNext()) {
- list.add(iterator.next());
- }
- this.iterator = list.iterator();
- }
-
- }
-
- /**
- * Return an Enumeration over the values of the specified Map.
- *
- * @param map
- * Map whose values should be enumerated
- */
- public Enumerator(Map map) {
-
- this(map.values().iterator());
-
- }
-
- /**
- * Return an Enumeration over the values of the specified Map.
- *
- * @param map
- * Map whose values should be enumerated
- * @param clone
- * true to clone iterator
- */
- public Enumerator(Map map, boolean clone) {
-
- this(map.values().iterator(), clone);
-
- }
-
- // ----------------------------------------------------- Instance Variables
-
- /**
- * The Iterator over which the Enumeration
- * represented by this class actually operates.
- */
- private Iterator iterator = null;
-
- // --------------------------------------------------------- Public Methods
-
- /**
- * Tests if this enumeration contains more elements.
- *
- * @return true if and only if this enumeration object
- * contains at least one more element to provide, false
- * otherwise
- */
- public boolean hasMoreElements() {
-
- return (iterator.hasNext());
-
- }
-
- /**
- * Returns the next element of this enumeration if this enumeration has at
- * least one more element to provide.
- *
- * @return the next element of this enumeration
- *
- * @exception NoSuchElementException
- * if no more elements exist
- */
- public Object nextElement() throws NoSuchElementException {
-
- return (iterator.next());
-
- }
-
-}
diff --git a/src/main/java/com/zero/utils/FileUtil.java b/src/main/java/com/zero/utils/FileUtil.java
deleted file mode 100644
index 50b3d9b..0000000
--- a/src/main/java/com/zero/utils/FileUtil.java
+++ /dev/null
@@ -1,609 +0,0 @@
-/**
- * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
- *
- * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
- *
- * 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.zero.utils;
-
-
-
-import static com.zero.utils.StringUtil.*;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import com.zero.utils.exceptions.IllegalPathException;
-
-/**
- * 操作文件的工具类
- *
- * @author renhui
- *
- */
-public class FileUtil {
-
- public static String readFileContent(File file, String encoding)
- throws Exception {
- FileInputStream fis = new FileInputStream(file);
- return readStreamContent(fis, encoding);
- }
-
- public static String readStreamContent(InputStream stream, String encoding)
- throws Exception {
- StringBuilder content = new StringBuilder("");
- byte[] bytearray = new byte[stream.available()];
- int bytetotal = stream.available();
- while (stream.read(bytearray, 0, bytetotal) != -1) {
- String temp = new String(bytearray, 0, bytetotal, encoding);
- content.append(temp);
- }
- return content.toString();
- }
-
-
- // ==========================================================================
- // 规格化路径。
- // ==========================================================================
-
- /**
- * 规格化绝对路径。
- * /”开始的绝对路径。转换规则如下:
- *
- *
- *
- * @param path 要规格化的路径
- * @return 规格化后的路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String normalizeAbsolutePath(String path) throws IllegalPathException {
- return normalizePath(path, true, false, false);
- }
-
- /**
- * 规格化绝对路径。
- * ""。/”开始的绝对路径。转换规则如下:
- *
- *
- *
- * @param path 要规格化的路径
- * @param removeTrailingSlash 是否强制移除末尾的""。removeTrailingSlash==true)。"/"
- * @return 规格化后的路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String normalizeAbsolutePath(String path, boolean removeTrailingSlash) throws IllegalPathException {
- return normalizePath(path, true, false, removeTrailingSlash);
- }
-
- /**
- * 规格化相对路径。
- * /”开始的相对路径。转换规则如下:
- *
- *
- *
- * @param path 要规格化的路径
- * @return 规格化后的路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String normalizeRelativePath(String path) throws IllegalPathException {
- return normalizePath(path, false, true, false);
- }
-
- /**
- * 规格化相对路径。
- * ""。/”开始的相对路径。转换规则如下:
- *
- *
- *
- * @param path 要规格化的路径
- * @param removeTrailingSlash 是否强制移除末尾的""。removeTrailingSlash==true)。"/"
- * @return 规格化后的路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String normalizeRelativePath(String path, boolean removeTrailingSlash) throws IllegalPathException {
- return normalizePath(path, false, true, removeTrailingSlash);
- }
-
- /**
- * 规格化路径。规则如下:
- *
- *
- *
- * @param path 要规格化的路径
- * @return 规格化后的路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String normalizePath(String path) throws IllegalPathException {
- return normalizePath(path, false, false, false);
- }
-
- /**
- * 规格化路径。规则如下:
- * ""。
- *
- *
- * @param path 要规格化的路径
- * @param removeTrailingSlash 是否强制移除末尾的""。removeTrailingSlash==true)。"/"
- * @return 规格化后的路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String normalizePath(String path, boolean removeTrailingSlash) throws IllegalPathException {
- return normalizePath(path, false, false, removeTrailingSlash);
- }
-
- private static String normalizePath(String path, boolean forceAbsolute, boolean forceRelative,
- boolean removeTrailingSlash) throws IllegalPathException {
- char[] pathChars = trimToEmpty(path).toCharArray();
- int length = pathChars.length;
-
- // 检查绝对路径,以及path尾部的"/"
- boolean startsWithSlash = false;
- boolean endsWithSlash = false;
-
- if (length > 0) {
- char firstChar = pathChars[0];
- char lastChar = pathChars[length - 1];
-
- startsWithSlash = firstChar == '/' || firstChar == '\\';
- endsWithSlash = lastChar == '/' || lastChar == '\\';
- }
-
- StringBuilder buf = new StringBuilder(length);
- boolean isAbsolutePath = forceAbsolute || !forceRelative && startsWithSlash;
- int index = startsWithSlash ? 0 : -1;
- int level = 0;
-
- if (isAbsolutePath) {
- buf.append("/");
- }
-
- while (index < length) {
- // 跳到第一个非slash字符,或末尾
- index = indexOfSlash(pathChars, index + 1, false);
-
- if (index == length) {
- break;
- }
-
- // 取得下一个slash index,或末尾
- int nextSlashIndex = indexOfSlash(pathChars, index, true);
-
- String element = new String(pathChars, index, nextSlashIndex - index);
- index = nextSlashIndex;
-
- // 忽略"."
- if (".".equals(element)) {
- continue;
- }
-
- // 回朔".."
- if ("..".equals(element)) {
- if (level == 0) {
- // 如果是绝对路径,../试图越过最上层目录,这是不可能的,
- // 抛出路径非法的异常。
- if (isAbsolutePath) {
- throw new IllegalPathException(path);
- } else {
- buf.append("../");
- }
- } else {
- buf.setLength(pathChars[--level]);
- }
-
- continue;
- }
-
- // 添加到path
- pathChars[level++] = (char) buf.length(); // 将已经读过的chars空间用于记录指定level的index
- buf.append(element).append('/');
- }
-
- // 除去最后的"/"
- if (buf.length() > 0) {
- if (!endsWithSlash || removeTrailingSlash) {
- buf.setLength(buf.length() - 1);
- }
- }
-
- return buf.toString();
- }
-
- private static int indexOfSlash(char[] chars, int beginIndex, boolean slash) {
- int i = beginIndex;
-
- for (; i < chars.length; i++) {
- char ch = chars[i];
-
- if (slash) {
- if (ch == '/' || ch == '\\') {
- break; // if a slash
- }
- } else {
- if (ch != '/' && ch != '\\') {
- break; // if not a slash
- }
- }
- }
-
- return i;
- }
-
- // ==========================================================================
- // 取得基于指定basedir规格化路径。
- // ==========================================================================
-
- /**
- * 如果指定路径已经是绝对路径,则规格化后直接返回之,否则取得基于指定basedir的规格化路径。
- *
- * @param basedir 根目录,如果path为相对路径,表示基于此目录
- * @param path 要检查的路径
- * @return 规格化的绝对路径
- * @throws IllegalPathException 如果路径非法
- */
- public static String getAbsolutePathBasedOn(String basedir, String path) throws IllegalPathException {
- // 如果path为绝对路径,则规格化后返回
- boolean isAbsolutePath = false;
-
- path = trimToEmpty(path);
-
- if (path.length() > 0) {
- char firstChar = path.charAt(0);
- isAbsolutePath = firstChar == '/' || firstChar == '\\';
- }
-
- if (!isAbsolutePath) {
- // 如果path为相对路径,将它和basedir合并。
- if (path.length() > 0) {
- path = trimToEmpty(basedir) + "/" + path;
- } else {
- path = trimToEmpty(basedir);
- }
- }
-
- return normalizeAbsolutePath(path);
- }
-
- /**
- * 取得和系统相关的绝对路径。
- *
- * @throws IllegalPathException 如果basedir不是绝对路径
- */
- public static String getSystemDependentAbsolutePathBasedOn(String basedir, String path) {
- path = trimToEmpty(path);
-
- boolean endsWithSlash = path.endsWith("/") || path.endsWith("\\");
-
- File pathFile = new File(path);
-
- if (pathFile.isAbsolute()) {
- // 如果path已经是绝对路径了,则直接返回之。
- path = pathFile.getAbsolutePath();
- } else {
- // 否则以basedir为基本路径。
- // 下面确保basedir本身为绝对路径。
- basedir = trimToEmpty(basedir);
-
- File baseFile = new File(basedir);
-
- if (baseFile.isAbsolute()) {
- path = new File(baseFile, path).getAbsolutePath();
- } else {
- throw new IllegalPathException("Basedir is not absolute path: " + basedir);
- }
- }
-
- if (endsWithSlash) {
- path = path + '/';
- }
-
- return normalizePath(path);
- }
-
- // ==========================================================================
- // 取得相对于指定basedir相对路径。
- // ==========================================================================
-
- /**
- * 取得相对于指定根目录的相对路径。
- *
- * @param basedir 根目录
- * @param path 要计算的路径
- * @return 如果path和basedir是兼容的,则返回相对于
- * basedir的相对路径,否则返回path本身。
- * @throws IllegalPathException 如果路径非法
- */
- public static String getRelativePath(String basedir, String path) throws IllegalPathException {
- // 取得规格化的basedir,确保其为绝对路径
- basedir = normalizeAbsolutePath(basedir);
-
- // 取得规格化的path
- path = getAbsolutePathBasedOn(basedir, path);
-
- // 保留path尾部的"/"
- boolean endsWithSlash = path.endsWith("/");
-
- // 按"/"分隔basedir和path
- String[] baseParts = StringUtil.split(basedir, '/');
- String[] parts = StringUtil.split(path, '/');
- StringBuilder buf = new StringBuilder();
- int i = 0;
-
- while (i < baseParts.length && i < parts.length && baseParts[i].equals(parts[i])) {
- i++;
- }
-
- if (i < baseParts.length && i < parts.length) {
- for (int j = i; j < baseParts.length; j++) {
- buf.append("..").append('/');
- }
- }
-
- for (; i < parts.length; i++) {
- buf.append(parts[i]);
-
- if (i < parts.length - 1) {
- buf.append('/');
- }
- }
-
- if (endsWithSlash && buf.length() > 0 && buf.charAt(buf.length() - 1) != '/') {
- buf.append('/');
- }
-
- return buf.toString();
- }
-
- // ==========================================================================
- // 取得文件名后缀。
- // ==========================================================================
-
- /**
- * 取得文件路径的后缀。
- *
- *
- */
- public static String getExtension(String fileName) {
- return getExtension(fileName, null, false);
- }
-
- /**
- * 取得文件路径的后缀。
- * null。null。
- *
- */
- public static String getExtension(String fileName, boolean toLowerCase) {
- return getExtension(fileName, null, toLowerCase);
- }
-
- /**
- * 取得文件路径的后缀。
- * null。null。
- *
- */
- public static String getExtension(String fileName, String nullExt) {
- return getExtension(fileName, nullExt, false);
- }
-
- /**
- * 取得文件路径的后缀。
- * null。nullExt。
- *
- */
- public static String getExtension(String fileName, String nullExt, boolean toLowerCase) {
- fileName = trimToNull(fileName);
-
- if (fileName == null) {
- return null;
- }
-
- fileName = fileName.replace('\\', '/');
- fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
-
- int index = fileName.lastIndexOf(".");
- String ext = null;
-
- if (index >= 0) {
- ext = trimToNull(fileName.substring(index + 1));
- }
-
- if (ext == null) {
- return nullExt;
- } else {
- return toLowerCase ? ext.toLowerCase() : ext;
- }
- }
-
- /**
- * 取得指定路径的名称和后缀。
- *
- * @param path 路径
- * @return 路径和后缀
- */
- public static FileNameAndExtension getFileNameAndExtension(String path) {
- return getFileNameAndExtension(path, false);
- }
-
- /**
- * 取得指定路径的名称和后缀。
- *
- * @param path 路径
- * @return 路径和后缀
- */
- public static FileNameAndExtension getFileNameAndExtension(String path, boolean extensionToLowerCase) {
- path = StringUtil.trimToEmpty(path);
-
- String fileName = path;
- String extension = null;
-
- if (!StringUtil.isEmpty(path)) {
- // 如果找到后缀,则index >= 0,且extension != null(除非name以.结尾)
- int index = path.lastIndexOf('.');
-
- if (index >= 0) {
- extension = StringUtil.trimToNull(StringUtil.substring(path, index + 1));
-
- if (!StringUtil.containsNone(extension, "/\\")) {
- extension = null;
- index = -1;
- }
- }
-
- if (index >= 0) {
- fileName = StringUtil.substring(path, 0, index);
- }
- }
-
- return new FileNameAndExtension(fileName, extension, extensionToLowerCase);
- }
-
- /**
- * 规格化文件名后缀。
- * null。nullExt。
- *
- */
- public static String normalizeExtension(String ext) {
- ext = trimToNull(ext);
-
- if (ext != null) {
- ext = ext.toLowerCase();
-
- if (ext.startsWith(".")) {
- ext = trimToNull(ext.substring(1));
- }
- }
-
- return ext;
- }
-
- private static final Pattern schemePrefixPattern = Pattern.compile(
- "(file:*[a-z]:)|(\\w+://.+?/)|((jar|zip):.+!/)|(\\w+:)", Pattern.CASE_INSENSITIVE);
-
- /**
- * 根据指定url和相对路径,计算出相对路径所对应的完整url。类似于.”。null。URI.resolve()
- * 方法,然后后者不能正确处理jar类型的URL。
- */
- public static String resolve(String url, String relativePath) {
- url = trimToEmpty(url);
-
- Matcher m = schemePrefixPattern.matcher(url);
- int index = 0;
-
- if (m.find()) {
- index = m.end();
-
- if (url.charAt(index - 1) == '/') {
- index--;
- }
- }
-
- return url.substring(0, index) + normalizeAbsolutePath(url.substring(index) + "/../" + relativePath);
- }
-
- public static void delete(File file) {
- if (file != null && file.exists()) {
- if (file.isFile()) {
- file.delete();
- }
- else if (file.isDirectory()) {
- File files[] = file.listFiles();
- for (int i=0; inull
- * if not resolvable or if the single argument is of type {@link WildcardType}.
- */
- public static Class> resolveReturnTypeArgument(Method method, Class> genericIfc) {
- Assert.assertNotNull(method, "method must not be null");
- Type returnType = method.getReturnType();
- Type genericReturnType = method.getGenericReturnType();
- if (returnType.equals(genericIfc)) {
- if (genericReturnType instanceof ParameterizedType) {
- ParameterizedType targetType = (ParameterizedType) genericReturnType;
- Type[] actualTypeArguments = targetType.getActualTypeArguments();
- Type typeArg = actualTypeArguments[0];
- if (!(typeArg instanceof WildcardType)) {
- return (Class>) typeArg;
- }
- }
- else {
- return null;
- }
- }
- return GenericTypeResolver.resolveTypeArgument((Class>) returnType, genericIfc);
- }
-
- /**
- * Resolve the single type argument of the given generic interface against
- * the given target class which is assumed to implement the generic interface
- * and possibly declare a concrete type for its type variable.
- * @param clazz the target class to check against
- * @param genericIfc the generic interface or superclass to resolve the type argument from
- * @return the resolved type of the argument, or null if not resolvable
- */
- public static Class> resolveTypeArgument(Class clazz, Class genericIfc) {
- Class[] typeArgs = resolveTypeArguments(clazz, genericIfc);
- if (typeArgs == null) {
- return null;
- }
- if (typeArgs.length != 1) {
- throw new IllegalArgumentException("Expected 1 type argument on generic interface [" +
- genericIfc.getName() + "] but found " + typeArgs.length);
- }
- return typeArgs[0];
- }
-
- /**
- * Resolve the type arguments of the given generic interface against the given
- * target class which is assumed to implement the generic interface and possibly
- * declare concrete types for its type variables.
- * @param clazz the target class to check against
- * @param genericIfc the generic interface or superclass to resolve the type argument from
- * @return the resolved type of each argument, with the array size matching the
- * number of actual type arguments, or null if not resolvable
- */
- public static Class[] resolveTypeArguments(Class clazz, Class genericIfc) {
- return doResolveTypeArguments(clazz, clazz, genericIfc);
- }
-
- private static Class[] doResolveTypeArguments(Class ownerClass, Class classToIntrospect, Class genericIfc) {
- while (classToIntrospect != null) {
- if (genericIfc.isInterface()) {
- Type[] ifcs = classToIntrospect.getGenericInterfaces();
- for (Type ifc : ifcs) {
- Class[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc);
- if (result != null) {
- return result;
- }
- }
- }
- else {
- Class[] result = doResolveTypeArguments(
- ownerClass, classToIntrospect.getGenericSuperclass(), genericIfc);
- if (result != null) {
- return result;
- }
- }
- classToIntrospect = classToIntrospect.getSuperclass();
- }
- return null;
- }
-
- private static Class[] doResolveTypeArguments(Class ownerClass, Type ifc, Class genericIfc) {
- if (ifc instanceof ParameterizedType) {
- ParameterizedType paramIfc = (ParameterizedType) ifc;
- Type rawType = paramIfc.getRawType();
- if (genericIfc.equals(rawType)) {
- Type[] typeArgs = paramIfc.getActualTypeArguments();
- Class[] result = new Class[typeArgs.length];
- for (int i = 0; i < typeArgs.length; i++) {
- Type arg = typeArgs[i];
- result[i] = extractClass(ownerClass, arg);
- }
- return result;
- }
- else if (genericIfc.isAssignableFrom((Class) rawType)) {
- return doResolveTypeArguments(ownerClass, (Class) rawType, genericIfc);
- }
- }
- else if (ifc != null && genericIfc.isAssignableFrom((Class) ifc)) {
- return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc);
- }
- return null;
- }
-
- /**
- * Extract a class instance from given Type.
- */
- private static Class extractClass(Class ownerClass, Type arg) {
- if (arg instanceof ParameterizedType) {
- return extractClass(ownerClass, ((ParameterizedType) arg).getRawType());
- }
- else if (arg instanceof GenericArrayType) {
- GenericArrayType gat = (GenericArrayType) arg;
- Type gt = gat.getGenericComponentType();
- Class> componentClass = extractClass(ownerClass, gt);
- return Array.newInstance(componentClass, 0).getClass();
- }
- else if (arg instanceof TypeVariable) {
- TypeVariable tv = (TypeVariable) arg;
- arg = getTypeVariableMap(ownerClass).get(tv);
- if (arg == null) {
- arg = extractBoundForTypeVariable(tv);
- }
- else {
- arg = extractClass(ownerClass, arg);
- }
- }
- return (arg instanceof Class ? (Class) arg : Object.class);
- }
-
-
- /**
- * Resolve the specified generic type against the given TypeVariable map.
- * @param genericType the generic type to resolve
- * @param typeVariableMap the TypeVariable Map to resolved against
- * @return the type if it resolves to a Class, or Object.class otherwise
- */
- public static Class> resolveType(Type genericType, Map