01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.tests.harness.util;
11:
12: import java.util.Random;
13:
14: /**
15: * <code>ArrayUtil</code> contains methods for array
16: * examination.
17: */
18: public class ArrayUtil {
19: private static Random randomBox = new Random();
20:
21: /**
22: * Returns a random object chosen from an array.
23: *
24: * @param array the input array
25: * @return a random object in the array
26: */
27: public static Object pickRandom(Object[] array) {
28: int num = randomBox.nextInt(array.length);
29: return array[num];
30: }
31:
32: /**
33: * Returns whether an array is not null and
34: * each object in the array is not null.
35: *
36: * @param array the input array
37: * @return <code>true or false</code>
38: */
39: public static boolean checkNotNull(Object[] array) {
40: if (array == null)
41: return false;
42: else {
43: for (int i = 0; i < array.length; i++)
44: if (array[i] == null)
45: return false;
46: return true;
47: }
48: }
49:
50: /**
51: * Returns whether an array contains a given object.
52: *
53: * @param array the input array
54: * @param element the test object
55: * @return <code>true</code> if the array contains the object,
56: * <code>false</code> otherwise.
57: */
58: public static boolean contains(Object[] array, Object element) {
59: if (array == null || element == null)
60: return false;
61: else {
62: for (int i = 0; i < array.length; i++)
63: if (array[i] == element)
64: return true;
65: return false;
66: }
67: }
68:
69: /**
70: * Returns whether two arrays are equal. They must
71: * have the same size and the same contents.
72: *
73: * @param one the first array
74: * @param two the second array
75: * @return <code>true</code> if the array are equal,
76: * <code>false</code> otherwise.
77: */
78: public static boolean equals(Object[] one, Object[] two) {
79: if (one.length != two.length)
80: return false;
81: else {
82: for (int i = 0; i < one.length; i++)
83: if (one[i] != two[i])
84: return false;
85: return true;
86: }
87: }
88: }
|