Math.random() | [0 .................................... 1) [0 .................................... max - min) | x (what we need)
private boolean isMyServiceRunning(Class<?> serviceClass) { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; }
public static String humanReadableByteCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); }
static void sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException ex) { throw new RuntimeException(ex); } } }
if (string.contains("-")) { // Split it. } else { throw new IllegalArgumentException("String " + string + " does not contain -"); }
String string = "004-034556"; String[] parts = string.split("(?<=-)"); String part1 = parts[0]; // 004- String part2 = parts[1]; // 034556
public void waitToFinish() { try { t.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } }
@Override public boolean test(Throwable t) throws Exception { return t.getMessage() != null && t.getMessage().contains("Forced"); } });
@Override public Integer apply(Integer t1) { if (c++ <= 1) { // slow try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } return t1; }
ParamOverride(Class<?> clazz, int index, ParamMode mode, String name, Class<?>... arguments) { this.clazz = clazz; this.index = index; this.mode = mode; this.name = name; this.arguments = arguments; try { clazz.getMethod(name, arguments); } catch (Exception ex) { throw new AssertionError(ex); } }
public CrashDummy assertError(Class<? extends Throwable> clazz) { if (!clazz.isInstance(error)) { throw new AssertionError("Different error: " + error); } return this; }
function delay() { // `delay` returns a promise return new Promise(function(resolve, reject) { // Only `delay` is able to resolve or reject the promise setTimeout(function() { resolve(42); // After 3 seconds, resolve the promise with value 42 }, 3000); }); } delay().then(function(v) { // `delay` returns a promise console.log(v); // Log the value once it is resolved }).catch(function(v) { // Or do something else if it is rejected // (it would not happen in this example, since `reject` is not called). });
function guid() { return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); } function s4() { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } document.getElementById('jsGenId').addEventListener('click', function() { document.getElementById('jsIdResult').value = guid(); })
// or it's a property that can throw an error Object.defineProperty(window, "myVariable", { get: function() { throw new Error("W00t?"); }, set: undefined }); if (myVariable) { // Error: W00t? }
String string = "004-034556"; String[] parts = string.split("-"); String part1 = parts[0]; // 004 String part2 = parts[1]; // 034556
public void waitToFinish() { try { t.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
// These two have the same value new String("test").equals("test") // --> true // ... but they are not the same object new String("test") == "test" // --> false // ... neither are these new String("test") == new String("test") // --> false // ... but these are because literals are interned by // the compiler and thus refer to the same object "test" == "test" // --> true // ... but you should really just call Objects.equals() Objects.equals("test", new String("test")) // --> true Objects.equals(null, "test") // --> false
String string = "004-034556"; String[] parts = string.split("(?=-)"); String part1 = parts[0]; // 004 String part2 = parts[1]; // -034556
String string = "004-034556-42"; String[] parts = string.split("-", 2); String part1 = parts[0]; // 004 String part2 = parts[1]; // 034556-42