Skip to content

Commit

Permalink
Moved some utils out of U.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Nov 1, 2015
1 parent 64a6c7b commit fbb164d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 66 deletions.
16 changes: 13 additions & 3 deletions rapidoid-crypto/src/main/java/org/rapidoid/crypto/Crypto.java
Expand Up @@ -57,14 +57,24 @@ public static Cipher cipher(String transformation) {
} }
} }


public static String bytesAsText(byte[] bytes) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}

return sb.toString();
}

public static byte[] md5Bytes(byte[] bytes) { public static byte[] md5Bytes(byte[] bytes) {
MessageDigest md5 = digest("MD5"); MessageDigest md5 = digest("MD5");
md5.update(bytes); md5.update(bytes);
return md5.digest(); return md5.digest();
} }


public static String md5(byte[] bytes) { public static String md5(byte[] bytes) {
return U.bytesAsText(md5Bytes(bytes)); return bytesAsText(md5Bytes(bytes));
} }


public static String md5(String data) { public static String md5(String data) {
Expand All @@ -78,7 +88,7 @@ public static byte[] sha1Bytes(byte[] bytes) {
} }


public static String sha1(byte[] bytes) { public static String sha1(byte[] bytes) {
return U.bytesAsText(sha1Bytes(bytes)); return bytesAsText(sha1Bytes(bytes));
} }


public static String sha1(String data) { public static String sha1(String data) {
Expand All @@ -92,7 +102,7 @@ public static byte[] sha512Bytes(byte[] bytes) {
} }


public static String sha512(byte[] bytes) { public static String sha512(byte[] bytes) {
return U.bytesAsText(sha512Bytes(bytes)); return bytesAsText(sha512Bytes(bytes));
} }


public static String sha512(String data) { public static String sha512(String data) {
Expand Down
Expand Up @@ -48,7 +48,7 @@


/* /*
* #%L * #%L
* rapidoid-widget * rapidoid-gui
* %% * %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors * Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %% * %%
Expand Down Expand Up @@ -541,7 +541,7 @@ public static Var<Integer> local(String name, int defaultValue, int min, int max
Var<Integer> var = local(name, defaultValue); Var<Integer> var = local(name, defaultValue);


// TODO put the constraints into the variable implementation // TODO put the constraints into the variable implementation
Integer pageN = U.limited(min, var.get(), max); Integer pageN = U.bounded(min, var.get(), max);
var.set(pageN); var.set(pageN);


return var; return var;
Expand Down
Expand Up @@ -2,7 +2,7 @@


/* /*
* #%L * #%L
* rapidoid-widget * rapidoid-gui
* %% * %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors * Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %% * %%
Expand Down Expand Up @@ -30,6 +30,7 @@
import org.rapidoid.io.Res; import org.rapidoid.io.Res;
import org.rapidoid.lambda.Predicate; import org.rapidoid.lambda.Predicate;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.UTILS;


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("4.1.0") @Since("4.1.0")
Expand Down Expand Up @@ -61,7 +62,7 @@ public static PageMenu from(Object data) {
} }


public List<PageMenuItem> leftItems() { public List<PageMenuItem> leftItems() {
return U.filter(items, new Predicate<PageMenuItem>() { return UTILS.filter(items, new Predicate<PageMenuItem>() {
@Override @Override
public boolean eval(PageMenuItem menuItem) throws Exception { public boolean eval(PageMenuItem menuItem) throws Exception {
return !menuItem.isRight(); return !menuItem.isRight();
Expand All @@ -70,7 +71,7 @@ public boolean eval(PageMenuItem menuItem) throws Exception {
} }


public List<PageMenuItem> rightItems() { public List<PageMenuItem> rightItems() {
return U.filter(items, new Predicate<PageMenuItem>() { return UTILS.filter(items, new Predicate<PageMenuItem>() {
@Override @Override
public boolean eval(PageMenuItem menuItem) throws Exception { public boolean eval(PageMenuItem menuItem) throws Exception {
return menuItem.isRight(); return menuItem.isRight();
Expand Down
64 changes: 7 additions & 57 deletions rapidoid-u/src/main/java/org/rapidoid/u/U.java
Expand Up @@ -58,7 +58,6 @@


import org.rapidoid.lambda.Dynamic; import org.rapidoid.lambda.Dynamic;
import org.rapidoid.lambda.Mapper; import org.rapidoid.lambda.Mapper;
import org.rapidoid.lambda.Predicate;


/** /**
* @author Nikolche Mihajlovski * @author Nikolche Mihajlovski
Expand Down Expand Up @@ -683,23 +682,9 @@ public static void bounds(int value, int min, int max) {
must(value >= min && value <= max, "%s is not in the range [%s, %s]!", value, min, max); must(value >= min && value <= max, "%s is not in the range [%s, %s]!", value, min, max);
} }


public static void notNullAll(Object... items) {
for (int i = 0; i < items.length; i++) {
if (items[i] == null) {
throw rte("The item[%s] must NOT be null!", i);
}
}
}

public static <T> T notNull(T value, String msgOrDesc, Object... descArgs) { public static <T> T notNull(T value, String msgOrDesc, Object... descArgs) {
if (value == null) { if (value == null) {
if (msgOrDesc.endsWith("!")) { throw rte("%s must NOT be null!", frmt(msgOrDesc, descArgs));
// message
throw rte(msgOrDesc, descArgs);
} else {
// description
throw rte("%s must NOT be null!", frmt(msgOrDesc, descArgs));
}
} }


return value; return value;
Expand Down Expand Up @@ -775,7 +760,7 @@ public static int num(String s) {
return Integer.parseInt(s); return Integer.parseInt(s);
} }


public static int limited(int min, int value, int max) { public static int bounded(int min, int value, int max) {
return Math.min(Math.max(min, value), max); return Math.min(Math.max(min, value), max);
} }


Expand Down Expand Up @@ -827,8 +812,8 @@ public static <T> List<T> range(Iterable<T> items, int fromIndex, int toIndex) {
// TODO more efficient implementation // TODO more efficient implementation
List<T> list = list(items); List<T> list = list(items);


fromIndex = limited(0, fromIndex, list.size()); fromIndex = bounded(0, fromIndex, list.size());
toIndex = limited(fromIndex, toIndex, list.size()); toIndex = bounded(fromIndex, toIndex, list.size());


return list(list.subList(fromIndex, toIndex)); return list(list.subList(fromIndex, toIndex));
} }
Expand All @@ -853,16 +838,6 @@ public static String triml(String s, String prefix) {
return s.startsWith(prefix) ? s.substring(prefix.length()) : s; return s.startsWith(prefix) ? s.substring(prefix.length()) : s;
} }


public static String bytesAsText(byte[] bytes) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}

return sb.toString();
}

public static void argMust(boolean expectedCondition, String message, Object... args) { public static void argMust(boolean expectedCondition, String message, Object... args) {
if (!expectedCondition) { if (!expectedCondition) {
throw illegalArg(message, args); throw illegalArg(message, args);
Expand Down Expand Up @@ -958,6 +933,9 @@ public Set<V> map(K src) throws Exception {
}); });
} }


/**
* Simpler casts, less warnings.
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T cast(Object value) { public static <T> T cast(Object value) {
return (T) value; return (T) value;
Expand Down Expand Up @@ -1054,34 +1032,6 @@ public static boolean exists(Callable<?> accessChain) {
} }
} }


public static <T> List<T> filter(List<T> items, Predicate<T> predicate) {
List<T> filtered = list();
addIf(items, filtered, predicate);
return filtered;
}

public static <T> Set<T> filter(Set<T> items, Predicate<T> predicate) {
Set<T> filtered = set();
addIf(items, filtered, predicate);
return filtered;
}

public static <T> void addIf(Collection<T> src, Collection<T> dest, Predicate<T> predicate) {
for (T item : src) {
boolean shouldAdd;

try {
shouldAdd = predicate.eval(item);
} catch (Exception e) {
throw rte(e);
}

if (shouldAdd) {
dest.add(item);
}
}
}

public static String uri(String... parts) { public static String uri(String... parts) {
return "/" + constructPath("/", false, parts); return "/" + constructPath("/", false, parts);
} }
Expand Down
28 changes: 28 additions & 0 deletions rapidoid-utils/src/main/java/org/rapidoid/util/UTILS.java
Expand Up @@ -806,4 +806,32 @@ public static byte[] toBytes(Object obj) {
} }
} }


public static <T> List<T> filter(List<T> items, Predicate<T> predicate) {
List<T> filtered = U.list();
addIf(items, filtered, predicate);
return filtered;
}

public static <T> Set<T> filter(Set<T> items, Predicate<T> predicate) {
Set<T> filtered = U.set();
addIf(items, filtered, predicate);
return filtered;
}

public static <T> void addIf(Collection<T> src, Collection<T> dest, Predicate<T> predicate) {
for (T item : src) {
boolean shouldAdd;

try {
shouldAdd = predicate.eval(item);
} catch (Exception e) {
throw U.rte(e);
}

if (shouldAdd) {
dest.add(item);
}
}
}

} }
Expand Up @@ -78,7 +78,7 @@ public OUT nextResult(boolean blocking) {
@Override @Override
protected void loop() { protected void loop() {
IN task = input.take(); IN task = input.take();
U.notNullAll(task); U.notNull(task, "task");


OUT result = Lmbd.eval(mapper, task); OUT result = Lmbd.eval(mapper, task);
U.notNull(result, "worker mapper result"); U.notNull(result, "worker mapper result");
Expand Down

0 comments on commit fbb164d

Please sign in to comment.