/** * Checks if the given object is member of the specified array. * <p/> * <p> The method simply returns <code>getElementIndex(element, array) >= 0</code>. * * @param element the element to be searched * @param array the array in which to search the element, must not be <code>null</code> * * @return <code>true</code> if the given object is member of the specified array */ public static boolean isMemberOf(Object element, Object[] array) { return getElementIndex(element, array) >= 0; }
private String createDefaultSpectrumName() { int spectrumNumber = 1; StringBuilder builder = new StringBuilder("Spectrum "); builder.append(spectrumNumber++); while (ArrayUtils.isMemberOf(builder.toString(), alreadyDefinedSpectrumNames)) { builder.delete(9, builder.length()); builder.append(spectrumNumber++); } return builder.toString(); }
/** * Swaps the content of the given array. * * @param array the array, must be an instance of a native array type such as <code>float[]</code> * * @throws IllegalArgumentException if the given array is <code>null</code> or does not have a native array type */ public static void swapArray(Object array) { Guardian.assertNotNull("array", array); if (array instanceof byte[]) { swapArray((byte[]) array); } else if (array instanceof char[]) { swapArray((char[]) array); } else if (array instanceof short[]) { swapArray((short[]) array); } else if (array instanceof int[]) { swapArray((int[]) array); } else if (array instanceof long[]) { swapArray((long[]) array); } else if (array instanceof float[]) { swapArray((float[]) array); } else if (array instanceof double[]) { swapArray((double[]) array); } else if (array instanceof Object[]) { swapArray((Object[]) array); } else { throw new IllegalArgumentException(UtilConstants.MSG_OBJ_NO_ARRAY); } }
public void testGetElementIndexAndIsMemberOf() { Object[] array1 = {"A", "B", "C"}; assertEquals(0, ArrayUtils.getElementIndex("A", array1)); assertEquals(1, ArrayUtils.getElementIndex("B", array1)); assertEquals(2, ArrayUtils.getElementIndex("C", array1)); assertEquals(-1, ArrayUtils.getElementIndex("D", array1)); assertTrue(ArrayUtils.isMemberOf("A", array1)); assertTrue(ArrayUtils.isMemberOf("B", array1)); assertTrue(ArrayUtils.isMemberOf("C", array1)); assertTrue(!ArrayUtils.isMemberOf("D", array1)); Object[] array2 = {"A", new Object[]{"B1", "B2"}, "C"}; assertEquals(0, ArrayUtils.getElementIndex("A", array2)); assertEquals(1, ArrayUtils.getElementIndex(new Object[]{"B1", "B2"}, array2)); assertEquals(2, ArrayUtils.getElementIndex("C", array2)); assertEquals(-1, ArrayUtils.getElementIndex("D", array2)); assertTrue(ArrayUtils.isMemberOf("A", array2)); assertTrue(ArrayUtils.isMemberOf(new Object[]{"B1", "B2"}, array2)); assertTrue(ArrayUtils.isMemberOf("C", array2)); assertTrue(!ArrayUtils.isMemberOf("D", array2)); }
public void testAddToArrayForInts() { try { ArrayUtils.addToArray(null, 23); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // IllegalArgumentException expected } final int[] oldArray = new int[]{12, 45, 2, 4}; final int[] expArray = new int[]{12, 45, 2, 4, 17}; final int[] newArray = ArrayUtils.addToArray(oldArray, 17); assertEquals(true, Arrays.equals(expArray, newArray)); }
public void testEqualArrays() { Object[] a = {"A", "B", "C"}; Object[] b = {"A", "B", "C"}; Object[] c = {"1", "2", "3"}; Object[] d = {"A", "B", "C", "D"}; Object[] e = a; assertEquals(ArrayUtils.equalArrays(a, b), true); assertEquals(ArrayUtils.equalArrays(a, c), false); assertEquals(ArrayUtils.equalArrays(a, d), false); assertEquals(ArrayUtils.equalArrays(a, e), true); assertEquals(ArrayUtils.equalArrays(null, null), true); assertEquals(ArrayUtils.equalArrays(a, null), false); assertEquals(ArrayUtils.equalArrays(null, a), false); }
public void testCreateIntArray() { assertEquals(true, Arrays.equals(new int[]{18, 19, 20, 21, 22, 23}, ArrayUtils.createIntArray(18, 23))); assertEquals(true, Arrays.equals(new int[]{18, 19, 20, 21, 22, 23}, ArrayUtils.createIntArray(23, 18))); assertEquals(true, Arrays.equals(new int[]{-2, -1, 0, 1, 2, 3}, ArrayUtils.createIntArray(-2, 3))); assertEquals(true, Arrays.equals(new int[]{-2, -1, 0, 1, 2, 3}, ArrayUtils.createIntArray(3, -2))); } }
private int[] withoutMandatoryLines(final int[] value) { return ArrayUtils.addArrays(_notMandatoryLines, value); }
final int rowIndex = getBandRow(newDataVector, bandName); if (rowIndex > -1) { ArrayUtils.addToArray(rowsToRemove, rowIndex);
public void testEqualDoubleArrays() { double[] a = {2.4, 3.7, 0.0005423, -424.6}; double[] b = {2.4, 3.7, 0.0005423, -424.6}; double[] c = {2.4, 3.7, 0.0005657, -424.6}; double[] d = {2.4, 3.7, 0.0005423}; double[] e = a; assertEquals(true, ArrayUtils.equalArrays(a, b, 1e-10)); assertEquals(false, ArrayUtils.equalArrays(a, c, 1e-10)); assertEquals(false, ArrayUtils.equalArrays(a, d, 1e-10)); assertEquals(true, ArrayUtils.equalArrays(a, e, 1e-10)); assertEquals(true, ArrayUtils.equalArrays(null, null, 1e-10)); assertEquals(false, ArrayUtils.equalArrays(a, null, 1e-10)); assertEquals(false, ArrayUtils.equalArrays(null, a, 1e-10)); }
dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(12, 15))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(10, 16))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(8, 17))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(6, 18))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(3, 71))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(1, 72))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(19, 54))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(21, 53))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(23, 52))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product)); dom = createDom(withoutMandatoryLines(ArrayUtils.createIntArray(24, 51))); assertEquals(null, DimapProductHelpers.createGeoCoding(dom, product));
public void testAddArraysForInts() { try { ArrayUtils.addArrays(null, new int[3]); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // IllegalArgumentException expected } try { ArrayUtils.addArrays(new int[3], null); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // IllegalArgumentException expected } /* spacer */ final int[] secondArray = new int[]{27, 32, 1}; final int[] firstArray = new int[]{12, 45, 2, 4}; final int[] expecArray = new int[]{12, 45, 2, 4, 27, 32, 1}; final int[] newArray = ArrayUtils.addArrays(firstArray, secondArray); assertEquals(true, Arrays.equals(expecArray, newArray)); }
public void testSwapLongArray() { final long[] array0 = {}; final long[] array1 = {1}; final long[] array2 = {1, 2}; final long[] array3 = {1, 2, 3}; final long[] array10 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; final long[] array0Rev = {}; final long[] array1Rev = {1}; final long[] array2Rev = {2, 1}; final long[] array3Rev = {3, 2, 1}; final long[] array10Rev = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; ArrayUtils.swapArray(array0); assertTrue(Arrays.equals(array0Rev, array0)); ArrayUtils.swapArray(array1); assertTrue(Arrays.equals(array1Rev, array1)); ArrayUtils.swapArray(array2); assertTrue(Arrays.equals(array2Rev, array2)); ArrayUtils.swapArray(array3); assertTrue(Arrays.equals(array3Rev, array3)); ArrayUtils.swapArray(array10); assertTrue(Arrays.equals(array10Rev, array10)); }
public static int getShape(ImageIcon shapeIcon) { return ArrayUtils.getElementIndex(shapeIcon, shapeIcons); } }
public static String getUniqueFrameTitle(final JInternalFrame[] frames, final String titleBase) { if (frames.length == 0) { return titleBase; } String[] titles = new String[frames.length]; for (int i = 0; i < frames.length; i++) { JInternalFrame frame = frames[i]; titles[i] = frame.getTitle(); } if (!ArrayUtils.isMemberOf(titleBase, titles)) { return titleBase; } for (int i = 0; i < frames.length; i++) { final String title = titleBase + " (" + (i + 2) + ")"; if (!ArrayUtils.isMemberOf(title, titles)) { return title; } } return titleBase + " (" + (frames.length + 1) + ")"; }
public void testEqualFloatArrays() { float[] a = {2.4f, 3.7f, 0.0005423f, -424.6f}; float[] b = {2.4f, 3.7f, 0.0005423f, -424.6f}; float[] c = {2.4f, 3.7f, 0.0005657f, -424.6f}; float[] d = {2.4f, 3.7f, 0.0005423f}; float[] e = a; assertEquals(true, ArrayUtils.equalArrays(a, b, 1e-5f)); assertEquals(false, ArrayUtils.equalArrays(a, c, 1e-5f)); assertEquals(false, ArrayUtils.equalArrays(a, d, 1e-5f)); assertEquals(true, ArrayUtils.equalArrays(a, e, 1e-5f)); assertEquals(true, ArrayUtils.equalArrays((float[]) null, (float[]) null, 1e-5f)); assertEquals(false, ArrayUtils.equalArrays(a, null, 1e-5f)); assertEquals(false, ArrayUtils.equalArrays(null, a, 1e-5f)); }
public void testSwapDoubleArray() { final double[] array0 = {}; final double[] array1 = {1}; final double[] array2 = {1, 2}; final double[] array3 = {1, 2, 3}; final double[] array10 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; final double[] array0Rev = {}; final double[] array1Rev = {1}; final double[] array2Rev = {2, 1}; final double[] array3Rev = {3, 2, 1}; final double[] array10Rev = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; ArrayUtils.swapArray(array0); assertTrue(Arrays.equals(array0Rev, array0)); ArrayUtils.swapArray(array1); assertTrue(Arrays.equals(array1Rev, array1)); ArrayUtils.swapArray(array2); assertTrue(Arrays.equals(array2Rev, array2)); ArrayUtils.swapArray(array3); assertTrue(Arrays.equals(array3Rev, array3)); ArrayUtils.swapArray(array10); assertTrue(Arrays.equals(array10Rev, array10)); }
public static Stroke getStroke(ImageIcon strokeIcon) { return strokes[ArrayUtils.getElementIndex(strokeIcon, strokeIcons)]; }
private boolean isInSpectrum(Band availableSpectralBand) { if (spectrum == null) { return false; } return ArrayUtils.isMemberOf(availableSpectralBand, spectrum.getSpectralBands()); }
public void testRemoveFromArray() { String[] strings; strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "A"); assertTrue("String[]{\"B\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"B", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "B"); assertTrue("String[]{\"A\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "C"); assertTrue("String[]{\"A\",\"B\"} expected", ArrayUtils.equalArrays(new String[]{"A", "B"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "D"); assertTrue("String[]{\"A\",\"B\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "B", "C"}, strings)); }