Creating an enum from it’s values

Enum’s provide valueOf(String) to get an enum via it’s String value.  This is good, but doesn’t help if we need to get an enum from one of it’s other values.  I added the following method to an enum.

public static OperationEnum get(String anyProperty) {

    OperationEnum returnValue = null;
    for (OperationEnum operationEnum : values()) {
        if (operationEnum.key.equals(anyProperty) ||
            operationEnum.description.equals(anyProperty)) {
            returnValue = operationEnum;
            break;
        }
    }
    if (returnValue == null) {
        returnValue = valueOf(anyProperty);
    }
    return returnValue;
}

The Enum that I have has a key and a description property. If it doesn’t find a value, I have it use the standard valueOf() implementation as a fallback.

I tested it with the following:

package com.cdi.igs.models.enums;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

/**
 * Tests the operation enum class.
 * @author norris.shelton
 */
public class TestOperationEnum {
    @Rule
    public ExpectedException expectedEx = ExpectedException.none();

    /**
     * Verify that an illegal argument exception is thrown if an unknown value is provided.
     */
    @Test(expected = IllegalArgumentException.class)
    public void testIllegalArgumentException() {
        OperationEnum.get("junk");
    }

    /**
     * Verify that the enum can be found via it's value.
     */
    @Test
    public void testValue() {
        OperationEnum operationEnum = OperationEnum.get("LEAVE_TABLE");
        assertEquals(OperationEnum.LEAVE_TABLE, operationEnum);
    }

    /**
     * Verify that the enum can be found via it's key property.
     */
    @Test
    public void testKey() {
        OperationEnum operationEnum = OperationEnum.get("102");
        assertEquals(OperationEnum.CHIPS_TO_TABLE, operationEnum);
    }

    /**
     * Verify that the enum can be found via it's description property.
     */
    @Test
    public void testDescription() {
        OperationEnum operationEnum = OperationEnum.get("leave table");
        assertEquals(OperationEnum.LEAVE_TABLE, operationEnum);
    }
}
Posted in Uncategorized | Tagged , | Leave a comment

Homebrew commands for mac

I had to switch to Homebrew when we got Mavericks OSX 10.9

Update Homebrew

brew update

Upgrade outdated formulas

brew upgrade

To see what old versions of a formula would be removed

brew cleanup -n

To remove old versions of a formula

brew cleanup

Posted in Mac | Leave a comment

Hamcrest matcher – not

Have you ever had the case where you had to test for the negative of something? I was helping a teammate who wanted to test that a collection was not empty. It was pretty hard to track down how to test for that.

We settled on not()

import static org.hamcrest.Matchers.not;
...
...not(hasSize(0))

This was pretty hard to find. There wasn’t much documentation on negation.

Posted in hamcrest, Java, JUnit | Leave a comment

Regex validation with Jquery Validation Plugin

We needed several regex validations. I coded various custom validators with the regex included, so the regex could be re-used instead of typing the regex again and again.

        /**
         * Custom validator for letters (uppercase/lowercase)
         */
        $.validator.addMethod("lettersOnly", function (value, element) {
            return this.optional(element) || /^[a-zA-Z]+$/i.test(value);
        }, "Please enter letters only.");

        /**
         * Custom validator for numbers only (0-9)
         */
        $.validator.addMethod("numbersOnly", function (value, element) {
            return this.optional(element) || /^[0-9]+$/i.test(value);
        }, "Please enter numbers only.");

        /**
         * Custom validator for dates (0-9 and slashes).  Does not check month, day or year.
         */
        $.validator.addMethod("date", function (value, element) {
            return this.optional(element) || /^[0-9/]+$/i.test(value);
        }, "Please enter dates only.");

        /**
         * Custom validator for phone numbers (0-9, (, ) and -).  Does not check number of digits, etc.
         */
        $.validator.addMethod("phone", function (value, element) {
            return this.optional(element) || /^[0-9()-]+$/i.test(value);
        }, "Please enter dates only.");

        /**
         * Custom validators for letters and numbers only.  Uppercase/lowercase letters and numbers (0-9).
         */
        $.validator.addMethod("lettersAndNumbersOnly", function (value, element) {
            return this.optional(element) || /^[a-zA-Z0-9]+$/i.test(value);
        }, "Please enter letters and numbers only.");

Here is an example of how the validation was wired in via the rules section of the Jquery validation plugin

            rules: {
                firstName: {
                    required: true,
                    minlength: 1,
                    maxlength: 32,
                    lettersOnly: true
                },
Posted in javascript, JQuery | Tagged , , , | Leave a comment

Injecting value from Spring Properties in Java

This is so easy, but I rarely use it and always forget how to use it. This is how you define a properties:

<util:properties id="commonProperties" location="classpath:common-dev.properties"/>

The contents of the properties file is:

hostUrl=my.host.com

To inject it into a class:

    @Value("#{commonProperties.hostUrl}")
    private String hostUrl;
Posted in Java, Spring | Tagged , , | Leave a comment

JDK setting on Mac OS X Maverick causing you pain?

I found a very useful link

http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/

I added the following to my .profile and typed setjdk 1.7 at the command prompt. Problem solved.

function setjdk() {  
  if [ $# -ne 0 ]; then  
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'  
   if [ -n "${JAVA_HOME+x}" ]; then  
    removeFromPath $JAVA_HOME  
   fi  
   export JAVA_HOME=`/usr/libexec/java_home -v $@`  
   export PATH=$JAVA_HOME/bin:$PATH  
  fi  
 }  
 function removeFromPath() {  
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")  
 }
setjdk 1.7
Posted in Bash, OS/X | Tagged , , , | Leave a comment

Mirroring Maven Dependencies

The benefit of having a local Maven repository is that only one developer suffers the delay of the network traffic requests to repo1. If you have a local maven repository and your developers have mirrors setup in their settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <mirror>
            <id>artifactory</id>
            <mirrorOf>*</mirrorOf>
            <url>http://artifactory.cdinteractive.com:8080/artifactory/repo</url>
            <name>Artifactory</name>
        </mirror>
    </mirrors>
</settings>
Posted in Maven | 1 Comment