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

📄 arrayutils.java

📁 Bluetooth chat Server and Client in j2me
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* The Bluetooth Library for client-server communication Copyright (C) 2006 Martin Vysny This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. */package net.sf.btw.tools;import java.util.Enumeration;import java.util.NoSuchElementException;import java.util.Vector;/** * Array utilities. *  * @author Martin Vysny */public final class ArrayUtils {	private ArrayUtils() {		// prevent instantiation	}	/**	 * Finds first occurence of given object in given vector. To compare objects	 * {@link Object#equals(Object)} is used.	 * 	 * @param vector	 *            vector	 * @param obj	 *            object to find, may be <code>null</code>.	 * @return index of given object or -1 if the enumeration does not contain	 *         the object.	 */	public static int indexOf(final Vector vector, final Object obj) {		for (int i = 0; i < vector.size(); i++) {			final Object other = vector.elementAt(i);			if (obj == null) {				if (other == null)					return i;			} else if (obj.equals(other)) {				return i;			}		}		return -1;	}	/**	 * Appends all items from given array after last item of the vector.	 * 	 * @param vector	 *            here all items will be appended.	 * @param copyFrom	 *            array to append.	 */	public static void addAll(Vector vector, Object[] copyFrom) {		if (copyFrom == null)			return;		vector.ensureCapacity(vector.size() + copyFrom.length);		for (int i = 0; i < copyFrom.length; i++) {			vector.addElement(copyFrom[i]);		}	}	/**	 * Appends all items from given array after last item of the vector.	 * 	 * @param vector	 *            here all items will be appended.	 * @param copyFrom	 *            array to append. See {@link #arrayToEnumeration(Object)} for	 *            details. Does not support {@link Enumeration}.	 */	public static void addAll(Vector vector, Object copyFrom) {		if (copyFrom == null)			return;		if (copyFrom instanceof Object[]) {			addAll(vector, (Object[]) copyFrom);			return;		}		if (copyFrom instanceof Enumeration) {			final Enumeration e = (Enumeration) copyFrom;			for (; e.hasMoreElements();) {				vector.addElement(e.nextElement());			}			return;		}		final int copyFromLength = getLength(copyFrom);		vector.ensureCapacity(vector.size() + copyFromLength);		for (int i = 0; i < copyFromLength; i++) {			vector.addElement(getElementAt(copyFrom, i));		}	}	/**	 * Appends all items from given array after last item of the vector.	 * 	 * @param vector	 *            here all items will be appended.	 * @param copyFrom	 *            vector to append.	 */	public static void addAll(Vector vector, Vector copyFrom) {		if (copyFrom == null)			return;		vector.ensureCapacity(vector.size() + copyFrom.size());		addAll(vector, copyFrom.elements());	}	/**	 * Compares two enumerations if they contain equal objects in correct order.	 * 	 * @param enum1	 *            first enumeration	 * @param enum2	 *            second enumeration	 * @return <code>true</code> if two enumerations match, <code>false</code>	 *         otherwise.	 */	public static boolean equals(final Enumeration enum1,			final Enumeration enum2) {		while (true) {			if (!enum1.hasMoreElements()) {				return !enum2.hasMoreElements();			}			if (!enum2.hasMoreElements())				return false;			final Object obj1 = enum1.nextElement();			final Object obj2 = enum2.nextElement();			if (!equalsObjects(obj1, obj2))				return false;		}	}	/**	 * The ultimate comparator. Handles <code>null</code>s and arrays	 * correctly.	 * 	 * @param o1	 *            first object.	 * @param o2	 *            second object.	 * @return <code>true</code> if objects equals, <code>false</code>	 *         otherwise.	 */	public static boolean equalsObjects(final Object o1, final Object o2) {		if (o1 == null)			return o2 == null;		if (o2 == null)			return false;		if (isArray(o1)) {			if (!isArray(o2))				return false;			return equals(o1, o2);		}		if (isArray(o2))			return false;		return o1.equals(o2);	}	/**	 * Compares two enumerations if they contain equal objects in correct order.	 * 	 * @param enum1	 *            first enumeration	 * @param enum2	 *            second enumeration	 * @return <code>true</code> if two enumerations match, <code>false</code>	 *         otherwise.	 */	public static boolean equals(final Enumeration enum1, final Object[] enum2) {		int i = 0;		while (true) {			if (!enum1.hasMoreElements()) {				return i >= enum2.length;			}			if (i >= enum2.length)				return false;			final Object obj1 = enum1.nextElement();			final Object obj2 = enum2[i++];			if (!((obj1 == null) ? obj2 == null : obj1.equals(obj2)))				return false;		}	}	/**	 * Compares two arrays if they contain equal objects in correct order.	 * 	 * @param enum1	 *            first array	 * @param enum2	 *            second array	 * @return <code>true</code> if two enumerations match, <code>false</code>	 *         otherwise.	 */	public static boolean equals(final Object[] enum1, final Object[] enum2) {		if (enum1.length != enum2.length)			return false;		for (int i = 0; i < enum1.length; i++) {			if (!enum1[i].equals(enum2[i]))				return false;		}		return true;	}	/**	 * Compares two arrays if they contain equal objects in correct order. For	 * details please see {@link #arrayToEnumeration(Object)}.	 * 	 * @param enum1	 *            first array	 * @param enum2	 *            second array	 * @return <code>true</code> if two enumerations match, <code>false</code>	 *         otherwise.	 * @throws IllegalArgumentException	 *             if the object is not {@link #isArray(Object) an array}.	 */	public static boolean equals(final Object enum1, final Object enum2) {		return equals(arrayToEnumeration(enum1), arrayToEnumeration(enum2));	}	/**	 * Computes correct hashcode for given object.	 * 	 * @param object	 *            the object to compute hash from. It will compute correct	 *            hashcode from arrays and enumerations aswell.	 * @return the hashcode, <code>0</code> if <code>null</code> was given.	 */	public static int hashCode(final Object object) {		if (object == null)			return 0;		if (!object.getClass().isArray())			return object.hashCode();		int result = 1;		for (final Enumeration e = arrayToEnumeration(object); e				.hasMoreElements();) {			final Object o = e.nextElement();			result = result * 1001 + hashCode(o);		}		return result;	}	/**	 * Creates an enumeration from given object. If the object is an array of	 * primitive types then the enumeration will enumerate appropriate object	 * equivalents. If the object is a {@link Queue} or a {@link Vector} then	 * their enumerations are returned instead.	 * 	 * @param array	 *            the object to convert to an enumeration.	 * @return enumeration instance.	 * @throws IllegalArgumentException	 *             if the object is none of the above.	 */	public static Enumeration arrayToEnumeration(final Object array) {		checkIsArray(array, true);		if (array instanceof Queue)			return ((Queue) array).getEnumeration();		if (array instanceof Vector)			return ((Vector) array).elements();		if (array instanceof Enumeration)			return (Enumeration) array;		return new ArrayEnumeration(array);	}	/**	 * Returns true if given object is an array or a {@link Queue}, a	 * {@link Vector} or an {@link Enumeration}.	 * 	 * @param array	 *            the array instance.	 * @return <code>true</code> if given object is an array,	 *         <code>false</code> if it is a regular object or	 *         <code>null</code>.	 */	public static boolean isArray(final Object array) {		if (array == null)			return false;		if (array.getClass().isArray())			return true;		return (array instanceof Queue) || (array instanceof Vector)				|| (array instanceof Enumeration);	}	/**	 * Checks if given object is an array, as per {@link #isArray(Object)}.	 * 	 * @param array	 *            the array to check	 * @param allowEnumeration	 *            if <code>true</code> then enumeration is allowed as a valid	 *            array type.	 * @throws IllegalArgumentException	 *             if given object is not array.	 */

⌨️ 快捷键说明

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