Should I use uppercase or camelcase in label enums?
Question
The title says it all.
In cypher, camelcase is used for labels. For instance :Car
and :Bike
. The API docs for Label
uses uppercase when defining each label in the enum, so then Label.CAR.name()
will always be CAR
, not Car
.
Using camelcase fits in more nicely with cypher queries, but this conflicts with the Java standard for naming enum members. This forces me to use labels like Label.BIKE_SEAT
instead of Label.BikeSeat
in my graph database. Which one should I use?
Even though using uppercase for enum members is the correct way to do it in Java, I'm still a bit curious about why the Neo4j people then decided to represent labels as an enums?
Solution 2
First approach to a solution
To comply with the Java convention, I can override Enum#toString()
and use that instead of Enum#name()
which cannot be overridden. Then each member will have a constructor with its real Neo4j label as represented in the graph database:
public enum Label implements org.neo4j.graphdb.Label {
CAR("Car"),
BIKE("Bike"),
BUS("Bus");
private String name;
private Label(name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
This doesn't work well with methods such as GraphDatabaseService#findNodesByLabelAndProperty(Label, String, String)
because it seems to be using Label#name()
for matching instead of Label#toString()
so it won't match nodes having camel case labels in the db.
My working solution
Since overriding Enum#toString()
doesn't work and since Neo4j uses Label#name()
for everything internally, I'll need to override it anyway. So I ended up defining a class implementing Label
which is able to override Label#name()
:
public class Label implements org.neo4j.graphdb.Label {
public static final Label CAR = new Label("Car");
public static final Label BIKE = new Label("Bike");
public static final Label BUS = new Label("Bus");
private String name;
private Label(String name) {
this.name = name;
}
@Override
public String name() {
return name;
}
@Override
public String toString() {
return name;
}
}
Externally, no changes are needed on the client side using the previous label enum.
OTHER TIPS
ORACLE DOC : Because they are constants, the names of an enum type's fields are in uppercase letters.
check : http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html