• Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Java and Wicket web development thoughts, tutorials, and tips

roman@coderdreams.com
Coder DreamsCoder Dreams
  • Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Using interfaces to make Java enums more flexible

July 10, 2019 Posted by Roman Sery enum, java, wicket No Comments

Plain old java enumerations are pretty boring and quite limiting! They are mostly static but are great for IDE code refactoring tools and reference searches. Imagine you can get the best of both worlds!

Let’s take as an example a simple enumeration below in a hypothetical HR application that you will be selling to customers:

public enum EmployeeDepartmentType {
	DEV_TEAM, HR, EXEC, QA, SALES, PROJ_MANAGEMENT, INTERNS;
}

Of course in the real world, every customer that would be interested in your product would have different department types and you would need to customize your code for each customer which is a pain. Using the power of interfaces we can transform this enum to something more useful:

public interface GenericEnum extends Serializable {
	String getDescription();
	void setDescription(String description);
	boolean isHidden();
	void setHidden(boolean hidden);
	int getId();
}

And now the new enum definition:

public enum EmployeeDepartmentType implements GenericEnum {
	DEV_TEAM(1, "Dev Team"),
	HR(2, "Human resources"),
	EXEC(3, "Executive"),
	QA(4, "Quality assurance/Testing"),
	SALES(5, "Sales"),
	PROJ_MANAGEMENT(6, "Project Managers"),
	INTERNS(7, "Interns")
	;
		
	public static final List<EmployeeDepartmentType> VALUES;
	private final int id;
	private String description;
	private boolean hidden = false;
	
	EmployeeDepartmentType(int id, String description) {
		this.id = id;
		this.description = description;
	}
	
	static {
        VALUES = GenericEnum.configure(EmployeeDepartmentType.class, EmployeeDepartmentType.values());
    }  

	@Override public void setDescription(String description) { this.description = description; }
	@Override public String getDescription() { return description; }
	@Override public int getId() { return id; }	
	@Override public boolean isHidden() { return hidden; }
	@Override public void setHidden(boolean hidden) { this.hidden = hidden; }		
}

Now EmployeeDepartmentType becomes much more flexible! Suppose one of your customers doesn’t have interns or they call the development team something else? You can now easily oblige them while keeping the convenience of Enums using a properties file:

DEV_TEAM.desc=Engineering
EmployeeDepartmentType.INTERN.hidden=1

The trick comes from setting a unique JVM environment variable for each client allowing the code to automatically select the correct properties file. You can do a lot using this technique, stay tuned for more tricks and tips using GenericEnum’s in future posts!

Bonus Wicket code!

As a bonus here is a Wicket ChoiceRenderer that you can re-use for all of your GenericEnum’s:

private static final class GenericEnumChoiceRenderer<T extends GenericEnum> extends ChoiceRenderer<T> {
		private static final long serialVersionUID = 1L;
        @Override
		public Object getDisplayValue(T object) {
        	return object == null ? "" : object.getDescription();
		}
        @Override
		public String getIdValue(T object, int index) {
			return String.valueOf(object.getId());
		}
	}

Check out the full code and simple app below!

https://github.com/RomanSery/codesnippets

No Comments
Share
4

About Roman Sery

I've been a software developer for over 10 years and still loving Java!

You also might be interested in

Wicket quick tip #5: Prevent duplicate appendJavascript() invocations

Aug 12, 2020

Implement a custom AjaxRequestTargetProvider to prevent duplicate Javascript being sent to the browser.

Dynamically add Wicket panels

Aug 8, 2019

How to dynamically choose the implementation of Wicket panels when adding them to your page using reflection and the factory pattern.

Initializing your Wicket components using the builder pattern

Mar 18, 2020

How to use the builder pattern to initialize your custom Wicket components and the advantages of doing it.

Categories

  • aws
  • customization
  • database
  • debugging
  • enum
  • java
  • models
  • performance
  • projects
  • react
  • software design
  • Spring
  • tool
  • Uncategorized
  • wicket

Recent Posts

  • Rent Day
  • Self-contained Wicket Fragments
  • Pros and cons of unit testing
  • Themeable React Monopoly board
  • Please dont use client-specific release branches

Recent Comments

  • TCI Express Thanks for sharing such insightful information. TCI Express truly stands out as the best air logistics company, offering fast, secure, and efficient air express and cold chain transportation services....

    Tracking down a bug in production Wicket application ·  March 25, 2025

  • Tom Error: A zip file cannot include itself Can you please correct the plugin part so it doesn't use the same folder as input?

    Deploying Spring Boot app to AWS Beanstalk with Nginx customization ·  September 3, 2021

  • Golfman: Reality always wins I've used both Wicket and front-end JS frameworks and, having worked extensively on both, I can tell you that "Speed of development" is definitely NOT with the JS frameworks. You basically end up...

    Five reasons you should use Apache Wicket ·  August 29, 2021

  • Kiriller Sorry can not agree with you, wicket might be a well built technical framework. But the advantages of using a front-end framework like react.js and vue.js can not be beaten by Wicket nowadays. - Speed...

    Five reasons you should use Apache Wicket ·  August 23, 2021

  • Bernd Lauert Sorry but i have to refute your claims with the following arguments: 1. the Wicket community may be small but it is also very responsive, you usually get a helpful answer from the core devs on the...

    Five reasons you should use Apache Wicket ·  July 1, 2021

Archives

  • May 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019

Contact Me

Send Message
Next