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