Skip to content

Instantly share code, notes, and snippets.

View Koboo's full-sized avatar
🔬
....

Koboo

🔬
....
View GitHub Profile
@Koboo
Koboo / MongoFilters.java
Last active October 5, 2021 06:46
MongoDB Java Driver Filter for equals ignore case / case insensitive
import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;
import java.util.regex.Pattern;
public class MongoFilters {
public static Bson eqIgn(String fieldName, String value) {
String patternString = new StringBuilder("(?i)^").append(value).append("$").toString();
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
@Koboo
Koboo / Tuple.java
Created June 11, 2021 07:05
Simple class to "fake" Tuple functionality from C# in java
public interface Tuple {
static <T extends Tuple, T1, T2> T create(T1 item1, T2 item2) {
return (T) new Two<>(item1, item2);
}
static <T extends Tuple, T1, T2, T3> T create(T1 item1, T2 item2, T3 item3) {
return (T) new Three<>(item1, item2, item3);
}
@Koboo
Koboo / Handle.java
Created June 11, 2021 07:24
Java Consumer imitation with up to 5 parameters
public interface Handle {
interface Two<T1, T2> {
void handle(T1 item1, T2 item2);
}
interface Three<T1, T2, T3> {
void handle(T1 item1, T2 item2, T3 item3);
}
@Koboo
Koboo / BsonToBinary.java
Last active February 15, 2023 19:01
Just a static utility class to convert BSON Documents to byte-array and vice versa
import org.bson.BsonBinaryReader;
import org.bson.BsonBinaryWriter;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.io.BasicOutputBuffer;
import java.nio.ByteBuffer;
@Koboo
Koboo / LongId.java
Created October 5, 2021 06:45
Simple utility class to automatically generate long ids
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Predicate;
public class LongId {
private final AtomicLong atomicLong = new AtomicLong(0);
public long generateId(Predicate<Long> exists) {
if(this.atomicLong.get() == Integer.MAX_VALUE) {
this.atomicLong.set(0);
@Koboo
Koboo / Clipboard.java
Created January 12, 2022 12:59
Viewless Clipboard utility for vaadin flow web-apps
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
public class Clipboard {
public Clipboard() {
String javaScriptFunction = "window.copyToClipboard = (str) => {\n"
+ " const textarea = document.createElement(\"textarea\");\n"
+ " textarea.value = str;\n"
@Koboo
Koboo / StringGenerator.java
Created May 5, 2022 11:11
Create a random string by a given length and characters
public class StringGenerator {
public static String createRandomString(int length, String characters) {
StringBuilder stringBuilder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (characters.length() * Math.random());
stringBuilder.append(characters.charAt(index));
}
return stringBuilder.toString();
}
@Koboo
Koboo / TimeStamp.java
Created August 22, 2022 09:28
Simple TmeStamp class to measure times
import java.util.concurrent.TimeUnit;
public class TimeStamp {
private static final String ZERO = "0";
private static final String MILLIS = "ms";
private static final String NANOS = "ns";
private static final String SECONDS = "s";
private static final String ZERO_MILLIS = ZERO + MILLIS;
@Koboo
Koboo / Pagifier.java
Created August 22, 2022 09:30
Pagifier for generic Objects
public class Pagifier<T> {
private final int maxItemsPerPage;
private final List<List<T>> allPages;
public Pagifier(int maxItemsPerPage) {
this.maxItemsPerPage = maxItemsPerPage;
allPages = new LinkedList<>();
allPages.add(new LinkedList<>());
@Koboo
Koboo / EncryptUtils.java
Created August 22, 2022 09:41
Very simple Encryption utility
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class EncryptUtils {
private static final String ALGORITHM = "AES";