Showing posts with label enums. Show all posts
Showing posts with label enums. Show all posts

Thursday, 13 July 2017

Enumerations in EclipseLink

I have a field in the database that does not match with an Enumeration.

So I needed to do a little conversion in EclipseLink, and I didn't know how.

Below is the answer on that one.

References

EclipseLink - @ObjectTypeConverter
https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/ObjectTypeConverter
EclipseLink - EclipseLink/Examples/JPA/EnumToCode
https://wiki.eclipse.org/EclipseLink/Examples/JPA/EnumToCode

Thursday, 17 December 2015

Trailing Commas

This post might seem trivial, but I decided to go ahead with it.

I recently found out that the following code compiles.
/**
 * The Seasons.
 * @author mr.bear
 */

public enum Seasons
{
    SPRING, SUMMER, AUTUMN, WINTER,
}
I was a little surprised, that the last comma was not a problem. The same can be seen in the following array initializer:
int[] b = {1, 2, 3, 4, 5, 6,};

Apparently, to my surprise, it's written in the JLS.

According to [1], if you look at the Grammar defined for Enums:
EnumBody:
{ [EnumConstantList] [,] [EnumBodyDeclarations] }
For arrays the same rule applies2.
“A trailing comma may appear after the last expression in an array initializer and is ignored.”
I was a little surprised that it has been part of the spec for a very long time3.

References

[1] JLS 8.0 Section 8.9.1 Enum Constants
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.1
[2] JLS 8.0 Section 10.6
https://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.6
[3] JSL 1.0
http://titanium.cs.berkeley.edu/doc/java-langspec-1.0/
StackOverflow - Arrays with trailing commas inside an array initializer in java
http://stackoverflow.com/questions/11621917/arrays-with-trailing-commas-inside-an-array-initializer-in-java

Monday, 29 December 2014

My Enum Frustration

At work, there is a bit of Legacy around.

The following example of several Enum classes we have, is what is currently frustrating me (and I try actively to change them).

Let's enumerate (oh, look, I made a joke) the issues:
Replacing magic numbers with constants and enums is, of course, one of the Good Thingstm.

But in this example, they seem to have totally missed the point.

Not only have they replaced magic numbers with "magic enums", it's an eyesore to use the underscore in the Enum instances.

When you have to take refuge in the use of underscores to make things work, it is a sure sign that you are doing something seriously wrong.