Skip to content

Instantly share code, notes, and snippets.

View ansig's full-sized avatar

Anders Sigfridsson ansig

View GitHub Profile
@ansig
ansig / MyForkJoinPool.java
Created January 20, 2015 18:52
Solve a computing task with the ForkJoin framework.
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
* Solve a computing task with the ForkJoin framework.
*/
public class MyForkJoinPool {
private static final int NTHREADS = 10;
@ansig
ansig / SerializeDeserialize.java
Created February 1, 2015 20:05
Serialize and deserialize an object. Also demonstrates how constructors are invoked on deserialization.
import java.io.*;
/**
* Serialize and deserialize an object. Also demonstrates how constructors are invoked on deserialization.
*/
public class SerializeDeserialize {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Bar myBar = new Bar();
@ansig
ansig / NioTestClass.java
Created February 2, 2015 19:54
Create, copy, move and delete a file using NIO.
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import static java.nio.file.StandardOpenOption.*;
import static java.nio.file.StandardCopyOption.*;
@ansig
ansig / old_style_vs_new_style_classes.py
Created January 30, 2016 07:01
Demonstrate some differences between old-style classes and new-style classes in Python
class OldStyle():
def __init__(self):
print "Init old style"
def func(self, arg):
print "Invoked old func with: {}".format(arg)
class NewStyle(object):
@ansig
ansig / Java8ParallelStream.java
Created August 1, 2016 12:45
Demonstrates the time difference between sequential streams and parallel streams.
public class Java8ParallelStream {
public static void main(String[] args) {
List<String> values = new ArrayList<>();
int max = 1000000;
for (int i = 0; i < max; i++) {
UUID id = UUID.randomUUID();
values.add(id.toString());
}
NoArgsFunction sequential = () -> values.stream().sorted().count();
@ansig
ansig / TestRunner.java
Created August 2, 2016 06:20
Demonstrates how a simple test runner can work using annotations and reflection.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class TestRunner {
public static void main(String[] args) {
System.out.println("Testing...");
import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
@ansig
ansig / MySpringAppWithProperties.java
Created July 27, 2017 06:11
Spring app annotation config to read properties from file on default path or from parameter
@Configuration
@ComponentScan(basePackages = "sen.ansig.spring")
@PropertySource("classpath:config.properties")
@PropertySource(value = "file:${config.path:config.properties}", ignoreResourceNotFound = true)
public class MySpringAppWithProperties {
}
@ansig
ansig / fetch_artifact_from_nexus.sh
Created July 28, 2017 07:44
This is a Bash script for fetching artifacts from Nexus via the command line
#!/usr/bin/env bash
########################################################################################################
# This is a utility script to fetch artifacts from a Nexus server. It is based on an example #
# from Sonartype Nexus: http://blog.sonatype.com/2011/01/downloading-artifacts-from-nexus-with-bash/ #
########################################################################################################
set -e
REST_PATH=/service/local
@ansig
ansig / jobdsl-add-secure-groovy-script-sandbox-with-configure-block.groovy
Created August 21, 2017 09:11
JobDSL: add secure Groovy script in sandbox with configure blocks
Closure envVar(String key, String val) {
return {
it / 'buildWrappers' / 'EnvInjectBuildWrapper' / 'info' << {
propertiesContent("${key}=${val}")
}
}
}
Closure secureGroovyScript(String scriptText, boolean useSandbox) {
return {