Skip to content

Commit

Permalink
Refactored and enriched array utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 7, 2016
1 parent baba6b8 commit b013e12
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 52 deletions.
Expand Up @@ -25,13 +25,13 @@
import org.rapidoid.bytes.ByteBufferBytes; import org.rapidoid.bytes.ByteBufferBytes;
import org.rapidoid.bytes.Bytes; import org.rapidoid.bytes.Bytes;
import org.rapidoid.bytes.BytesUtil; import org.rapidoid.bytes.BytesUtil;
import org.rapidoid.commons.Arr;
import org.rapidoid.data.Range; import org.rapidoid.data.Range;
import org.rapidoid.data.Ranges; import org.rapidoid.data.Ranges;
import org.rapidoid.pool.Pool; import org.rapidoid.pool.Pool;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.Constants; import org.rapidoid.util.Constants;
import org.rapidoid.util.D; import org.rapidoid.util.D;
import org.rapidoid.util.UTILS;
import org.rapidoid.wrap.IntWrap; import org.rapidoid.wrap.IntWrap;


import java.io.IOException; import java.io.IOException;
Expand Down Expand Up @@ -177,7 +177,7 @@ private int _size() {


private void expandUnit() { private void expandUnit() {
if (bufN == bufs.length) { if (bufN == bufs.length) {
bufs = Arr.expand(bufs, 2); bufs = UTILS.expand(bufs, 2);
} }


bufs[bufN] = bufPool.get(); bufs[bufN] = bufPool.get();
Expand Down
4 changes: 2 additions & 2 deletions rapidoid-commons/src/main/java/org/rapidoid/aop/AOP.java
Expand Up @@ -22,8 +22,8 @@


import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.commons.Arr;
import org.rapidoid.lambda.Lmbd; import org.rapidoid.lambda.Lmbd;
import org.rapidoid.util.UTILS;


import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
Expand All @@ -40,7 +40,7 @@ public static void reset() {


public static synchronized void intercept(AOPInterceptor interceptor, Class<? extends Annotation>... annotated) { public static synchronized void intercept(AOPInterceptor interceptor, Class<? extends Annotation>... annotated) {
InterceptorConfig config = new InterceptorConfig(interceptor, annotated); InterceptorConfig config = new InterceptorConfig(interceptor, annotated);
INTERCEPTORS = Arr.expand(INTERCEPTORS, config); INTERCEPTORS = UTILS.expand(INTERCEPTORS, config);
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.UTILS;


import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
Expand All @@ -47,7 +48,7 @@ public static boolean contains(Object arrOrColl, Object value) {
public static Object include(Object arrOrColl, Object item) { public static Object include(Object arrOrColl, Object item) {
if (arrOrColl instanceof Object[]) { if (arrOrColl instanceof Object[]) {
Object[] arr = (Object[]) arrOrColl; Object[] arr = (Object[]) arrOrColl;
return Arr.indexOf(arr, item) < 0 ? Arr.expand(arr, item) : arr; return Arr.indexOf(arr, item) < 0 ? UTILS.expand(arr, item) : arr;
} else if (arrOrColl instanceof Collection<?>) { } else if (arrOrColl instanceof Collection<?>) {
Collection<Object> coll = (Collection<Object>) arrOrColl; Collection<Object> coll = (Collection<Object>) arrOrColl;
if (!coll.contains(item)) { if (!coll.contains(item)) {
Expand All @@ -64,7 +65,7 @@ public static Object exclude(Object arrOrColl, Object item) {
if (arrOrColl instanceof Object[]) { if (arrOrColl instanceof Object[]) {
Object[] arr = (Object[]) arrOrColl; Object[] arr = (Object[]) arrOrColl;
int ind = Arr.indexOf(arr, item); int ind = Arr.indexOf(arr, item);
return ind >= 0 ? Arr.deleteAt(arr, ind) : arr; return ind >= 0 ? UTILS.deleteAt(arr, ind) : arr;
} else if (arrOrColl instanceof Collection<?>) { } else if (arrOrColl instanceof Collection<?>) {
Collection<Object> coll = (Collection<Object>) arrOrColl; Collection<Object> coll = (Collection<Object>) arrOrColl;
if (coll.contains(item)) { if (coll.contains(item)) {
Expand Down
152 changes: 121 additions & 31 deletions rapidoid-commons/src/main/java/org/rapidoid/commons/Arr.java
Expand Up @@ -30,6 +30,8 @@
@Since("2.0.0") @Since("2.0.0")
public class Arr { public class Arr {


/* indexOf */

public static int indexOf(Object[] arr, Object value) { public static int indexOf(Object[] arr, Object value) {
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
if (U.eq(arr[i], value)) { if (U.eq(arr[i], value)) {
Expand All @@ -39,63 +41,151 @@ public static int indexOf(Object[] arr, Object value) {
return -1; return -1;
} }


public static Object[] deleteAt(Object[] arr, int index) { public static int indexOf(boolean[] arr, boolean value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}


Object[] res = new Object[arr.length - 1]; public static int indexOf(byte[] arr, byte value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}


if (index > 0) { public static int indexOf(char[] arr, char value) {
System.arraycopy(arr, 0, res, 0, index); for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
} }
return -1;
}


if (index < arr.length - 1) { public static int indexOf(int[] arr, int value) {
System.arraycopy(arr, index + 1, res, index, res.length - index); for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
} }
return -1;
}


return res; public static int indexOf(long[] arr, long value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
} }


public static <T> T[] expand(T[] arr, int factor) { public static int indexOf(float[] arr, float value) {
int len = arr.length; for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}


arr = Arrays.copyOf(arr, len * factor); public static int indexOf(double[] arr, double value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}

/* contains */

public static <T> boolean contains(T[] arr, T value) {
return indexOf(arr, value) >= 0;
}


return arr; public static boolean contains(boolean[] arr, boolean value) {
return indexOf(arr, value) >= 0;
} }


public static <T> T[] expand(T[] arr, T item) { public static boolean contains(byte[] arr, byte value) {
int len = arr.length; return indexOf(arr, value) >= 0;
}


arr = Arrays.copyOf(arr, len + 1); public static boolean contains(char[] arr, char value) {
arr[len] = item; return indexOf(arr, value) >= 0;
}


return arr; public static boolean contains(int[] arr, int value) {
return indexOf(arr, value) >= 0;
} }


public static <T> T[] subarray(T[] arr, int from, int to) { public static boolean contains(long[] arr, long value) {
return indexOf(arr, value) >= 0;
}

public static boolean contains(float[] arr, float value) {
return indexOf(arr, value) >= 0;
}

public static boolean contains(double[] arr, double value) {
return indexOf(arr, value) >= 0;
}

/* sub */

public static <T> T[] sub(T[] arr, int from, int to) {
int start = from >= 0 ? from : arr.length + from; int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to; int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
}


if (start < 0) { public static boolean[] sub(boolean[] arr, int from, int to) {
start = 0; int start = from >= 0 ? from : arr.length + from;
} int end = to >= 0 ? to : arr.length + to;

return Arrays.copyOfRange(arr, start, end);
if (end > arr.length - 1) { }
end = arr.length - 1;
}


U.must(start <= end, "Invalid range: expected form <= to!"); public static byte[] sub(byte[] arr, int from, int to) {
int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
}


int size = end - start + 1; public static char[] sub(char[] arr, int from, int to) {
int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
}


T[] part = Arrays.copyOf(arr, size); public static int[] sub(int[] arr, int from, int to) {
int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
}


System.arraycopy(arr, start, part, 0, size); public static long[] sub(long[] arr, int from, int to) {
int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
}


return part; public static float[] sub(float[] arr, int from, int to) {
int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
} }


public static boolean isArray(Object value) { public static double[] sub(double[] arr, int from, int to) {
return value != null && value.getClass().isArray(); int start = from >= 0 ? from : arr.length + from;
int end = to >= 0 ? to : arr.length + to;
return Arrays.copyOfRange(arr, start, end);
} }


} }
Expand Up @@ -115,7 +115,7 @@ private DispatchResult alternativeDispatch(PojoRequest req) throws PojoHandlerNo


for (int i = 0; i < parts.length; i++) { for (int i = 0; i < parts.length; i++) {
try { try {
String path = U.join("/", Arr.subarray(parts, 0, i)); String path = U.join("/", Arr.sub(parts, 0, i + 1));
return process(req, req.command(), path, parts, i + 1); return process(req, req.command(), path, parts, i + 1);
} catch (PojoHandlerNotFoundException e) { } catch (PojoHandlerNotFoundException e) {
// ignore, continue trying... // ignore, continue trying...
Expand Down
Expand Up @@ -20,10 +20,10 @@
* #L% * #L%
*/ */


import org.rapidoid.commons.Arr;
import org.rapidoid.insight.AbstractInsightful; import org.rapidoid.insight.AbstractInsightful;
import org.rapidoid.log.Log; import org.rapidoid.log.Log;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.UTILS;


import java.util.concurrent.Callable; import java.util.concurrent.Callable;


Expand Down Expand Up @@ -73,7 +73,7 @@ public void release(T obj) {
int newSize = free.length * expandFactor; int newSize = free.length * expandFactor;
Log.info("The pool wasn't big enough, expanding...", "name", getName(), "old size", free.length, Log.info("The pool wasn't big enough, expanding...", "name", getName(), "old size", free.length,
"new size", newSize); "new size", newSize);
free = Arr.expand(free, expandFactor); free = UTILS.expand(free, expandFactor);
} }


free[freeN++] = obj; free[freeN++] = obj;
Expand Down
41 changes: 37 additions & 4 deletions rapidoid-commons/src/main/java/org/rapidoid/util/UTILS.java
Expand Up @@ -43,10 +43,7 @@
import java.net.Socket; import java.net.Socket;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
Expand Down Expand Up @@ -714,4 +711,40 @@ public static byte[] toBytes(Object obj) {
} }
} }


public static boolean isArray(Object value) {
return value != null && value.getClass().isArray();
}

public static Object[] deleteAt(Object[] arr, int index) {

Object[] res = new Object[arr.length - 1];

if (index > 0) {
System.arraycopy(arr, 0, res, 0, index);
}

if (index < arr.length - 1) {
System.arraycopy(arr, index + 1, res, index, res.length - index);
}

return res;
}

public static <T> T[] expand(T[] arr, int factor) {
int len = arr.length;

arr = Arrays.copyOf(arr, len * factor);

return arr;
}

public static <T> T[] expand(T[] arr, T item) {
int len = arr.length;

arr = Arrays.copyOf(arr, len + 1);
arr[len] = item;

return arr;
}

} }
12 changes: 6 additions & 6 deletions rapidoid-commons/src/test/java/org/rapidoid/arr/ArrTest.java
Expand Up @@ -34,25 +34,25 @@ public class ArrTest extends AbstractCommonsTest {
public void testSubarray() { public void testSubarray() {
String[] arr = new String[]{"aa", "bb", "c", "ddd", "e"}; String[] arr = new String[]{"aa", "bb", "c", "ddd", "e"};


String[] subarr = Arr.subarray(arr, 0, 2); String[] subarr = Arr.sub(arr, 0, 3);
eq(subarr, new String[]{"aa", "bb", "c"}); eq(subarr, new String[]{"aa", "bb", "c"});


subarr = Arr.subarray(arr, 2, 4); subarr = Arr.sub(arr, 2, 5);
eq(subarr, new String[]{"c", "ddd", "e"}); eq(subarr, new String[]{"c", "ddd", "e"});


subarr = Arr.subarray(arr, 0, 4); subarr = Arr.sub(arr, 0, 5);
eq(subarr, new String[]{"aa", "bb", "c", "ddd", "e"}); eq(subarr, new String[]{"aa", "bb", "c", "ddd", "e"});


subarr = Arr.subarray(arr, 3, 3); subarr = Arr.sub(arr, 3, 4);
eq(subarr, new String[]{"ddd"}); eq(subarr, new String[]{"ddd"});


subarr = Arr.subarray(arr, 1, 3); subarr = Arr.sub(arr, 1, 4);
eq(subarr, new String[]{"bb", "c", "ddd"}); eq(subarr, new String[]{"bb", "c", "ddd"});
} }


@Test(expected = RuntimeException.class) @Test(expected = RuntimeException.class)
public void testSubarrayException() { public void testSubarrayException() {
Arr.subarray(new String[]{"aa", "bb", "c"}, 2, 1); Arr.sub(new String[]{"aa", "bb", "c"}, 2, 1);
} }


} }
Expand Up @@ -22,6 +22,7 @@
import org.rapidoid.model.Property; import org.rapidoid.model.Property;
import org.rapidoid.plugins.db.DB; import org.rapidoid.plugins.db.DB;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.UTILS;
import org.rapidoid.var.Var; import org.rapidoid.var.Var;


import java.io.Serializable; import java.io.Serializable;
Expand Down Expand Up @@ -795,7 +796,7 @@ public static Object values(Object... values) {
List<Object> list = U.list(); List<Object> list = U.list();


for (Object value : values) { for (Object value : values) {
if (Arr.isArray(value) && !hasGUIElements(value)) { if (UTILS.isArray(value) && !hasGUIElements(value)) {
value = U.str(value); value = U.str(value);
} }
if (value == null || value instanceof Iterable<?>) { if (value == null || value instanceof Iterable<?>) {
Expand Down

0 comments on commit b013e12

Please sign in to comment.