Source for org.jfree.util.ArrayUtilities

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it
  10:  * under the terms of the GNU Lesser General Public License as published by
  11:  * the Free Software Foundation; either version 2.1 of the License, or
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  22:  * USA.
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------
  28:  * ArrayUtilities.java
  29:  * -------------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: ArrayUtilities.java,v 1.7 2008/09/10 09:21:30 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 21-Aug-2003 : Version 1 (DG);
  40:  * 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.util;
  45: 
  46: import java.util.Arrays;
  47: 
  48: /**
  49:  * Utility methods for working with arrays.
  50:  *
  51:  * @author David Gilbert
  52:  */
  53: public class ArrayUtilities {
  54: 
  55:     /**
  56:      * Private constructor prevents object creation.
  57:      */
  58:     private ArrayUtilities() {
  59:     }
  60: 
  61:     /**
  62:      * Clones a two dimensional array of floats.
  63:      *
  64:      * @param array  the array.
  65:      *
  66:      * @return A clone of the array.
  67:      */
  68:     public static float[][] clone(final float[][] array) {
  69: 
  70:         if (array == null) {
  71:             return null;
  72:         }
  73:         final float[][] result = new float[array.length][];
  74:         System.arraycopy(array, 0, result, 0, array.length);
  75: 
  76:         for (int i = 0; i < array.length; i++) {
  77:             final float[] child = array[i];
  78:             final float[] copychild = new float[child.length];
  79:             System.arraycopy(child, 0, copychild, 0, child.length);
  80:             result[i] = copychild;
  81:         }
  82: 
  83:         return result;
  84: 
  85:     }
  86: 
  87:     /**
  88:      * Returns <code>true</code> if all the references in <code>array1</code>
  89:      * are equal to all the references in <code>array2</code> (two
  90:      * <code>null</code> references are considered equal for this test).
  91:      *
  92:      * @param array1  the first array (<code>null</code> permitted).
  93:      * @param array2  the second array (<code>null</code> permitted).
  94:      *
  95:      * @return A boolean.
  96:      */
  97:     public static boolean equalReferencesInArrays(final Object[] array1,
  98:                                                   final Object[] array2) {
  99:         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: }