You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2012/02/07 11:16:03 UTC

svn commit: r1241409 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java test/java/org/apache/jackrabbit/mk/util/ArrayUtilsTest.java

Author: thomasm
Date: Tue Feb  7 10:16:03 2012
New Revision: 1241409

URL: http://svn.apache.org/viewvc?rev=1241409&view=rev
Log:
Improved ArrayUtils and a test case.

Added:
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/ArrayUtilsTest.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java?rev=1241409&r1=1241408&r2=1241409&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java Tue Feb  7 10:16:03 2012
@@ -28,7 +28,24 @@ public class ArrayUtils {
     public static final int[] EMPTY_INTEGER_ARRAY = new int[0];
 
     /**
-     * Insert an element into an array at the given position.
+     * Replace an element in a clone of the array at the given position.
+     *
+     * @param values the values
+     * @param index the index
+     * @param x the value to add
+     * @return the new array
+     */
+    public static <T> T[] arrayReplace(T[] values, int index, T x) {
+        int size = values.length;
+        @SuppressWarnings("unchecked")
+        T[] v2 = (T[]) Array.newInstance(values.getClass().getComponentType(), size);
+        System.arraycopy(values, 0, v2, 0, size);
+        v2[index] = x;
+        return v2;
+    }
+
+    /**
+     * Insert an element into a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -44,7 +61,7 @@ public class ArrayUtils {
     }
 
     /**
-     * Insert an element into an array at the given position.
+     * Insert an element into a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -60,7 +77,7 @@ public class ArrayUtils {
     }
 
     /**
-     * Insert an element into an array at the given position.
+     * Insert an element into a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -77,7 +94,7 @@ public class ArrayUtils {
     }
 
     /**
-     * Insert an element into an array at the given position.
+     * Insert an element into a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -93,7 +110,7 @@ public class ArrayUtils {
     }
 
     /**
-     * Remove an element from an array at the given position.
+     * Remove an element from a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -110,7 +127,7 @@ public class ArrayUtils {
     }
 
     /**
-     * Remove an element from an array at the given position.
+     * Remove an element from a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -125,7 +142,7 @@ public class ArrayUtils {
     }
 
     /**
-     * Remove an element from an array at the given position.
+     * Remove an element from a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index
@@ -143,7 +160,7 @@ public class ArrayUtils {
 
 
     /**
-     * Remove an element from an array at the given position.
+     * Remove an element from a clone of the array at the given position.
      *
      * @param values the values
      * @param index the index

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/ArrayUtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/ArrayUtilsTest.java?rev=1241409&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/ArrayUtilsTest.java (added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/ArrayUtilsTest.java Tue Feb  7 10:16:03 2012
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import org.junit.Test;
+
+/**
+ * Tests the ArrayUtils class
+ */
+public class ArrayUtilsTest {
+
+    @Test
+    public void insertInt() {
+        int[] x = {10, 20};
+        int[] y = ArrayUtils.arrayInsert(x, 1, 15);
+        assertFalse(x == y);
+        assertEquals(3, y.length);
+        assertEquals(10, y[0]);
+        assertEquals(15, y[1]);
+        assertEquals(20, y[2]);
+    }
+
+    @Test
+    public void insertLong() {
+        long[] x = {10, 20};
+        long[] y = ArrayUtils.arrayInsert(x, 1, 15);
+        assertFalse(x == y);
+        assertEquals(3, y.length);
+        assertEquals(10, y[0]);
+        assertEquals(15, y[1]);
+        assertEquals(20, y[2]);
+    }
+
+    @Test
+    public void insertObject() {
+        Long[] x = {Long.valueOf(10), Long.valueOf(20)};
+        Long[] y = ArrayUtils.arrayInsert(x, 1, Long.valueOf(15));
+        assertFalse(x == y);
+        assertEquals(3, y.length);
+        assertEquals(Long.valueOf(10), y[0]);
+        assertEquals(Long.valueOf(15), y[1]);
+        assertEquals(Long.valueOf(20), y[2]);
+    }
+
+    @Test
+    public void insertString() {
+        String[] x = {"10", "20"};
+        String[] y = ArrayUtils.arrayInsert(x, 1, "15");
+        assertFalse(x == y);
+        assertEquals(3, y.length);
+        assertEquals("10", y[0]);
+        assertEquals("15", y[1]);
+        assertEquals("20", y[2]);
+    }
+
+    @Test
+    public void removeInt() {
+        int[] x = {10, 20};
+        int[] y = ArrayUtils.arrayRemove(x, 1);
+        assertFalse(x == y);
+        assertEquals(1, y.length);
+        assertEquals(10, y[0]);
+        y = ArrayUtils.arrayRemove(y, 0);
+        assertEquals(0, y.length);
+    }
+
+    @Test
+    public void removeLong() {
+        long[] x = {10, 20};
+        long[] y = ArrayUtils.arrayRemove(x, 1);
+        assertFalse(x == y);
+        assertEquals(1, y.length);
+        assertEquals(10, y[0]);
+        y = ArrayUtils.arrayRemove(y, 0);
+        assertEquals(0, y.length);
+    }
+
+    @Test
+    public void removeObject() {
+        Long[] x = {Long.valueOf(10), Long.valueOf(20)};
+        Long[] y = ArrayUtils.arrayRemove(x, 1);
+        assertFalse(x == y);
+        assertEquals(1, y.length);
+        assertEquals(Long.valueOf(10), y[0]);
+        y = ArrayUtils.arrayRemove(y, 0);
+        assertEquals(0, y.length);
+    }
+
+    @Test
+    public void removeString() {
+        String[] x = {"10", "20"};
+        String[] y = ArrayUtils.arrayRemove(x, 1);
+        assertFalse(x == y);
+        assertEquals(1, y.length);
+        assertEquals("10", y[0]);
+        y = ArrayUtils.arrayRemove(y, 0);
+        assertEquals(0, y.length);
+    }
+
+    @Test
+    public void replaceObject() {
+        Long[] x = {Long.valueOf(10), Long.valueOf(20)};
+        Long[] y = ArrayUtils.arrayReplace(x, 1, Long.valueOf(11));
+        assertFalse(x == y);
+        assertEquals(2, y.length);
+        assertEquals(Long.valueOf(10), y[0]);
+        assertEquals(Long.valueOf(11), y[1]);
+    }
+
+}