Java code snippet: Enumeration to return String value

I probably should be adding this kind of stuff on GitHub, but I’m lazy. 🙂

public enum TokenType {


    private String tokenType;

    TokenType(String tokenType) {
        this.tokenType = tokenType;

    public String getTokenType() {
        return tokenType;

    public boolean isTicket() {
        return this == TICKET;

    public boolean isValidType() {
        return this == TICKET;

    public static TokenType getByTokenType(String tokenType) {
        String safeTokenType = (tokenType != null) ? tokenType.toLowerCase().trim() : "";

        for (TokenType tt : values()) {
            if (tt.getTokenType().equals(safeTokenType)) {
                return tt;

        return UNKNOWN;

    public boolean isUnknown() {
        return this == UNKNOWN;

Maven error with Java 6 on OS X: “Unsupported major.minor version 51.0”

If you encounter the error below with Java 6 on OS X, make sure you’re using Maven 3.2.5 or older.

$ mvn
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClassCond(
  at java.lang.ClassLoader.defineClass(
  at Method)
  at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(
  at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(
  at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(
  at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(
  at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(
  at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(
  at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(
  at org.codehaus.plexus.classworlds.launcher.Launcher.launch(
  at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(
  at org.codehaus.plexus.classworlds.launcher.Launcher.main(

Seven performance metrics for Java applications

I came across this in this blog post and am adding it here as a checklist. It’s a worthwhile list to have so you can understand how your Java app behaves in production.

  1. Response times and throughput
  2. Load Average
  3. Error Rates (and how to solve them)
  4. GC rate and pause duration
  5. Business Metrics
  6. Uptime and service health
  7. Log size

Switching between multiple Java versions on OS X quickly

If you’re a Java developer like me, you will need to work on various Java versions. If so, adding the following in your ~./bash_profile makes switching between versions a snap. (As of this writing, I’m running on OS X El Capitan.)

alias java6='export JAVA_HOME=$(/usr/libexec/java_home -v 1.6);java -version'
alias java7='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7);java -version'
alias java8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8);java -version'
alias java9='export JAVA_HOME=$(/usr/libexec/java_home -v 9);java -version'

NOTE: Java 9 above is the beta version, so it may be 1.9 once it’s been finalized.

How to install jshell via Homebrew

At the time of this writing, Java 9 is in beta. I installed because I wanted to check out jshell, Java’s new REPL (read-evaluate-print-loop). On top of the below, I brew-installed jenv (a must for managing different JDK versions).

$ brew install Caskroom/versions/java9-beta
$ jenv add /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
$ jenv shell oracle64-9-ea
$ java -version
java version "9-ea"
Java(TM) SE Runtime Environment (build
Java HotSpot(TM) 64-Bit Server VM (build, mixed mode)
$ jshell
|  Welcome to JShell -- Version 9-ea
|  Type /help for help

-> "Hello JDK 9!"
|  Expression value is: "Hello JDK 9!"
|    assigned to temporary variable $1 of type String

I also did the following to get jshell to work:

1. Added to ~/.bash_profile

eval "$(jenv init -)"

2. Added my hostname in /etc/hosts


Cheers, and enjoy learning lambdas!