001 /* ======================================================================== 002 * JCommon : a free general purpose class library for the Java(tm) platform 003 * ======================================================================== 004 * 005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jcommon/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------- 028 * ArrayUtilities.java 029 * ------------------- 030 * (C) Copyright 2003-2005, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: ArrayUtilities.java,v 1.7 2008/09/10 09:21:30 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 21-Aug-2003 : Version 1 (DG); 040 * 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG); 041 * 042 */ 043 044 package org.jfree.util; 045 046 import java.util.Arrays; 047 048 /** 049 * Utility methods for working with arrays. 050 * 051 * @author David Gilbert 052 */ 053 public class ArrayUtilities { 054 055 /** 056 * Private constructor prevents object creation. 057 */ 058 private ArrayUtilities() { 059 } 060 061 /** 062 * Clones a two dimensional array of floats. 063 * 064 * @param array the array. 065 * 066 * @return A clone of the array. 067 */ 068 public static float[][] clone(final float[][] array) { 069 070 if (array == null) { 071 return null; 072 } 073 final float[][] result = new float[array.length][]; 074 System.arraycopy(array, 0, result, 0, array.length); 075 076 for (int i = 0; i < array.length; i++) { 077 final float[] child = array[i]; 078 final float[] copychild = new float[child.length]; 079 System.arraycopy(child, 0, copychild, 0, child.length); 080 result[i] = copychild; 081 } 082 083 return result; 084 085 } 086 087 /** 088 * Returns <code>true</code> if all the references in <code>array1</code> 089 * are equal to all the references in <code>array2</code> (two 090 * <code>null</code> references are considered equal for this test). 091 * 092 * @param array1 the first array (<code>null</code> permitted). 093 * @param array2 the second array (<code>null</code> permitted). 094 * 095 * @return A boolean. 096 */ 097 public static boolean equalReferencesInArrays(final Object[] array1, 098 final Object[] array2) { 099 if (array1 == null) { 100 return (array2 == null); 101 } 102 if (array2 == null) { 103 return false; 104 } 105 if (array1.length != array2.length) { 106 return false; 107 } 108 for (int i = 0; i < array1.length; i++) { 109 if (array1[i] == null) { 110 if (array2[i] != null) { 111 return false; 112 } 113 } 114 if (array2[i] == null) { 115 if (array1[i] != null) { 116 return false; 117 } 118 } 119 if (array1[i] != array2[i]) { 120 return false; 121 } 122 } 123 return true; 124 } 125 126 /** 127 * Tests two float arrays for equality. 128 * 129 * @param array1 the first array (<code>null</code> permitted). 130 * @param array2 the second arrray (<code>null</code> permitted). 131 * 132 * @return A boolean. 133 */ 134 public static boolean equal(final float[][] array1, 135 final float[][] array2) { 136 if (array1 == null) { 137 return (array2 == null); 138 } 139 140 if (array2 == null) { 141 return false; 142 } 143 144 if (array1.length != array2.length) { 145 return false; 146 } 147 148 for (int i = 0; i < array1.length; i++) { 149 if (!Arrays.equals(array1[i], array2[i])) { 150 return false; 151 } 152 } 153 return true; 154 } 155 156 /** 157 * Returns <code>true</code> if any two items in the array are equal to 158 * one another. Any <code>null</code> values in the array are ignored. 159 * 160 * @param array the array to check. 161 * 162 * @return A boolean. 163 */ 164 public static boolean hasDuplicateItems(final Object[] array) { 165 for (int i = 0; i < array.length; i++) { 166 for (int j = 0; j < i; j++) { 167 final Object o1 = array[i]; 168 final Object o2 = array[j]; 169 if (o1 != null && o2 != null) { 170 if (o1.equals(o2)) { 171 return true; 172 } 173 } 174 } 175 } 176 return false; 177 } 178 179 /** 180 * Compares the initial elements of two arrays. 181 * 182 * @param a1 array 1. 183 * @param a2 array 2. 184 * 185 * @return An integer showing the relative ordering. 186 */ 187 public static int compareVersionArrays (Comparable[] a1, Comparable[] a2) 188 { 189 int length = Math.min (a1.length, a2.length); 190 for (int i = 0; i < length; i++) 191 { 192 Comparable o1 = a1[i]; 193 Comparable o2 = a2[i]; 194 if (o1 == null && o2 == null) 195 { 196 // cannot decide .. 197 continue; 198 } 199 if (o1 == null) 200 { 201 return 1; 202 } 203 if (o2 == null) 204 { 205 return -1; 206 } 207 int retval = o1.compareTo(o2); 208 if (retval != 0) 209 { 210 return retval; 211 } 212 } 213 return 0; 214 } 215 216 }