Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
dehidehidehi / SimpleTestContainer.java
Created January 9, 2024 17:21
TestContainers class setup
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
@dehidehidehi
dehidehidehi / profile.zsh
Created November 30, 2023 23:05
Enable navigating between Tmux panes within zsh using vim-like navigation bindings.
# Unbind ZSH keys occupying VIM-like navigation shortcuts.
bindkey -r '^H' # Unbind Ctrl-h
bindkey -r '^J' # Unbind Ctrl-j
bindkey -r '^K' # Unbind Ctrl-k
bindkey -r '^L' # Unbind Ctrl-l
# Define functions calling Tmux pane navigation.
function tmux-navigate-left() {
tmux select-pane -L
}
@dehidehidehi
dehidehidehi / jshell.sh
Created October 26, 2023 15:44
JShell bootstrap code for loading full application classpath with Maven.
#!/bin/bash
# Compile if needed.
#mvn package -DskipTests
# Remove Maven logs and trim leading and trailing whitespaces with xargs.
cp=$(mvn -q dependency:build-classpath \
-DincludeTypes=jar \
-Dmdep.outputFile=/dev/stderr \
2>&1 >/dev/null | grep -v -E '(DEBUG|INFO|WARNING)' | xargs)
@dehidehidehi
dehidehidehi / Dockerfile
Created October 2, 2023 12:57
Dockerfile with Java and remote debugger attached.
FROM openjdk:20
EXPOSE 9090
EXPOSE 9091
EXPOSE 9092
COPY /target/${project_name}-*.jar /app/app.jar
WORKDIR /app
# Enable remote debugging
/**
* Normalizes a {@link CharSequence} using the {@link Normalizer.Form#NFKC} method;
* It is a Unicode normalization form that replaces characters with their
* compatibility equivalents and then applies canonical composition.
* This method is useful for ensuring that strings containing different
* representations of the same characters are treated as equal.
* <p>
* Example 1:
* Input: "№123"
* Output: "No123"
@dehidehidehi
dehidehidehi / FilterCharacters.java
Created May 15, 2023 13:29
Filters the characters of a given CharSequence based on a provided IntPredicate and a specified Charset.
/**
* Filters the characters of a given CharSequence based on a provided IntPredicate.
* This method is useful for removing unwanted characters from a string, such as non-alphanumeric characters.
*
* @param cs The input CharSequence to be filtered.
* @param characterFilter The IntPredicate to determine which characters should be kept.
* @return A new String containing only the characters that satisfy the characterFilter.
*/
private String filterCharacters(final CharSequence cs, final IntPredicate characterFilter) {
return cs
@dehidehidehi
dehidehidehi / ResourceFileIterator.java
Created May 9, 2023 20:37
Utility for iterating over a folder located in the resources folder in Java.
/**
* Utility for iterating over a folder located in the resources folder.<br>
* Methods using this method are responsible for closing or operate on the stream.
*/
private static Stream<Path> getResourceFolderFiles (String folder) throws IOException, URISyntaxException {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final URI uri = Objects.requireNonNull(loader.getResource(folder)).toURI();
final String path = Paths.get(uri).toString();
return Files.walk(Paths.get(path));
}
@dehidehidehi
dehidehidehi / main.yml
Created January 11, 2023 23:28
Github workflow: execute Maven tests with pom.xml cached-dependencies (.github/workflows/main.yml)
name: 'Test PRs'
on:
pull_request:
types: [ opened, synchronize, reopened ]
jobs:
test:
runs-on: ubuntu-latest
steps:
@dehidehidehi
dehidehidehi / SpringQualifiedBeanConstructorParameterTest.java
Created January 10, 2023 12:07
Assert that the correct @qualified spring bean is being injected into a `constructor`.
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ActiveProfiles;
@dehidehidehi
dehidehidehi / EmptyLinkedHashMapCollector.java
Created December 5, 2022 18:51
Custom collector for a LinkedHashMap with values all set to Optional empty.
/**
* Custom collector for a {@link LinkedHashMap} with values all set to {@link Optional#empty()}
*/
private <T, R> Collector<T, ?, LinkedHashMap<T, Optional<R>>> toEmptyLinkedHashMap() {
return Collectors.toMap(Function.identity(), f -> Optional.empty(), (x, y) -> y, LinkedHashMap::new);
}