Skip to content

Instantly share code, notes, and snippets.

View ogregoire's full-sized avatar

Olivier Grégoire ogregoire

  • Europe
View GitHub Profile
@ogregoire
ogregoire / ThreadContext.java
Last active August 29, 2015 14:14
Proper ThreadLocal usage
import java.util.concurrent.Callable;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
public final class ThreadContext {
private static final ThreadLocal<Context> CONTEXT = new ThreadLocal<>();
private ThreadContext() {
}
import static com.google.common.base.Preconditions.*;
import java.util.Random;
public class RandomDistribution {
public static RandomDistribution using(Random random) {
return new RandomDistribution(checkNotNull(random));
}
private final Random random;
@ogregoire
ogregoire / using git
Last active August 29, 2015 14:25
Using git
We couldn’t find that file to show.
@ogregoire
ogregoire / IsPerfectSquare.java
Created October 17, 2015 18:49
is perfect square
public class IsPerfectSquare {
// Taken from http://stackoverflow.com/a/18686659/180719
private static long goodMask = computeGoodMask(); // 0xC840C04048404040 computed below
private static long computeGoodMask() {
long goodMask = 0L;
for (int i = 0; i < 64; i++) {
goodMask |= Long.MIN_VALUE >>> (i * i);
}
return goodMask;
}
@ogregoire
ogregoire / Confidence.java
Created November 26, 2015 14:02
Confidence score
public class Confidence {
private static double erf(double x) {
double a1 = 0.254829592;
double a2 = -0.284496736;
double a3 = 1.421413741;
double a4 = -1.453152027;
double a5 = 1.061405429;
double p = 0.3275911;
x = Math.abs(x);
double t = 1 / (1 + p * x);
@ogregoire
ogregoire / Proguard example
Last active November 27, 2015 15:39
proguard
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-keep, allowobfuscation class be.ogregoire.*
-keepclassmembers, allowobfuscation class * {
*;
}
@ogregoire
ogregoire / Glob.java
Created December 29, 2015 17:25
Glob Java
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Glob {
public static Glob compile(String pattern) {
return new Glob(Pattern.compile(globToRegex(pattern)));
}
private final Pattern pattern;
@ogregoire
ogregoire / log4j2.xml
Created February 5, 2016 14:32
Log4J 2 - Rolling file with deletion after 60 days.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="baseDir">logs</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
#!/bin/sh
sudo apt-get install software-properties-common python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt-get install oracle-java8-set-default
wget http://download.jboss.org/wildfly/10.0.0.Final/wildfly-10.0.0.Final.tar.gz
sudo tar -zxf wildfly-10.0.0.Final.tar.gz -C /opt/
sudo ln -s /opt/wildfly-10.0.0.Final /opt/wildfly
sudo cp /opt/wildfly/bin/init.d/wildfly.conf /etc/default/wildfly
@ogregoire
ogregoire / Pi.java
Last active March 18, 2016 10:47
Computing Pi to the nth decimal in Java
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Pi {
private static final BigDecimal TWO = new BigDecimal("2");
private static final BigDecimal FOUR = new BigDecimal("4");
private static final BigDecimal FIVE = new BigDecimal("5");
private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239");