Skip to content

Instantly share code, notes, and snippets.

View thinkbigthings's full-sized avatar
🏠
Working from home

Jason Young thinkbigthings

🏠
Working from home
View GitHub Profile
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Base64;
@thinkbigthings
thinkbigthings / Logout.txt
Last active July 28, 2020 09:41
Get other users' sessions (as admin) to log them out in Spring Security
With sessions it might be necessary to logout on password change. Also the Logout button from UI should additionally logout on the server.
https://stackoverflow.com/questions/44359792/log-out-user-by-admin-spring-security
https://docs.spring.io/spring-security/site/docs/current/reference/html5/#jc-logout
// in WebSecurityConfigurerAdapter
@thinkbigthings
thinkbigthings / Functional.java
Created July 28, 2020 09:31
Some functional utilities in Java
public class Functional {
@FunctionalInterface
public interface CheckedFunction<T, R> {
R apply(T t) throws Exception;
}
public static <T, R> Function<T, R> uncheck(CheckedFunction<T, R> checkedFunction) {
return t -> {
try {
import static java.util.stream.IntStream.rangeClosed;
import static java.util.stream.Collectors.toCollection;
List<Integer> randomList(int firstInclusive, int lastInclusive) {
List<Integer> numbers = rangeClosed(firstInclusive, lastInclusive).boxed().collect(toCollection(()->new ArrayList<Integer>()));
java.util.Collections.shuffle(numbers, new java.security.SecureRandom());
return numbers;
}
// usage: randomList(1,10);
@thinkbigthings
thinkbigthings / canvas.html
Last active March 20, 2018 11:44
plot lots of points on a canvas using real-to-screen coordinate transformation
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="1000" height="500" style="border:1px solid #000000;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
// Math.max overflows if array is too big, use reduce instead
const fastMin = (array) => array.reduce( (a,b) => (a < b) ? a : b );
const fastMax = (array) => array.reduce( (a,b) => (a > b) ? a : b );
@thinkbigthings
thinkbigthings / VariableDepthCopier.java
Last active August 20, 2019 12:27
Handy utility for copying domain objects into a response.
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.BeanUtils;
@thinkbigthings
thinkbigthings / Vagrantfile
Last active December 16, 2015 02:29
Starter vagrant file that creates a machine with Java and MySQL
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<SCRIPT
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y update
apt-get -q -y install emacs
@thinkbigthings
thinkbigthings / HibernateUnproxifier.java
Created March 12, 2013 10:24
How to get the entity behind a Hibernate proxy
import org.hibernate.Hibernate;
import org.hibernate.proxy.HibernateProxy;
public class HibernateUnproxifier<T> {
@SuppressWarnings("unchecked")
public T unproxy(T entity) {
if (entity == null) {
throw new NullPointerException("Entity passed for initialization is null");
Map<String,String> eqs = new HashMap<String,String>();
eqs.put("piApprox(k)","8*cumsum(ChebyshevTerm(k))");
eqs.put("ChebyshevTerm(k)","( (-1)^k * (sqrt(2)-1)^(2*k+1)) / (2*k+1)");
eqs.put("absError(k)", "abs(pi-piApprox)");
EquationProcessor k = new EquationProcessor(eqs);
Map<String,String> defs = new HashMap<String,String>();
defs.put("k","[0:20]");