📄 listtools.java
字号:
*** Returns true if the specified list contains the specified element *** @param list The array instance to search *** @param item The element which is tested for inclusion in the specified array *** @return True if the array contains the specified element **/ public static <T> boolean contains(T list[], T item) { return (ListTools.indexOf(list, 0, -1, item) >= 0); } /** *** Returns true if the specified list contains the specified element *** @param list The array instance to search *** @param ofs The offset within the array to begin searching *** @param len The number of elements to search *** @param item The element which is tested for inclusion in the specified array *** @return True if the array contains the specified element **/ public static <T> boolean contains(T list[], int ofs, int len, T item) { return (ListTools.indexOf(list, ofs, len, item) >= 0); } /** *** Returns true if the specified array contains the specified case-insensitive String value *** @param list The array instance to search *** @param item The element which is tested for inclusion in the specified array *** @return True if the array contains the specified element **/ public static boolean containsIgnoreCase(String list[], String item) { return (ListTools.indexOfIgnoreCase(list, item) >= 0); } // ------------------------------------------------------------------------ /** *** Returns an Iterator for the specified Iterable *** @param i The Iterable *** @return An iterator **/ public static <T> Iterator<T> toIterator(Iterable<T> i) { if (i != null) { return i.iterator(); } else { return new Iterator<T>() { public boolean hasNext() { return false; } public T next() { throw new NoSuchElementException("Null Iterable"); } public void remove() { throw new UnsupportedOperationException(); } }; } } /** *** Converts the specified Enumeration into an Iterator *** @param e The Enumeration *** @return An iterator **/ public static <T> Iterator<T> toIterator(final Enumeration<T> e) { return new Iterator<T>() { public boolean hasNext() { return (e != null)? e.hasMoreElements() : false; } public T next() { if (e != null) { return e.nextElement(); } else { throw new NoSuchElementException("Null Enumeration"); } } public void remove() { throw new UnsupportedOperationException(); } }; } /** *** Returns an iterator over the elements in the specified array *** @param list The array *** @return An iterator **/ public static <T> Iterator<T> toIterator(final T list[]) { return new Iterator<T>() { private int ndx = 0; public boolean hasNext() { return ((list != null) && (this.ndx < list.length)); } public T next() { if ((list != null) && (this.ndx < list.length)) { return list[this.ndx++]; } else { throw new NoSuchElementException("end of array"); } } public void remove() { throw new UnsupportedOperationException(); } }; } // ------------------------------------------------------------------------ /** *** Sorts the specified List based on the specified Comparator *** @param list The list to sort (in-place) *** @param comp The Comparator used to sort the specified List *** @return The sorted List (in-place) **/ public static <T> java.util.List<T> sort(java.util.List<T> list, Comparator<? super T> comp) { return ListTools.sort(list, comp, true); } /** *** Sorts the specified List based on the specified Comparator and sort order *** @param list The list to sort (in-place) *** @param comp The Comparator used to sort the specified List *** @param forwardOrder True to sort based on the Comparator, false to sort based on the *** reverse order of the Comparator *** @return The sorted List (in-place) **/ public static <T> java.util.List<T> sort(java.util.List<T> list, Comparator<? super T> comp, boolean forwardOrder) { if (list != null) { Comparator<? super T> c = comp; if (c == null) { c = new StringComparator<T>(forwardOrder); } else if (forwardOrder) { c = comp; } else { c = new ReverseOrderComparator<T>(comp); } Collections.sort(list, c); } return list; } /** *** Sorts the specified array based on the specified Comparator *** @param list The array to sort (in-place) *** @param comp The Comparator used to sort the specified array *** @return The sorted array (in-place) **/ public static <T> T[] sort(T list[], Comparator<? super T> comp) { return ListTools.sort(list, comp, true); } /** *** Sorts the specified array based on the specified Comparator *** @param list The array to sort (in-place) *** @param comp The Comparator used to sort the specified array *** @param forwardOrder True to sort based on the Comparator, false to sort based on the *** reverse order of the Comparator *** @return The sorted array (in-place) **/ public static <T> T[] sort(T list[], Comparator<? super T> comp, boolean forwardOrder) { if (list != null) { Comparator<? super T> c = comp; if (c == null) { c = new StringComparator<T>(forwardOrder); } else if (forwardOrder) { c = comp; } else { c = new ReverseOrderComparator<T>(comp); } Arrays.sort(list, c); } return list; } /** *** Sorts the specified String array in ascending order *** @param list The array to sort (in-place) *** @return The sorted array (in-place) **/ public static String[] sort(String list[]) { return (String[])ListTools.sort(list, new StringComparator<String>(), true); } /** *** Sorts the specified String array *** @param list The array to sort (in-place) *** @param forwardOrder True to sort ascending, false descending *** @return The sorted array (in-place) **/ public static String[] sort(String list[], boolean forwardOrder) { return (String[])ListTools.sort(list, new StringComparator<String>(), forwardOrder); } /** *** StringComparator class for sorting objects based on their 'toString()' value **/ public static class StringComparator<T> implements Comparator<T> { private boolean ascending = true; private boolean ignoreCase = false; public StringComparator() { this(true, false); } public StringComparator(boolean ascending) { this(ascending, false); } public StringComparator(boolean ascending, boolean ignroeCase) { this.ascending = ascending; this.ignoreCase = ignoreCase; } public int compare(T o1, T o2) { String s1 = (o1 != null)? o1.toString() : ""; String s2 = (o2 != null)? o2.toString() : ""; if (this.ignoreCase) { s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); } return this.ascending? s1.compareTo(s2) : s2.compareTo(s1); } public boolean equals(Object other) { if (other instanceof StringComparator) { StringComparator sc = (StringComparator)other; return (this.ascending == sc.ascending) && (this.ignoreCase == sc.ignoreCase); } return false; } } /** *** ReverseOrderComparator class which reserses the sort order of other Comparators **/ public static class ReverseOrderComparator<T> implements Comparator<T> { private Comparator<? super T> otherComp = null; public ReverseOrderComparator(Comparator<? super T> comp) { if (comp != null) { this.otherComp = comp; } else { this.otherComp = new StringComparator<T>(); } } public int compare(T o1, T o2) { int compVal = this.otherComp.compare(o1, o2); if (compVal > 0) { return -1; } if (compVal < 0) { return 1; } return 0; } public boolean equals(Object obj) { if (obj instanceof ReverseOrderComparator) { ReverseOrderComparator descComp = (ReverseOrderComparator)obj; return this.otherComp.equals(descComp.otherComp); } return false; } } // ------------------------------------------------------------------------ /** *** Reverses the order of the elements in the specified array (in-place) *** @param list The array in which to reverse the order of elements (in-place) *** @return The array with the element order reversed (in-place) **/ public static <T> T[] reverseOrder(T list[]) { if ((list != null) && (list.length > 1)) { int len = list.length / 2; for (int i = 0; i < len; i++) { int i2 = (list.length - 1) - i; T obj = list[i]; list[i] = list[i2]; list[i2] = obj; } } return list; } /** *** Reverses the order of the elements in the specified List (in-place) *** @param list The List in which to reverse the order of elements (in-place) *** @return The List with the element order reversed (in-place) **/ public static <T> java.util.List<T> reverseOrder(java.util.List<T> list) { Collections.reverse(list); return list; } // ------------------------------------------------------------------------ /** *** CollectionProxy class **/ public static class CollectionProxy<E> implements Collection<E> { private Collection<E> delegate = null; public CollectionProxy(Collection<E> c) { this.delegate = c; } public boolean add(E o) { return this.delegate.add(o); } public boolean addAll(Collection<? extends E> c) { return this.delegate.addAll(c); } public void clear() { this.delegate.clear(); } public boolean contains(Object o) { return this.delegate.contains(o); } public boolean containsAll(Collection<?> c) { return this.delegate.containsAll(c); } public boolean equals(Object o) { if (o instanceof CollectionProxy) { return this.delegate.equals(((CollectionProxy)o).delegate); } else { return false; } } public int hashCode() { return this.delegate.hashCode(); } public boolean isEmpty() { return this.delegate.isEmpty(); } public Iterator<E> iterator() { return this.delegate.iterator(); } public boolean remove(Object o) { return this.delegate.remove(o); } public boolean removeAll(Collection<?> c) { return this.delegate.removeAll(c); } public boolean retainAll(Collection<?> c) { return this.delegate.retainAll(c); } public int size() { return this.delegate.size(); } public Object[] toArray() { return this.delegate.toArray(); } public <T> T[] toArray(T[] a) { return this.delegate.toArray(a); } } // ------------------------------------------------------------------------ // ------------------------------------------------------------------------}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -