Skip to content

Instantly share code, notes, and snippets.

View SwingGuy1024's full-sized avatar

Miguel Muñoz SwingGuy1024

View GitHub Profile
@SwingGuy1024
SwingGuy1024 / standardWarning.java
Created June 3, 2022 08:23
Log warnings in a standard way
private static void standardWarning(Throwable throwable, WebRequest request) {
// On ResponseException, it's not caused by a bug, so we just log th emessage and path.
// If it's not, it's a bug, so we include a stack trace.
if (throwable instanceof ResponseException) {
if (log.isWarnEnabled()) {
log.warn("Error proecessing request at {}", extractPath(request));
log.warn(throwable.getMessage());
}
} else if (log.isWarnEnabled()) {
log.warn("Error processing request at {}:", extractPath(request), throwable);
@SwingGuy1024
SwingGuy1024 / HashSim.java
Created September 26, 2019 01:24
HashCode for a 2d (Dimension) integer vector, with positive & negative values, with no overlap
public final class HashSim {
private HashSim() { }
public static void main(String[] args) {
for (int i=1; i<13; ++i) {
System.out.printf("%4d -> %4d%n", i, i*(i-1) + (i-1)*(i-1));
}
System.out.println("");
for (int i = 0; i < 13; ++i) {
draw(-1 - i, i, limit(i + 2));
@SwingGuy1024
SwingGuy1024 / CalUtil.java
Last active September 16, 2019 05:15
CalUtil
import static java.lang.Integer.compare;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalField;
import java.time.temporal.UnsupportedTemporalTypeException;
@SwingGuy1024
SwingGuy1024 / TestPosMessgeSerializer.java
Created August 20, 2019 20:11
PhantomReference /Garbage Collection problem
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.junit.Test;
public class TestPosMessageSerializer {
@SwingGuy1024
SwingGuy1024 / New Bad Usage of Optional.md
Created July 30, 2019 05:03
New bad usage of Optional
public enum TicketType {
    LOCAL_ADMIN,
    USER;

    public static Optional<TicketType> getTicketType(String ticket) {
        Optional<TicketType> ticketTypeOptional = Optional.empty();
        if (StringUtils.startsWith(ticket, ADMIN_PREFIX)) {
            ticketTypeOptional = Optional.of(LOCAL_ADMIN);
        } else if (StringUtils.startsWith(ticket, TICKET_PREFIX)) {

ticketTypeOptional = Optional.of(USER);

@SwingGuy1024
SwingGuy1024 / ChromeToPostman.java
Last active February 20, 2019 04:57
Converts Chrome-inspect data to Postman x-www-form-urlencoded data, using Bulk-Edit
package com.neptunedreams.tools;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import static com.neptunedreams.tools.UrlConvert.*;
/**
@SwingGuy1024
SwingGuy1024 / CharConvert.java
Last active February 20, 2019 04:40
Encoder Decoder UI
package com.neptunedreams.tools;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import static com.neptunedreams.tools.UrlConvert.*;
@SwingGuy1024
SwingGuy1024 / UrlConvert.java
Last active February 20, 2019 04:42
Encoder/Decoder
package com.neptunedreams.tools;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.HashSet;
@SwingGuy1024
SwingGuy1024 / BadExample6.java
Last active February 8, 2019 04:48
Test BadExample 6 -- orElse(null)
package com.tillster.exp;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import java.util.Optional;
import java.util.function.BiFunction;
@SuppressWarnings("WeakerAccess")
public class BadOptionalUse6 {
@SwingGuy1024
SwingGuy1024 / TimeConvert.java
Created January 23, 2019 02:28
Convert a millisecond value to a time, base on the Java epoch starting at 1/1/70, UTC
package com.tillster.tools;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
* Convert a number of milliseconds into an time, based on the Java Epoch.
*