Skip to content

Instantly share code, notes, and snippets.

@zencd
zencd / stopscript.js
Last active May 22, 2024 20:55
Copy steam games to an SG compilation
await (async function() {
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
async function rewriteAllGames(compId, gameIds) {
console.log('gameIds:', gameIds);
var formData = new FormData();
for (let i = 0; i < gameIds.length; i++) {
let gameId = gameIds[i];
formData.append('items[]', 'game/' + gameId);
@zencd
zencd / bidi.txt
Created May 22, 2024 08:26
bidi example
[⁦]
@zencd
zencd / temp.txt
Last active May 21, 2024 17:33
Temp gist
this is a <font color="red">gist</font>
to embed [⁦]
@zencd
zencd / resolve-field-type-argument.groovy
Created May 6, 2023 18:41
Resolve type argument of a Java generics typed field
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface TestPojo {
String file()
}
@TestPojo(file = "src/test/resources/foo-response.json")
FooResponse fooResponse
// invoke processSpecAnnotations()
@zencd
zencd / validator.groovy
Created May 6, 2023 18:36
Pojo Validator
static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator()
static void validatePojo(Object pojo) {
def errors = VALIDATOR.validate(pojo);
if (errors.size() > 0) {
log.error("Validation errors for {}: {}", pojo.getClass().getSimpleName(), errors);
throw new RuntimeException("Pojo validation failed - see logs - " + pojo.getClass().getName());
}
}
@zencd
zencd / DoubleCheckedLocking.java
Last active April 5, 2023 15:12
Double Checked Locking
/** private final DCLValue<Foo> foo = new DCLValue.create();
* Foo result = foo.getOrCreate(syncOn, () -> new Foo());
* Foo result = foo.get(); */
interface DCLValue<T> {
T getOrCreate(Object syncOn, Supplier<T> creator);
T get();
void set(T value);
static <T> DCLValue<T> create() {
return new DCLValueRegular<>();
@zencd
zencd / EnumByNameExample.java
Last active April 5, 2023 15:14
Find an enum instance by any property of it
enum Foo {
ONE, TWO;
private static class Helper {
public static Map<String, Foo> enumByName = new HashMap<>();
static {
Arrays.stream(Foo.values()).forEach(it -> enumByName.put(it.name(), it));
assert Foo.Helper.enumByName.get("ONE") == Foo.ONE; // usage
}
}
}
public static boolean match(@Nullable Pattern pattern, @Nullable String input) {
if (pattern == null || input == null) {
return false;
}
return pattern.matcher(input).matches();
}
@Nullable
public static String toStr(@Nullable Object value) {
return (value != null) ? value.toString() : null;
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
@zencd
zencd / percentile.py
Last active May 19, 2021 15:13
pure python percentile
import math
def percentile(data, perc: int):
return sorted(data)[int(math.ceil(len(data) * perc / 100)) - 1]
assert 9.0 == percentile([10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], 90)
assert 146 == percentile([142, 232, 290, 120, 274, 123, 146, 113, 272, 119, 124, 277, 207], 50)