Skip to content

Commit

Permalink
Moved random generator utils from U to a separate class (Rnd).
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jan 24, 2015
1 parent 15e66f0 commit cbc5b1f
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 66 deletions.
Expand Up @@ -24,6 +24,7 @@
import org.rapidoid.data.Range; import org.rapidoid.data.Range;
import org.rapidoid.data.Ranges; import org.rapidoid.data.Ranges;
import org.rapidoid.util.Constants; import org.rapidoid.util.Constants;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;
import org.testng.annotations.Test; import org.testng.annotations.Test;


Expand Down Expand Up @@ -368,7 +369,7 @@ public void testDeleteAfter() {


int size = 0; int size = 0;
for (int i = 0; i < 1000000; i++) { for (int i = 0; i < 1000000; i++) {
int add = U.rnd(100); int add = Rnd.rnd(100);


for (int j = 0; j < 5; j++) { for (int j = 0; j < 5; j++) {
size += add; size += add;
Expand All @@ -378,14 +379,14 @@ public void testDeleteAfter() {


for (int j = 0; j < 5; j++) { for (int j = 0; j < 5; j++) {
if (buf.size() > 0) { if (buf.size() > 0) {
if (U.rnd(2) == 0) { if (Rnd.rnd(2) == 0) {
int delFrom = U.rnd(size); int delFrom = Rnd.rnd(size);
int delN = size - delFrom; int delN = size - delFrom;


size -= delN; size -= delN;
buf.deleteAfter(delFrom); buf.deleteAfter(delFrom);
} else { } else {
int delN = U.rnd(size); int delN = Rnd.rnd(size);


size -= delN; size -= delN;
buf.deleteBefore(delN); buf.deleteBefore(delN);
Expand Down
Expand Up @@ -24,6 +24,7 @@


import org.rapidoid.config.Conf; import org.rapidoid.config.Conf;
import org.rapidoid.db.model.Person; import org.rapidoid.db.model.Person;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;
import org.testng.annotations.Test; import org.testng.annotations.Test;


Expand Down Expand Up @@ -52,7 +53,7 @@ public void run() {
U.benchmarkMT(10, "update", count, latch, new Runnable() { U.benchmarkMT(10, "update", count, latch, new Runnable() {
@Override @Override
public void run() { public void run() {
int id = U.rnd(count) + 1; int id = Rnd.rnd(count) + 1;
DB.update(id, new Person("x", id * 100)); DB.update(id, new Person("x", id * 100));
latch.countDown(); latch.countDown();
} }
Expand Down
Expand Up @@ -22,6 +22,7 @@


import org.rapidoid.config.Conf; import org.rapidoid.config.Conf;
import org.rapidoid.db.model.IPerson; import org.rapidoid.db.model.IPerson;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;
import org.testng.annotations.Test; import org.testng.annotations.Test;


Expand All @@ -48,7 +49,7 @@ public void run() {
U.benchmarkMT(10, "update", count, new Runnable() { U.benchmarkMT(10, "update", count, new Runnable() {
@Override @Override
public void run() { public void run() {
int id = U.rnd(count) + 1; int id = Rnd.rnd(count) + 1;
DB.update(id, DB.create(IPerson.class, "name", "x", "age", id * 100)); DB.update(id, DB.create(IPerson.class, "name", "x", "age", id * 100));
} }
}); });
Expand Down
Expand Up @@ -22,6 +22,7 @@


import org.rapidoid.config.Conf; import org.rapidoid.config.Conf;
import org.rapidoid.db.DB; import org.rapidoid.db.DB;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;


public class DbPersistenceBenchmark { public class DbPersistenceBenchmark {
Expand All @@ -48,7 +49,7 @@ public void run() {
U.benchmarkMT(Conf.cpus(), "update", size, new Runnable() { U.benchmarkMT(Conf.cpus(), "update", size, new Runnable() {
@Override @Override
public void run() { public void run() {
DB.update(U.rnd(size) + 1, new Person("xyz", 10)); DB.update(Rnd.rnd(size) + 1, new Person("xyz", 10));
} }
}); });


Expand Down
Expand Up @@ -26,6 +26,7 @@


import org.rapidoid.html.Cmd; import org.rapidoid.html.Cmd;
import org.rapidoid.html.TagContext; import org.rapidoid.html.TagContext;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;
import org.rapidoid.var.Var; import org.rapidoid.var.Var;


Expand All @@ -41,7 +42,7 @@ public class TagContextImpl implements TagContext, Serializable {
public int newBinding(Var<Object> binding) { public int newBinding(Var<Object> binding) {
int hnd; int hnd;
do { do {
hnd = Math.abs(U.rnd()); hnd = Math.abs(Rnd.rnd());
} while (bindings.containsKey(hnd)); } while (bindings.containsKey(hnd));


bindings.put(hnd, binding); bindings.put(hnd, binding);
Expand All @@ -53,7 +54,7 @@ public int newBinding(Var<Object> binding) {
public int newCommand(Cmd cmd) { public int newCommand(Cmd cmd) {
int hnd; int hnd;
do { do {
hnd = Math.abs(U.rnd()); hnd = Math.abs(Rnd.rnd());
} while (commands.containsKey(hnd)); } while (commands.containsKey(hnd));


commands.put(hnd, cmd); commands.put(hnd, cmd);
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.rapidoid.util.Cls; import org.rapidoid.util.Cls;
import org.rapidoid.util.Constants; import org.rapidoid.util.Constants;
import org.rapidoid.util.IO; import org.rapidoid.util.IO;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;
import org.rapidoid.util.UTILS; import org.rapidoid.util.UTILS;
import org.rapidoid.util.UserInfo; import org.rapidoid.util.UserInfo;
Expand Down Expand Up @@ -691,7 +692,7 @@ public synchronized String sessionId() {
} }


if (sessionId == null) { if (sessionId == null) {
sessionId = U.rndStr(50); sessionId = Rnd.rndStr(50);
setCookie(SESSION_COOKIE, sessionId, "path=/"); setCookie(SESSION_COOKIE, sessionId, "path=/");
session.openSession(sessionId); session.openSession(sessionId);
} }
Expand Down
Expand Up @@ -25,7 +25,7 @@


import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.rapidoid.util.IO; import org.rapidoid.util.IO;
import org.rapidoid.util.U; import org.rapidoid.util.Rnd;
import org.testng.annotations.Test; import org.testng.annotations.Test;


public class HttpServerHeadersTest extends HttpTestCommons { public class HttpServerHeadersTest extends HttpTestCommons {
Expand Down Expand Up @@ -55,7 +55,7 @@ public Object handle(HttpExchange x) {
if (x.cookie("ses", null) == null) { if (x.cookie("ses", null) == null) {
x.setCookie("ses", "023B"); x.setCookie("ses", "023B");
} }
x.setCookie("key" + U.rnd(100), "val" + U.rnd(100)); x.setCookie("key" + Rnd.rnd(100), "val" + Rnd.rnd(100));


return x.writeJSON(x.cookies()); return x.writeJSON(x.cookies());
} }
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.rapidoid.log.Log; import org.rapidoid.log.Log;
import org.rapidoid.net.TCPServer; import org.rapidoid.net.TCPServer;
import org.rapidoid.util.Cls; import org.rapidoid.util.Cls;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;


public class RapidoidServerLoop extends AbstractEventLoop<TCPServer> implements TCPServer { public class RapidoidServerLoop extends AbstractEventLoop<TCPServer> implements TCPServer {
Expand Down Expand Up @@ -161,7 +162,7 @@ public synchronized TCPServer shutdown() {
} }


public synchronized RapidoidConnection newConnection() { public synchronized RapidoidConnection newConnection() {
int rndWorker = U.rnd(workers.length); int rndWorker = Rnd.rnd(workers.length);
return workers[rndWorker].newConnection(); return workers[rndWorker].newConnection();
} }


Expand Down
@@ -1,6 +1,7 @@
package org.rapidoid.oauth; package org.rapidoid.oauth;


import org.rapidoid.config.Conf; import org.rapidoid.config.Conf;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U; import org.rapidoid.util.U;


/* /*
Expand Down Expand Up @@ -31,7 +32,7 @@ public String generateState(String clientSecret, String sessionId) {
return "OK"; return "OK";
} }


String rnd = U.rndStr(10); String rnd = Rnd.rndStr(10);
String hash = U.md5(clientSecret + rnd); String hash = U.md5(clientSecret + rnd);
return rnd + "_" + hash; return rnd + "_" + hash;
} }
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.rapidoid.security.DataPermissions; import org.rapidoid.security.DataPermissions;
import org.rapidoid.util.AppCtx; import org.rapidoid.util.AppCtx;
import org.rapidoid.util.Cls; import org.rapidoid.util.Cls;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.TypeKind; import org.rapidoid.util.TypeKind;
import org.rapidoid.util.U; import org.rapidoid.util.U;
import org.rapidoid.var.Var; import org.rapidoid.var.Var;
Expand Down Expand Up @@ -573,7 +574,7 @@ public static Tag[] radios(String name, Collection<?> options, Var<?> var) {
} }


public static Tag[] radios(Collection<?> options, Var<?> var) { public static Tag[] radios(Collection<?> options, Var<?> var) {
return radios(U.rndStr(30), options, var); return radios(Rnd.rndStr(30), options, var);
} }


public static Tag[] checkboxes(String name, Collection<?> options, Var<?> var) { public static Tag[] checkboxes(String name, Collection<?> options, Var<?> var) {
Expand All @@ -590,7 +591,7 @@ public static Tag[] checkboxes(String name, Collection<?> options, Var<?> var) {
} }


public static Tag[] checkboxes(Collection<?> options, Var<?> var) { public static Tag[] checkboxes(Collection<?> options, Var<?> var) {
return checkboxes(U.rndStr(30), options, var); return checkboxes(Rnd.rndStr(30), options, var);
} }


public static Object display(Object item) { public static Object display(Object item) {
Expand Down
77 changes: 77 additions & 0 deletions rapidoid-u/src/main/java/org/rapidoid/util/Rnd.java
@@ -0,0 +1,77 @@
package org.rapidoid.util;

/*
* #%L
* rapidoid-u
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski
* %%
* Licensed 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.
* #L%
*/

import java.util.Random;

public class Rnd {

protected static final Random RND = new Random();

public static char rndChar() {
return (char) (65 + rnd(26));
}

public static String rndStr(int length) {
return rndStr(length, length);
}

public static String rndStr(int minLength, int maxLength) {
int len = minLength + rnd(maxLength - minLength + 1);
StringBuffer sb = new StringBuffer();

for (int i = 0; i < len; i++) {
sb.append(rndChar());
}

return sb.toString();
}

public static int rnd(int n) {
return RND.nextInt(n);
}

public static int rndExcept(int n, int except) {
if (n > 1 || except != 0) {
while (true) {
int num = RND.nextInt(n);
if (num != except) {
return num;
}
}
} else {
throw new RuntimeException("Cannot produce such number!");
}
}

public static <T> T rnd(T[] arr) {
return arr[rnd(arr.length)];
}

public static int rnd() {
return RND.nextInt();
}

public static long rndL() {
return RND.nextLong();
}

}
50 changes: 0 additions & 50 deletions rapidoid-u/src/main/java/org/rapidoid/util/U.java
Expand Up @@ -34,7 +34,6 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -47,7 +46,6 @@


public class U { public class U {


protected static final Random RND = new Random();
private static ScheduledThreadPoolExecutor EXECUTOR; private static ScheduledThreadPoolExecutor EXECUTOR;
private static long measureStart; private static long measureStart;


Expand Down Expand Up @@ -891,52 +889,4 @@ public static <T> int cmp(T val1, T val2) {
} }
} }


public static char rndChar() {
return (char) (65 + rnd(26));
}

public static String rndStr(int length) {
return rndStr(length, length);
}

public static String rndStr(int minLength, int maxLength) {
int len = minLength + rnd(maxLength - minLength + 1);
StringBuffer sb = new StringBuffer();

for (int i = 0; i < len; i++) {
sb.append(rndChar());
}

return sb.toString();
}

public static int rnd(int n) {
return RND.nextInt(n);
}

public static int rndExcept(int n, int except) {
if (n > 1 || except != 0) {
while (true) {
int num = RND.nextInt(n);
if (num != except) {
return num;
}
}
} else {
throw new RuntimeException("Cannot produce such number!");
}
}

public static <T> T rnd(T[] arr) {
return arr[rnd(arr.length)];
}

public static int rnd() {
return RND.nextInt();
}

public static long rndL() {
return RND.nextLong();
}

} }

0 comments on commit cbc5b1f

Please sign in to comment.