Extenable enum class


What is the best way to model one extendable enum, no code been extended just data.
The idea is to have one base enum with a set of possible discrete values and extend this enum in another place with new additional values.
What I am looking to model is kind of of a protocol of basic operations that are expected to be possible and be able to provide additional discover-able set of operations.

Thank you, kindly
Luis Oscar

1 Like

It depends on what you want and at what semantic level. At base level you use an interface and have the enum implement this interface (and any other enum can do the same). You cannot close this though and loose some of the advantages of having an enum in the first place.

It depends really what you want. There are different goals:

  • Ensure uniqueness (no duplicate items)
  • The ability to enumerate operations (just put them in a set)
  • Fast switching on values

What you probably want is some sort of protocol manager object that is responsible for providing operation objects. It could be possible to register new objects in some way, managed by the manager class. Operation objects can be an inner class of the manager and have private constructors such that only the manager can manage them. The manager can even maintain sequence numbers for efficient when/switch statements. You will basically have to write your own almost-enum class that doesn’t come with the restrictions of enum. You will have to take care of the initialisation issues manually though.

1 Like

Hi @pdvrieze:

This is what I end up making:

import org.apache.camel.builder.RouteBuilder
interface Operation {
    val name: String
    val description: String
data class DefaultOperation(override val name: String, override val description: String) : Operation
    object Availability : Operation by DefaultOperation("Availability", "Retrieve availabilities")
    object Booking : Operation by DefaultOperation("Booking", "Make a booking")
    object BookingInfo : Operation by DefaultOperation("BookingInfo", "Retrieve booking info")
    object Cancel : Operation by DefaultOperation("Cancel", "Cancel a booking")
    object Locations : Operation by DefaultOperation("Locations", "Retrieve list of locations")
abstract class OperationsRouteBuilder(val operations: Map<Operation, String>) : RouteBuilder()
1 Like

I saw an other way to make extensive enum by combining enum and interfaces on this article : Extensible Enum With Interface - DZone Java

1 Like

Thank you. for the reference

Hey I have similiar question, so I think I may join it to this discussion.

My problems related error handling in View. My business logic deliver to View generic object “Response” where in Error I have enum RemoteError. It would be fine just for remote but I want also in return there input validation Error.

So if I would send and email. My logic validates at first if email is correct, then if not it should return Response.Error containing EMAIL_INVALID enum. Ideally would be could extend basis EnumRemoteError in order to achieve that.

sealed class Response{
data class Success(…)
data class Error(val errorType: EnumRemoteError)

enum EnumRemoteError{

enum EnumEmail: EnumRemoteError{

// Error Handling in View (Activity/Fragment)

class View{
val response = BusinnesLogic.getResponse()
if(response is Response.Error){
val errorType = response.errorType as EnumEmail