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 / Node.java
Last active April 22, 2025 15:55
Java class with methods for managing neighbors and finding the shortest path with different search strategies.
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
@dhsrocha
dhsrocha / Bus.java
Created April 9, 2025 16:08
The Bus class is a thread-safe message bus that implements the Producer-Consumer pattern, allowing asynchronous message handling. It uses a BlockingQueue for queuing messages and an ExecutorService for processing them via an observer.
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/**
* A thread-safe message bus that allows for asynchronous message handling.
* <p>
@dhsrocha
dhsrocha / Validator.java
Last active April 9, 2025 16:06
Chainable validation mechanism that allows for the sequential application of predicates to a specified generic subject. Each predicate can be associated with a custom error message, which is used for reporting when a validation fails.
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* A class that represents a chain of predicates that can be applied to a subject.
* <p>
* Each predicate can have an associated message that is used for error reporting if the predicate
* test fails.
*
@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 / ocr.py
Created March 24, 2025 17:26
Extract text from PDF files.
import logging
import os
import PyPDF2
import fitz
import pytesseract
from PIL import Image
from fpdf import FPDF
# Configure logging
@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 / Cache.java
Last active July 23, 2024 14:16
Generic cache interface and default implementation for JDK 11+
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
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