⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listtools.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description://  This class provides many List/Collection/Array based utilities.// ----------------------------------------------------------------------------// Change History://  2006/03/26  Martin D. Flynn//     -Initial release//  2006/06/30  Martin D. Flynn//     -Repackaged//  2008/03/28  Martin D. Flynn//     -Added additional 'toMap' methods//  2008/05/14  Martin D. Flynn//     -Added initial Java 5 'generics'// ----------------------------------------------------------------------------package org.opengts.util;import java.lang.reflect.*;import java.util.*;public class ListTools{    // ------------------------------------------------------------------------    /**    *** Validates and returns an offset that fits within the specified length    *** @param ofs     The offset to validate.    *** @param length  The length used to constrain the offset (assumed to be valid)    *** @return The constrained offset value     **/    private static int _constrainOffset(int ofs, int length)    {        if ((ofs < 0) || (length <= 0)) {            return 0;        } else        if (ofs >= length) {            return length - 1;        } else {            return ofs;        }    }    /**    *** Validates and returns an offset that fits within the specified length    *** @param ofs     The offset within 'length' (assumed to be valid)    *** @param len     The length to validate/constrain    *** @param length  The length used to constrain the specified 'len' (assumed to be valid)    *** @return The constrained length value     **/    private static int _constrainLength(int ofs, int len, int length)    {        if (len < 0) {             return length;         } else        if (len > (length - ofs)) {             return length - ofs;         } else {            return len;        }    }        // ------------------------------------------------------------------------    /**    *** Copies the specified array elements to a List    *** @param a  The object array to copy to a List instance    *** @return The List instance containing the elements of the specified array    **/    public static <T> java.util.List<T> toList(T a[])    {        return ListTools.toList(a, null);    }    /**    *** Copies the specified array elements to a List    *** @param a    The object array to copy to a List instance    *** @param list The List instance to which the elements of the Object array will be copied    *** @return The List instance containing the elements of the specified array    **/    public static <T> java.util.List<T> toList(T a[], java.util.List<T> list)    {        return ListTools.toList(a, 0, -1, list);    }        /**    *** Copies the specified array elements to a List    *** @param a    The object array to copy to a List instance    *** @param ofs  The offset within the specified Object array to begin copying to List    *** @param len  The number of elements from the specified Object array to copy to List    *** @param list The List instance to which the elements of the object array will be copied    *** @return The List instance containing the elements of the specified array    **/    public static <T> java.util.List<T> toList(T a[], int ofs, int len, java.util.List<T> list)    {        java.util.List<T> v = (list != null)? list : new Vector<T>();        int alen = (a != null)? a.length : 0;        ofs = _constrainOffset(ofs, alen);        len = _constrainLength(ofs, len, alen);        for (int i = ofs; i < len; i++) { v.add(a[i]); }        return v;    }    // ------------------------------------------------------------------------    /**    *** Copies the Enumeration to a List    *** @param e  The Enumeration to copy to a List instance    *** @return The List instance containing the elements of the Enumeration    **/    public static <T> java.util.List<T> toList(Enumeration<T> e)    {        return ListTools.toList(e, null);    }    /**    *** Copies the Enumeration to a List    *** @param e  The Enumeration to copy to a List instance    *** @param list The List instance to which the elements from the Enumeration will be copied    *** @return The List instance containing the elements of the Enumeration    **/    public static <T> java.util.List<T> toList(Enumeration<T> e, java.util.List<T> list)    {        java.util.List<T> v = (list != null)? list : new Vector<T>();        if (e != null) { for (;e.hasMoreElements();) { v.add(e.nextElement()); } }        return v;    }    // ------------------------------------------------------------------------    /**    *** Copies the Iterator to a List    *** @param i  The Iterator to copy to a List instance    *** @return The List instance containing the elements of the Iterator    **/    public static <T> java.util.List<T> toList(Iterator<T> i)    {        return ListTools.toList(i, null);    }    /**    *** Copies the Iterator to a List    *** @param i  The Iterator to copy to a List instance    *** @param list The List instance to which the elements from the Iterator will be copied    *** @return The List instance containing the elements of the Iterator    **/    public static <T> java.util.List<T> toList(Iterator<T> i, java.util.List<T> list)    {        java.util.List<T> v = (list != null)? list : new Vector<T>();        if (i != null) { for (;i.hasNext();) { v.add(i.next()); } }        return v;    }    // ------------------------------------------------------------------------    /**    *** Copies the Set to a List    *** @param s  The Set to copy to a List instance    *** @return The List instance containing the elements of the Set    **/    public static <T> java.util.List<T> toList(Set<T> s)    {        return ListTools.toList(s, null);    }    /**    *** Copies the Set to a List    *** @param s  The Set to copy to a List instance    *** @param list The List instance to which the elements from the Set will be copied    *** @return The List instance containing the elements of the Set    **/    public static <T> java.util.List<T> toList(Set<T> s, java.util.List<T> list)    {        return ListTools.toList(((s != null)? s.iterator() : null), list);    }    // ------------------------------------------------------------------------    /**    *** Copies the StringTokenizer to a List    *** @param st  The StringTokenizer to copy to a List instance    *** @return The List instance containing the elements of the StringTokenizer    **/    public static java.util.List<String> toList(StringTokenizer st)    {        return ListTools.toList(st, (java.util.List<String>)null);    }    /**    *** Copies the StringTokenizer to a List    *** @param st   The StringTokenizer to copy to a List instance    *** @param list The List instance to which the elements from the StringTokenizer will be copied    *** @return The List instance containing the elements of the StringTokenizer    **/    public static java.util.List<String> toList(StringTokenizer st, java.util.List<String> list)    {        java.util.List<String> v = (list != null)? list : new Vector<String>();        if (st != null) { for (;st.hasMoreTokens();) { v.add(st.nextToken()); } }        return v;    }    // ------------------------------------------------------------------------    /**    *** Copies the contents of the specified List to a new List    *** @param ls  The List to copy to a new List instance    *** @return The List instance containing the elements of the specified List    **/    public static <T> java.util.List<T> toList(java.util.List<T> ls)    {        return ListTools.toList(ls, null);    }    /**    *** Copies the specified list to a new List    *** @param ls   The List to copy to a new List instance    *** @param list The List instance to which the elements from the specified List will be copied    *** @return The List instance containing the elements of the specified List    **/    public static <T> java.util.List<T> toList(java.util.List<T> ls, java.util.List<T> list)    {        java.util.List<T> v = (list != null)? list : new Vector<T>();        if (ls != null) { v.addAll(ls); }        return v;    }    // ------------------------------------------------------------------------    /**    *** Returns true if all elements of the specified list are subclasses of the specified Class    *** @param list  The List to check    *** @param type  The Class object used to check against all elements in the List    *** @return True if all elements in the List are subclasses of the specified Class    **/    public static <T> boolean isClassType(java.util.List<T> list, Class<T> type)    {        if ((type == null) || (type == Object.class)) {            return true;        } else        if (list != null) {            for (Iterator<T> i = list.iterator(); i.hasNext();) {                T obj = i.next();                if ((obj != null) && !type.isAssignableFrom(obj.getClass())) {                    return false;                }            }            return true;        } else {            return false;        }    }        // ------------------------------------------------------------------------    /**    *** Copies the specified Enumeration to a new array of the specified Class type    *** @param e    The Enumeration to copy to the new array    *** @param type The Class type of the return array    *** @return An array containing the elements from the Enumeration    **/    public static <T> T[] toArray(Enumeration<T> e, Class type)    {        return ListTools.toArray(ListTools.toList(e), type);    }    /**    *** Copies the specified Collection to a new array    *** @param list The Collection to copy to the new array    *** @return An array containing the elements from the Collection    **/    public static <T> T[] toArray(Collection<T> list)    {        return ListTools.toArray(list, null);    }    /**    *** Copies the specified Collection to a new array of the specified Class type    *** @param list The Collection to copy to the new array    *** @param type The Class type of the return array    *** @return An array containing the elements from the Collection    **/    public static <T> T[] toArray(Collection<T> list, Class type)    {        if (type == null) { type = Object.class; }        if (list != null) {            T array[] = (T[])Array.newInstance(type, list.size());  // "unchecked cast"            return list.toArray(array);        } else {            return (T[])Array.newInstance(type, 0);  // "unchecked cast"        }    }        /**    *** Creates a new array containing the elements in the specified array    *** @param arry  The array containing elements to be copied to a new array    *** @param ofs   The offset within 'arry' to begin copying    *** @param len   The number of elements to copy to the new array    *** @return  The new array    **/    public static <T> T[] toArray(T arry[], int ofs, int len)    {        if (arry != null) {            int alen = arry.length;            ofs = _constrainOffset(ofs, alen);            len = _constrainLength(ofs, len, alen);            Class type = arry.getClass().getComponentType();            T newArry[] = (T[])Array.newInstance(type, len);  // "unchecked cast"            System.arraycopy(arry, ofs, newArry, 0, len);            return newArry;        } else {            return arry;        }    }    // ------------------------------------------------------------------------    /**    *** Returns a String array comprised of 'toString()' calls to the specified Object array    *** @param a  The Object array    *** @return The String array    **/    public static String[] toStringArray(Object a[])    {        if (a != null) {            String v[] = new String[a.length];            for (int i = 0; i < a.length; i++) { v[i] = a[i].toString(); }            return v;        } else {            return new String[0];        }    }        // ------------------------------------------------------------------------    /**    *** Copies the specified Object array to a new Set    *** @param a  The array to copy to a new Set    *** @return The Set (HashSet) containing the elements from specified array    **/    public static <T> Set<T> toSet(T a[])    {        return ListTools.toSet(a, null);    }    /**    *** Copies the specified Object array to a new Set    *** @param a    The array to copy to the Set    *** @param set  The Set to which the array elements will be copied

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -