Skip to content

Instantly share code, notes, and snippets.

View dhsrocha's full-sized avatar
🎯
Focusing

Diego Rocha dhsrocha

🎯
Focusing
View GitHub Profile
@dhsrocha
dhsrocha / Broker.java
Last active April 3, 2025 09:42
Thread-safe publish-subscribe mechanism to It allows subscribers to register themselves for event notifications without preventing garbage collection.
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
@dhsrocha
dhsrocha / FSM.java
Last active April 2, 2025 08:26
A generic finite state machine implementation for managing stateful transitions.
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
/**
* A generic finite state machine implementation for managing state transitions.
*
* @param <S> the type of the state, which must be an enum.
* @param <D> the type of the data associated with the states.
@dhsrocha
dhsrocha / PortUtil.java
Last active April 14, 2021 23:20
Find next available TCP port.
import java.net.ServerSocket;
class PortUtil {
private PortUtil() {}
/**
* Recursively loops until finding a available TCP port, locks it, get
* its value and then releases it.
*
@dhsrocha
dhsrocha / Exec.java
Created February 27, 2021 03:39
Practical sample (Java 8+) for `Runtime.exec` and output returning.
package template;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
@dhsrocha
dhsrocha / setup.sh
Last active April 4, 2020 04:27
Python environment setup with pyenv and pipenv. Also contains self-check logic and middleware setup powered by Shellcheck and Docker respectively.
#!/usr/bin/env sh
set -e
# @see https://gist.github.com/dhsrocha/ee88f54c2e913c59412817e210bd206b/edit
# ::: Global variables
readonly RC="$HOME/.bashrc"
readonly PROJ_NAME="${PWD##*/}"
readonly PROJ_VER="3.8.2"
#!/usr/bin/env sh
set -e; set -f
# http://www.bashoneliners.com/oneliners/243/
CWD=$(cd "$(dirname "$0")" && pwd)
# Run database's docker stack
run_db() { (
sudo docker-compose \
# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.
# Default logging detail level for all instances of SimpleLogger.
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, defaults to "info".
#org.slf4j.simpleLogger.defaultLogLevel=info
# Logging detail level for a SimpleLogger instance named "xxxxx".
# Must be one of ("trace", "debug", "info", "warn", or "error").
@gene1wood
gene1wood / install-depot-multisystem.sh
Last active April 15, 2020 22:36
Create MultiBoot USB
#! /bin/bash
exec >& >(tee -a /tmp/debug-install-depot-multisystem.txt)
# β”‚β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β”‚
# β”‚ install-depot-multisystem.sh β”‚
# β”‚ written by FranΓ§ois Fabre β”‚
# β”‚ MultiSystem LiveUSB β”‚
# β”‚β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β”‚
# Mr Fabre FranΓ§ois @frafa
@branneman
branneman / better-nodejs-require-paths.md
Last active May 15, 2025 11:17
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions