How do I map empty or unknown values to the enum in java. I am using Ormlite.
#DatabaseField(canBeNull=false, columnName=COLUMN_NAME_DEVICE_TYPE,
dataType=DataType.ENUM_STRING)
private DeviceType deviceType;
And here is my enum
public enum DeviceType {
iPhone, iPad, Android,
OSX, Windows, Windows8,
WP7, WP8, Blackberry,
Browser, Facebook, Unknown;
#JsonCreator
public static DeviceType parse(String s) {
for (DeviceType t : values()) {
if (t.name().toLowerCase().equals(s)) {
return t;
}
}
return Unknown;
}
#JsonValue
#Override
public String toString() {
return name().toLowerCase();
}
}
Am always getting the following exception:
Caused by: java.sql.SQLException: Cannot get enum value of '' for field FieldType:name=deviceType,class=MyEntity
Take a look on data in database. I guess that this row has empty string '' for which no enum exists.
Related
I have the following:
public class Stat {
public enum HitType {
MOBILE1(0), MOBILE2(1), DESKTOP(2);
public final int value;
public int value() { return value; }
HitType(int val) {
value = val;
}
public static HitType parseInt(int i) {
switch (i) {
case 0: return MOBILE1;
case 1: return MOBILE2;
case 2: return DESKTOP;
default: return null;
}
}
}
public HitType hitType;
public long sourceId;
public Stat(... int hitType, BigInteger sourceId) {
this.hitType = HitType.parseInt(hitType);
this.sourceId = sourceId.longValueExact();
#Mapper
public interface StatMapper {
#Select("select * from stats where id = #{id}")
#Results(value = {
#Result(property = "hitType", column = "hit_type"),
...
})
public Stat findById(#Param("id") long id);
Stat s = statMapper.findById(1);
response.getOutputStream().print(s.toString());
It still gives this error:
Resolved exception caused by Handler execution: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'hit_type' from result set.
Cause: java.lang.IllegalArgumentException: No enum constant com.company.app.model.Stat.HitType.2
I tried http://stackoverflow.com/questions/5878952/ddg#5878986 and read Convert integer value to matching Java Enum.
If I change the constructor signature to
public Stat(..., int hitType, long sourceId) {
this.sourceId = sourceId;
Then it gives the error
nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.company.app.model.Stat matching [java.math.BigInteger, java.lang.String, java.sql.Timestamp, java.lang.Integer, java.math.BigInteger]
So it seems in the first case it may be setting properties directly, while in the 2nd case, it is using the constructor.
I tried putting HitType hitType in the constructor signature, but it still gives me the No constructor found... error.
MyBatis 3.4.5, MyBatis-Spring 1.3.1, Spring-Boot 1.5.13
I added the getter & setter for that type
public HitType getHitType() {
return hitType;
}
public void setHitType(int hitType) {
this.hitType = HitType.parseInt(hitType);
}
And then it started working. Which is weird because it was complaining about the constructor signature. If it is using the constructor, why would it need the getters and setters?
So I have this enum that doesn't work as I expected and need some modifications:
public enum MyEnum {
CODE000("text description comes here"),
private final String value;
private static final Map<String, MyEnum> LOOKUP = Maps.uniqueIndex(
Arrays.asList(MyEnum.values()),
MyEnum::getValue
);
MyEnum(final String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static MyEnum fromStatus(String status) {
return LOOKUP.get(status);
}
}
The way it works now is:
MyEnum.fromStatus("text description comes here") and of course I want the other way around:
MyEnum.fromStatus("CODE000") to return me "text description comes here"
Can someone suggest how I can change this ?
What you need is a literal lookup, which you get by calling valueOf:
MyEnum code000 = MyEnum.valueOf("CODE000");
And then:
String val = code000.getValue();
Please note that an exception will be raised if the string passed to valueOf doesn't resolve to an enum literal in MyEnum.
Your key function (MyEnum::getValue) is wrong as it returns the value.
It must be MyEnum::name
This will return the enum and not the text description as the value of the map is of type MyEnum. You can get the text value by calling getValue on the enum OR you can store the value in the map instead of the enum
If you want to get an enum value by enum name you can use this function :
public static String fromStatus(String status) {
MyEnum myEnum = valueOf(status);
return myEnum.getValue();
}
The answers so far are using the method valueOf. This method will return the enum constant as long as you provide a name of an enum constant. Otherwise an IllegalArgumentException will be thrown.
In your question you're using a lookup map. The Map (it looks like as it's created by Guava Maps) will return for non-enum-constant-names null. It will not throw a IllegalArgumentException in such cases. So it is a different behaviour.
In addition you say: "and of course I want the other way around"
This means you want to get the enum by status and the status by an enums name.
Therefore you would need to have two lookup methods:
status -> enum
name -> status
But you would get a compile time error if you define the two methods you mentioned:
public static MyEnum fromStatus(String status) { ... }
public static String fromStatus(String name) { ... }
The compiler could not distinguish the methods by name and parameter. But even though you wrote MyEnum.fromStatus("CODE000") actually it's the enum constant name you are using as parameter. So let's resolve the naming conflict by calling the second method fromName. The code for MyEnum could look like this:
public enum MyEnum {
CODE000("text description comes here");
private final String value;
private static final Map<String, MyEnum> LOOKUP_ENUM = Maps.uniqueIndex(Arrays.asList(MyEnum.values()), MyEnum::getValue);
private static final Map<String, String> LOOKUP_STATUS = Arrays.stream(MyEnum.values()).collect(Collectors.toMap(MyEnum::name, MyEnum::getValue));
MyEnum(final String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static MyEnum fromStatus(String status) {
return LOOKUP_ENUM.get(status);
}
public static String fromName(String name) {
return LOOKUP_STATUS.get(name);
}
}
If you want to lookup the enum constants by it's names in the same manner (no exception on non-enum-constant-names), you need a third map and a third lookup method:
private static final Map<String, MyEnum> LOOKUP = Maps.uniqueIndex(Arrays.asList(MyEnum.values()), MyEnum::name);
public static MyEnum byName(String name) {
return LOOKUP.get(name);
}
This would work as follows:
System.out.println(MyEnum.fromStatus("text description comes here")); // CODE000
System.out.println(MyEnum.fromStatus("invalid")); // null - no exception
System.out.println(MyEnum.fromStatus(null)); // null - no exception
System.out.println(MyEnum.fromName("CODE000")); // "text description comes here"
System.out.println(MyEnum.fromName("invalid")); // null - no exception
System.out.println(MyEnum.fromName(null)); // null - no exception
System.out.println(MyEnum.byName("CODE000")); // CODE000
System.out.println(MyEnum.byName("invalid")); // null - no exception
System.out.println(MyEnum.byName(null)); // null - no exception
If you need the byName method I would suggest to rename the methodfromName to something like statusByName to keep them comprehensible apart.
Finally one more suggestion:
Since the lookup methods may return null we could return Optional<String> / Optional<MyEnum> as result. This would allow to immediately continue processing the result.
public static Optional<MyEnum> fromStatus(String status) { ... }
public static Optional<String> statusByName(String name) { ... }
public static Optional<MyEnum> byName(String name) { ... }
I'm trying to have spring-data convert my enum type to and int field in cassandra but I'm getting the following exception:
Unexpected runtime exception java.lang.IllegalArgumentException:
Value 2 of type class com.twc.atg.td.dbo.client.ClassCode does not correspond to any CQL3 type
Here is the piece of code I'm using:
#Enumerated(EnumType.ORDINAL)
#Column("class_code")
public ClassCode classCode;
Since it is not supported by spring-data-cassandra, you can implement this logic in getters/setters.
#Table
public class Writer {
...
public enum WriterType {
POET, DETECTIVE, JOUNALIST
}
...
#Column(value = "writer_type")
private Integer writerType;
...
public WriterType getWriterType() {
return WriterType.values()[writerType];
}
public void setWriterType(WriterType writerType) {
this.writerType = writerType.ordinal();
}
I have an enum in Java I'd like to serialize, so that when I call it from anywhere in the code, I get the lowercase representation of the name.
Let's say I have the following enum:
public enum Status {
DRAFT, PENDING, COMPLETE;
}
println ("Status=" + Status.DRAFT);
I'd like to get the following:
Status=draft
[Note]: I want to use the enum constants in uppercase, and when requesting the value get the lowercase representation.
I am replying this question myself as i found the solution interesting and could not find a reply in the site. Just in case somebody else looks for a way to solve this.
The solution is simple, just override the Enum toString method like this:
public enum Status {
DRAFT, PENDING, COMPLETE;
#Override
public String toString() {
return name().toLowerCase();
}
}
println ("Status=" + Status.DRAFT);
This would output the name in lower case.
Another solution could be:
public enum Status {
DRAFT, PENDING, COMPLETE;
public String nameLowerCase(){
return name().toLowerCase();
}
}
If you want lower case, you could just use lower case, or mixed case, or whatever makes more sense to you.
public enum Status {
draft, pending, complete;
}
println ("Status=" + Status.draft);
prints
Status=draft
You can use the following Enum class which contains constructor with name and ordinal for each enum constant. You can assign values you need for the enum constant.
public enum Status {
DRAFT(0,"draft"), PENDING(1,"pending"), COMPLETE(2,"complete");
private int key;
private String value;
Status(int key, String value){
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#Override
public String toString(){
return this.value;
}
}
Since we override the toString method, the value which is lowercase is returned.
Using
System.out.print("Status = "+Status.DRAFT);
would print,
Status = draft
and
System.out.print("Status = "+Status.DRAFT.name());
would print
Status = DRAFT
I need to use an Enum with a combobox (values shown below).
YES (shown as YES on UI, stored in DB as Y)
NO (shown as NO on UI, stored in DB as N)
DEFAULT (shown as "" on UI, stored in DB as null)
The Enum has methods to perform the following -
toString() - to provide the custom String for UI. (showing the combo options)
OptionToDB (static) - Convert a selected option to db value (on save / update)
DBToOption (static)- Convert a DB value to selcted option (while loading the screen)
static enum EnumOption{
YES,NO,DEFAULT;
....
public static EnumOption DBToOption(String val){
if("Y".equals(val)){
return YES;
} else if("N".equals(val)){
return NO;
}else {
return DEFAULT;
}
}
....
}
It works pretty well, but the issue with above methods is that it uses if/else comparison to deduce which option / db value to be returned.
I thought of storing the dbValue as a field in enum but I was not able to reduce the if/else from DBToOption.
Can this if/else be avoided in any way using a better design??
If you store the dbValue as a field in the enum, you can remove the if/else and replace it with a for-loop, although I don't see anything wrong with those if/elses for this particular case:
static enum EnumOption {
YES("Y"),
NO("N"),
DEFAULT("");
private final String value;
private EnumOption(String value) {
this.value = value;
}
public static EnumOption DBToOption(String val) {
for (EnumOption opt : EnumOption.values()) {
if (opt.value.equals(val)) {
return opt;
}
}
return DEFAULT;
}
}
public enum EnumOption {
YES("Y"), NO("N"), DEFAULT("");
private final String value;
private final static Map<String, EnumOption> options;
static {
options = new HashMap<String, EnumOption>();
for (EnumOption opt : EnumOption.values()) {
options.put(opt.value, opt);
}
}
private EnumOption(String value) {
this.value = value;
}
public static EnumOption DBToOption(String val) {
return options.get(val) != null ? options.get(val) : DEFAULT;
}
}
And here is the test that proves it works.
public void testDBToOption() {
assertEquals(EnumOption.NO, EnumOption.DBToOption("N"));
assertEquals(EnumOption.YES, EnumOption.DBToOption("Y"));
assertEquals(EnumOption.DEFAULT, EnumOption.DBToOption(""));
assertEquals(EnumOption.DEFAULT, EnumOption.DBToOption(null));
assertEquals(EnumOption.DEFAULT, EnumOption.DBToOption("R"));
}
So you want to get rid of the remaining if/else ...Are you doing Object Calisthenics?
You could do the following, if you do not have compatibility issues:
public enum EnumOption {
Y("Y", "YES"),
N("N", "NO"),
D("D", "");
private final String dbValue;
private final String uiValue;
private EnumOption(String dbValue, String uiValue) {
this.dbValue = dbValue;
this.uiValue = uiValue;
}
public String getDbValue() {
return this.dbValue;
}
public String uiValue() {
return this.uiValue;
}
public static EnumOption getFromDb(String dbValue) {
return EnumOption.valueOf(dbValue);
}
}
Since each enum value can only occur once, this has at least the same performance as all the other implementations.
For details about the automatically generated valueOf(String) method in enum types, and James DW's solution, you can read up in Josh Bloch's Effective Java Item 30 (Use enums instead of int constants), page 154.