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 2011/12/12 13:51:34 UTC
svn commit: r1213218 - in
/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk:
index/BTreeLeaf.java index/BTreeNode.java util/ArrayUtils.java
util/StringUtils.java
Author: thomasm
Date: Mon Dec 12 12:51:33 2011
New Revision: 1213218
URL: http://svn.apache.org/viewvc?rev=1213218&view=rev
Log:
Move array utility methods to ArrayUtils.
Added:
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java
Modified:
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java?rev=1213218&r1=1213217&r2=1213218&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java Mon Dec 12 12:51:33 2011
@@ -18,8 +18,8 @@ package org.apache.jackrabbit.mk.index;
import java.util.Arrays;
import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.util.ArrayUtils;
import org.apache.jackrabbit.mk.util.PathUtils;
-import org.apache.jackrabbit.mk.util.StringUtils;
/**
* An index leaf page.
@@ -51,14 +51,14 @@ class BTreeLeaf extends BTreePage {
void insert(int pos, String key, String value) {
tree.modified(this);
- keys = StringUtils.arrayInsert(keys, pos, key);
- values = StringUtils.arrayInsert(values, pos, value);
+ keys = ArrayUtils.arrayInsert(keys, pos, key);
+ values = ArrayUtils.arrayInsert(values, pos, value);
}
void delete(int pos) {
tree.modified(this);
- keys = StringUtils.arrayRemove(keys, pos);
- values = StringUtils.arrayRemove(values, pos);
+ keys = ArrayUtils.arrayRemove(keys, pos);
+ values = ArrayUtils.arrayRemove(values, pos);
}
void writeData() {
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java?rev=1213218&r1=1213217&r2=1213218&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java Mon Dec 12 12:51:33 2011
@@ -18,8 +18,8 @@ package org.apache.jackrabbit.mk.index;
import java.util.Arrays;
import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.util.ArrayUtils;
import org.apache.jackrabbit.mk.util.PathUtils;
-import org.apache.jackrabbit.mk.util.StringUtils;
/**
* An index node page.
@@ -131,17 +131,17 @@ class BTreeNode extends BTreePage {
tree.modified(this);
if (size() > 0) {
// empty parent
- keys = StringUtils.arrayRemove(keys, Math.max(0, pos - 1));
- values = StringUtils.arrayRemove(values, Math.max(0, pos - 1));
+ keys = ArrayUtils.arrayRemove(keys, Math.max(0, pos - 1));
+ values = ArrayUtils.arrayRemove(values, Math.max(0, pos - 1));
}
- children = StringUtils.arrayRemove(children, pos);
+ children = ArrayUtils.arrayRemove(children, pos);
}
void insert(int pos, String key, String value, String child) {
tree.modified(this);
- keys = StringUtils.arrayInsert(keys, pos, key);
- values = StringUtils.arrayInsert(values, pos, value);
- children = StringUtils.arrayInsert(children, pos + 1, child);
+ keys = ArrayUtils.arrayInsert(keys, pos, key);
+ values = ArrayUtils.arrayInsert(values, pos, value);
+ children = ArrayUtils.arrayInsert(children, pos + 1, child);
}
boolean isEmpty() {
Added: 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=1213218&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java (added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ArrayUtils.java Mon Dec 12 12:51:33 2011
@@ -0,0 +1,180 @@
+/*
+ * 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 java.lang.reflect.Array;
+
+/**
+ * Array utility methods.
+ */
+public class ArrayUtils {
+
+ public static final String[] EMPTY_STRING_ARRAY = new String[0];
+ public static final long[] EMPTY_LONG_ARRAY = new long[0];
+ public static final int[] EMPTY_INTEGER_ARRAY = new int[0];
+
+ /**
+ * Insert an element into an 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 int[] arrayInsert(int[] values, int index, int x) {
+ int size = values.length;
+ int[] v2 = new int[size + 1];
+ v2[index] = x;
+ copyArrayAdd(values, v2, size, index);
+ return v2;
+ }
+
+ /**
+ * Insert an element into an 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 long[] arrayInsert(long[] values, int index, long x) {
+ int size = values.length;
+ long[] v2 = new long[size + 1];
+ v2[index] = x;
+ copyArrayAdd(values, v2, size, index);
+ return v2;
+ }
+
+ /**
+ * Insert an element into an 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[] arrayInsert(T[] values, int index, T x) {
+ int size = values.length;
+ @SuppressWarnings("unchecked")
+ T[] v2 = (T[]) Array.newInstance(values.getClass().getComponentType(), size + 1);
+ v2[index] = x;
+ copyArrayAdd(values, v2, size, index);
+ return v2;
+ }
+
+ /**
+ * Insert an element into an 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 String[] arrayInsert(String[] values, int index, String x) {
+ int size = values.length;
+ String[] v2 = new String[size + 1];
+ v2[index] = x;
+ copyArrayAdd(values, v2, size, index);
+ return v2;
+ }
+
+ /**
+ * Remove an element from an array at the given position.
+ *
+ * @param values the values
+ * @param index the index
+ * @return the new array
+ */
+ public static int[] arrayRemove(int[] values, int index) {
+ int size = values.length;
+ if (size == 1) {
+ return EMPTY_INTEGER_ARRAY;
+ }
+ int[] v2 = new int[size - 1];
+ copyArrayRemove(values, v2, size, index);
+ return v2;
+ }
+
+ /**
+ * Remove an element from an array at the given position.
+ *
+ * @param values the values
+ * @param index the index
+ * @return the new array
+ */
+ public static <T> T[] arrayRemove(T[] values, int index) {
+ int size = values.length;
+ @SuppressWarnings("unchecked")
+ T[] v2 = (T[]) Array.newInstance(values.getClass().getComponentType(), size - 1);
+ copyArrayRemove(values, v2, size, index);
+ return v2;
+ }
+
+ /**
+ * Remove an element from an array at the given position.
+ *
+ * @param values the values
+ * @param index the index
+ * @return the new array
+ */
+ public static long[] arrayRemove(long[] values, int index) {
+ int size = values.length;
+ if (size == 1) {
+ return EMPTY_LONG_ARRAY;
+ }
+ long[] v2 = new long[size - 1];
+ copyArrayRemove(values, v2, size, index);
+ return v2;
+ }
+
+
+ /**
+ * Remove an element from an array at the given position.
+ *
+ * @param values the values
+ * @param index the index
+ * @return the new array
+ */
+ public static String[] arrayRemove(String[] values, int index) {
+ int size = values.length;
+ if (size == 1) {
+ return EMPTY_STRING_ARRAY;
+ }
+ String[] v2 = new String[size - 1];
+ copyArrayRemove(values, v2, size, index);
+ return v2;
+ }
+
+ private static void copyArrayAdd(Object src, Object dst, int size, int index) {
+ if (index > 0) {
+ System.arraycopy(src, 0, dst, 0, index);
+ }
+ if (index < size) {
+ System.arraycopy(src, index, dst, index + 1, size - index);
+ }
+ }
+
+ private static void copyArrayRemove(Object src, Object dst, int size, int index) {
+ if (index > 0 && size > 0) {
+ System.arraycopy(src, 0, dst, 0, index);
+ }
+ if (index < size) {
+ System.arraycopy(src, index + 1, dst, index, size - index - 1);
+ }
+ }
+
+}
Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java?rev=1213218&r1=1213217&r2=1213218&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java Mon Dec 12 12:51:33 2011
@@ -80,112 +80,4 @@ public class StringUtils {
}
}
- /**
- * Insert an element into an 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 int[] arrayInsert(int[] values, int index, int x) {
- int size = values.length;
- int[] v2 = new int[size + 1];
- v2[index] = x;
- copyArrayAdd(values, v2, size, index);
- return v2;
- }
-
- /**
- * Insert an element into an 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 long[] arrayInsert(long[] values, int index, long x) {
- int size = values.length;
- long[] v2 = new long[size + 1];
- v2[index] = x;
- copyArrayAdd(values, v2, size, index);
- return v2;
- }
-
- /**
- * Insert an element into an 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 String[] arrayInsert(String[] values, int index, String x) {
- int size = values.length;
- String[] v2 = new String[size + 1];
- v2[index] = x;
- copyArrayAdd(values, v2, size, index);
- return v2;
- }
-
- /**
- * Remove an element from an array at the given position.
- *
- * @param values the values
- * @param index the index
- * @return the new array
- */
- public static int[] arrayRemove(int[] values, int index) {
- int size = values.length;
- int[] v2 = new int[size - 1];
- copyArrayRemove(values, v2, size, index);
- return v2;
- }
-
- /**
- * Remove an element from an array at the given position.
- *
- * @param values the values
- * @param index the index
- * @return the new array
- */
- public static long[] arrayRemove(long[] values, int index) {
- int size = values.length;
- long[] v2 = new long[size - 1];
- copyArrayRemove(values, v2, size, index);
- return v2;
- }
-
- /**
- * Remove an element from an array at the given position.
- *
- * @param values the values
- * @param index the index
- * @return the new array
- */
- public static String[] arrayRemove(String[] values, int index) {
- int size = values.length;
- String[] v2 = new String[size - 1];
- copyArrayRemove(values, v2, size, index);
- return v2;
- }
-
- private static void copyArrayAdd(Object src, Object dst, int size, int index) {
- if (index > 0) {
- System.arraycopy(src, 0, dst, 0, index);
- }
- if (index < size) {
- System.arraycopy(src, index, dst, index + 1, size - index);
- }
- }
-
- private static void copyArrayRemove(Object src, Object dst, int size, int index) {
- if (index > 0 && size > 0) {
- System.arraycopy(src, 0, dst, 0, index);
- }
- if (index < size) {
- System.arraycopy(src, index + 1, dst, index, size - index - 1);
- }
- }
-
}