Skip to content

Instantly share code, notes, and snippets.

View ansig's full-sized avatar

Anders Sigfridsson ansig

View GitHub Profile
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 / 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...");
@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 / 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 / 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 / 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 / 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 / CallableFuture.java
Last active August 29, 2015 14:10
Use a callable object to retrieve values from tasks executed in a thread pool.
import java.util.concurrent.*;
/**
* Use a callable to retrieve values from tasks executed in a thread pool.
*/
public class CallableFuture {
public static void main(String[] args) {
Callable<Long> task = new Factorial(20);
@ansig
ansig / ThreadPool.java
Created November 27, 2014 19:26
A thread pool with the executor framework to execute tasks. Also demonstrates a simple timeout mechanism.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* A thread pool with the executor framework to execute tasks. Also demonstrates a simple timeout mechanism.
*/
public class ThreadPool {
private static final int NTHREADS = Runtime.getRuntime().availableProcessors();
@ansig
ansig / DirWatcher.java
Created November 18, 2014 19:57
Watch a directory for changes using a WatchService.
import java.io.IOException;
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
/**
* Watch a directory for changes using a WatchService.
*/
public class DirWatcher {