-1

I'm facing a problem with nested enum. So, I have an nested enum which has default values as you can see. Before there were just an enums like CDs and CMs. Now I set something like definition to every of it as you can see "Cool Ds" and etc. Currently I'm facing a problem that I can't read enums String, which is in () and I don't know how to fix it anymore. Does anyone have an idea?

 package com.test.beans;

  import java.io.Serializable;
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@JsonIgnoreProperties(ignoreUnknown = true)
public class RecordBean implements Serializable {
public enum Types {
    CDs("Cool Ds"), CMs("Cool Ms");

   private final  String s;
   private Types(String s) {
        this.s=s;
    }

    public String getTypes(){
       return s;
    }
    public static Types fromNumeric(int index) {
        switch (index) {
            default:
                return null;
            case 0:
                return Types.CDs;
            case 1:
                return Types.CMs;

        }
    }

}


private Types type;

private float value;

public RecordBean() {
    // default constructor for default instantiate
}

public RecordBean(Types type, float value) {
    this.type = type;
    this.value = value;
}

public Types getType() {
    return type;
}

public void setType(Types type) {
    this.type = type;
}

public float getValue() {
    return value;
}

public void setValue(float value) {
    this.value = value;
}


}

UPDATE

Error what I'm getting:

    17/04/10 12:44:53 ERROR App$: Can not construct instance of com.test.beans.RecordBean$Types from String value 'Cool Ds': value not one of declared Enum instance names:  CDs, CMs ]

So as you can see he is not comparing my String 'Cool Ds' with enums String in brackets but with pure enum or CDs and CMs

My USE-CASE is like this. I'm working spark streaming where data are incoming to my RecordBean class and where are comparing to my enum type. Because in database are changed types from CMs to Cool Ms I needed to change the same in my app by adding definition to Enum. Afterwards I couldn't accomplish part where app will process enum like CMs and read its definition or Cool Ms

iMajna
  • 489
  • 4
  • 28
  • Unfortunately I have *no* idea what exactly you are asking ... – GhostCat Apr 10 '17 at 12:19
  • I update it and set **ERROR** maybe now it is clear? :) – iMajna Apr 10 '17 at 12:21
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Your updates help, but still just half of the things we might need. – GhostCat Apr 10 '17 at 12:23
  • Update no2, very well? – iMajna Apr 10 '17 at 12:27
  • 1
    Possible duplicate of [Jackson: Serialize and deserialize enum values as integers](http://stackoverflow.com/questions/37833557/jackson-serialize-and-deserialize-enum-values-as-integers) – Tom Apr 10 '17 at 12:38
  • The dupe uses `int` but that's not different to your Strings. – Tom Apr 10 '17 at 12:39
  • Side note: the constructor `public RecordBean()` is not a default constructor, so its comment is wrong. The default constructor is the one provided by the compiler when the source doesn't provide one. The `RecordBean` provides two, therefore neither is a default constructor. – Lew Bloch Apr 10 '17 at 17:24
  • You don't show the code that tries to find the enum constant from a string. Show that code to get advice. – Lew Bloch Apr 10 '17 at 17:25

2 Answers2

0

Be sure, you can read with : CDs.getTypes() and CMs.getTypes()

  • Noup, I'm getting `incompatible types: java.lang.String cannot be converted to com.test.beans.RecordBean.Types` – iMajna Apr 10 '17 at 12:32
0

Guessing: maybe the framework is calling toString() on your enum constants, thus you might want to add:

public enum Types {
 ...
 @Overrride 
 public String toString() { return s; }

In other words: make sure that your existing enum constants are really using that "new" changed string name.

But beyond that: consider stepping back and changing your overall design. Your problem isn't uncommon - enums are nice from a programming point of view (as in: using enum constants leads to cleaner code); but: they are not suited for persistence. Versioning with enums is hell.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Yeah, I'm aware what you said about versioning but for now I just need prototype and afterwards I'll redesign whole app. Also, I tried your guess but the **ERROR** is still here – iMajna Apr 10 '17 at 12:41
  • I guess you want to add a bunch of trace statements - like in each method of your enum ... just to see what is called; if there is anything that you could do easily. I am thinking of adding additional enum constants; probably using the **same** strings as CD and CM had originally. – GhostCat Apr 10 '17 at 12:43
  • Hmm, that's my original idea, I just want to return for example: "Cool Ds" but within enum otherwise I'll need to change lots of stuff which I don't need by now. I'll try with JSONValue annotation If you think of any easily step to resolve this say :) – iMajna Apr 10 '17 at 12:52
  • Be assured; if I had better ideas; I would have told you about already ;-( – GhostCat Apr 10 '17 at 13:00