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.