All posts tagged with 'Code Snippets'

Wildly unused helper: EnumSet

22-Nov-17

Available since Java 1.5 is the nice helper class EnumSet and I still find code like this way to often: public class Foo { enum Thing { A, B, C }   public static final Set<Thing> THINGS = new HashSet<>(); static { THINGS.add(Thing.A); THINGS.add(Thing.B); THINGS.add(Thing.C); } }public class Foo { enum Thing { A, B, […]

Read the complete article »

On my way to Java 8

12-Feb-14

This is a post where i want to collect some new expressions i’m learning on my way to Java 8. Hopefully i’ll keep updating it… Use an IntStream to generate an Array with constant default values // The "() -> 23" is a lambda that matches the functional interface of a Supplier // The the […]

Read the complete article »

Get the uptime of your Java VM

08-Feb-12

You don’t need JConsole or similar for just displaying the approximate uptime of your application respectively your Java Virtual Machine: import java.lang.management.ManagementFactory;   public class Demo { public static void main(String… args) { final long uptime = ManagementFactory.getRuntimeMXBean().getUptime(); System.out.println(String.format("Up for %dms", uptime)); } }import java.lang.management.ManagementFactory; public class Demo { public static void main(String… args) { […]

Read the complete article »

Creating a better PathMatcher for Spring 3

09-Mar-11

Spring 3 has excellent support for mapping URLs to @Controller methods through the @RequestMapping annotation. This works quite well and i especially like the fact having the mapping right next to the method and not in some other config file like routes.rb. My goal was to have urls like http://foobar.com/resource, http://foobar.com/resource.html, http://foobar.com/resource.zip etc. This is […]

Read the complete article »

Oracle “sleep” procedure: DBMS_LOCK.SLEEP

07-Sep-10

There’s a nice little “sleep” procedure in Oracle: A procedure that stops the execution of the current thread for n seconds. Strangely, this method can be called in SQL*Plus like so: EXEC dbms_lock.sleep(10);exec dbms_lock.sleep(10); but not in another stored procedure or function like so CREATE OR REPLACE PROCEDURE FOOBAR AS BEGIN DBMS_LOCK.SLEEP(1); END; /CREATE OR […]

Read the complete article »