001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------
028 * ArrayUtils.java
029 * ---------------
030 * (C) Copyright 2000-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributors:     -;
034 */
035
036package org.jfree.chart.util;
037
038import java.util.Arrays;
039
040/**
041 * Utility methods for working with arrays.
042 */
043public class ArrayUtils {
044
045    /**
046     * Private constructor prevents object creation.
047     */
048    private ArrayUtils() {
049    }
050
051    /**
052     * Clones a two dimensional array of floats.
053     *
054     * @param array  the array.
055     *
056     * @return A clone of the array.
057     */
058    public static float[][] clone(float[][] array) {
059
060        if (array == null) {
061            return null;
062        }
063        float[][] result = new float[array.length][];
064        System.arraycopy(array, 0, result, 0, array.length);
065
066        for (int i = 0; i < array.length; i++) {
067            float[] child = array[i];
068            float[] copychild = new float[child.length];
069            System.arraycopy(child, 0, copychild, 0, child.length);
070            result[i] = copychild;
071        }
072
073        return result;
074
075    }
076
077    /**
078     * Returns {@code true} if all the references in {@code array1}
079     * are equal to all the references in {@code array2} (two
080     * {@code null} references are considered equal for this test).
081     *
082     * @param array1  the first array ({@code null} permitted).
083     * @param array2  the second array ({@code null} permitted).
084     *
085     * @return A boolean.
086     */
087    public static boolean equalReferencesInArrays(Object[] array1, 
088            Object[] array2) {
089        if (array1 == null) {
090            return (array2 == null);
091        }
092        if (array2 == null) {
093            return false;
094        }
095        if (array1.length != array2.length) {
096            return false;
097        }
098        for (int i = 0; i < array1.length; i++) {
099            if (array1[i] == null) {
100                if (array2[i] != null) {
101                    return false;
102                }
103            }
104            if (array2[i] == null) {
105                if (array1[i] != null) {
106                    return false;
107                }
108            }
109            if (array1[i] != array2[i]) {
110                return false;
111            }
112        }
113        return true;
114    }
115
116    /**
117     * Tests two float arrays for equality.
118     *
119     * @param array1  the first array ({@code null} permitted).
120     * @param array2  the second arrray ({@code null} permitted).
121     *
122     * @return A boolean.
123     */
124    public static boolean equal(float[][] array1, float[][] array2) {
125        if (array1 == null) {
126            return (array2 == null);
127        }
128
129        if (array2 == null) {
130            return false;
131        }
132
133        if (array1.length != array2.length) {
134            return false;
135        }
136
137        for (int i = 0; i < array1.length; i++) {
138            if (!Arrays.equals(array1[i], array2[i])) {
139                return false;
140            }
141        }
142        return true;
143    }
144
145    /**
146     * Returns {@code true} if any two items in the array are equal to
147     * one another.  Any {@code null} values in the array are ignored.
148     *
149     * @param array  the array to check.
150     *
151     * @return A boolean.
152     */
153    public static boolean hasDuplicateItems(Object[] array) {
154        for (int i = 0; i < array.length; i++) {
155            for (int j = 0; j < i; j++) {
156                Object o1 = array[i];
157                Object o2 = array[j];
158                if (o1 != null && o2 != null) {
159                    if (o1.equals(o2)) {
160                        return true;
161                    }
162                }
163            }
164        }
165        return false;
166    }
167
168    /**
169     * Compares the initial elements of two arrays.
170     *
171     * @param a1  array 1.
172     * @param a2  array 2.
173     *
174     * @return An integer showing the relative ordering.
175     */
176    public static int compareVersionArrays (Comparable[] a1, Comparable[] a2)
177    {
178      int length = Math.min (a1.length, a2.length);
179      for (int i = 0; i < length; i++)
180      {
181        Comparable o1 = a1[i];
182        Comparable o2 = a2[i];
183        if (o1 == null && o2 == null)
184        {
185          // cannot decide ..
186          continue;
187        }
188        if (o1 == null)
189        {
190          return 1;
191        }
192        if (o2 == null)
193        {
194           return -1;
195        }
196        int retval = o1.compareTo(o2);
197        if (retval != 0)
198        {
199          return retval;
200        }
201      }
202      return 0;
203    }
204
205}