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 / Cache.java
Last active January 23, 2024 17:35
Generic cache interface and default implementation for JDK 11+
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
package com.ludo.adapter.awt;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
@dhsrocha
dhsrocha / rebase-develop.sh
Last active March 1, 2023 16:57
Function to rebase all branches from reference one,
#!/bin/sh
# rebase() - Rebase all branches from the specified source branch
#
# Usage:
# rebase <source-branch>
#
# Description:
# This function will check if git is on the PATH, and if the argument "$1"
# has been provided. If not, an error message will be printed and the
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.Random;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
/**
* A utility class to hash passwords and check passwords vs hashed values. It uses a combination of
@dhsrocha
dhsrocha / docker-local-deploy.sh
Last active November 6, 2021 22:48
Docker Swarm local cluster
deploy() { # Creates a local self-contained deployment cluster.
ping() {
until docker exec -it "$1" docker stats --no-stream > /dev/null
do sleep .5; done
}
# Cleanup
if docker swarm ca > /dev/null 2>&1; then docker swarm leave -f; fi
docker ps -aqf name="$(basename "$PWD")" | xargs docker rm -fv > /dev/null
# OpenSSL TODO Apply https://linuxhandbook.com/docker-remote-access/
@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;

Keybase proof

I hereby claim:

  • I am dhsrocha on github.
  • I am dhsrocha (https://keybase.io/dhsrocha) on keybase.
  • I have a public key ASDuXX8Db-1BF6Ch6dVUxLNxz0pQMeLXovKmjuVC6KDCzgo

To claim this, I am signing this object:

@dhsrocha
dhsrocha / HttpClient.java
Created July 19, 2020 08:43
Java 8+ HTTP Client.
import io.swagger.v3.oas.models.PathItem.HttpMethod;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.Collectors;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.java.Log;
import lombok.val;
@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"