Syntax error on token "1", Identifier expected in Enum Java - java-8

I want to implement Enum for Number, I want to get its respective String values. I already followed link: http://www.makeinjava.com/convert-enum-integer-string-value-java/.
The error which I'm getting is
Syntax error on token "1", Identifier expected
Syntax error on token "2", Identifier expected
public enum CompanyCityType {
1("New York"),
2("Reston");
private Integer companyCityType;
CompanyCityType(Integer companyCityType) {
this.companyCityType = companyCityType;
}
public Integer getCompanyAddrType() {
return this.companyCityType;
}
}

You cannot begin any identifier name in Java with a number, it must follow the rules as specified for having a valid variable name in Java.
As per the Oracle variable tutorial:
Variable names are case-sensitive. A variable's name can be any legal
identifier — an unlimited-length sequence of Unicode letters and
digits, beginning with a letter, the dollar sign "$", or the
underscore character "_".
As the fields in an enum are actually public static final fields (singleton instances) or class variables they follow the same set of naming rules as a normal Java variable.
You need to refactor your code to:
public enum CompanyCityType {
NEW_YORK(1),
RESTON(2);
private int companyCityType;
CompanyCityType(int companyCityType) {
this.companyCityType = companyCityType;
}
public int getCompanyAddrType() {
return this.companyCityType;
}
}

Related

Can I Convert a String to an Enum Value in D?

I have the following enum declared which using a string as its underlying value:
enum IssueType : string {
STUDENT_LEAVING = "leaving",
STUDENT_CONFLICT = "conflict",
NEEDS_ARCHIVE = "archive",
OTHER = "other"
}
I want to be able to do the following:
string s = "conflict";
IssueType type = std.conv.to!IssueType(s);
Currently, it is only possible to convert from a string to the enum by providing a case-sensitive match for one of the enum value names, like in this case the string "STUDENT_CONFLICT" would be accepted, while I want "conflict" to be accepted.
Simply cast the string to the enum type.
IssueType type = cast(IssueType)s;

JSF Chaining Converters [duplicate]

I'm using JSF 2.
I have a method that checks for matching values from a list of values:
#ManagedBean(name="webUtilMB")
#ApplicationScoped
public class WebUtilManagedBean implements Serializable{ ...
public static boolean isValueIn(Integer value, Integer ... options){
if(value != null){
for(Integer option: options){
if(option.equals(value)){
return true;
}
}
}
return false;
}
...
}
To call this method in EL I tried:
#{webUtilMB.isValueIn(OtherBean.category.id, 2,3,5)}
But it gave me a:
SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost/127.0.0.1:8080-5) java.lang.IllegalArgumentException: wrong number of arguments
Is there a way to execute such a method from EL?
No, it is not possible to use variable arguments in EL method expressions, let alone EL functions.
Your best bet is to create multiple different named methods with a different amount of fixed arguments.
public static boolean isValueIn2(Integer value, Integer option1, Integer option2) {}
public static boolean isValueIn3(Integer value, Integer option1, Integer option2, Integer option3) {}
public static boolean isValueIn4(Integer value, Integer option1, Integer option2, Integer option3, Integer option4) {}
// ...
As a dubious alternative, you could pass a commaseparated string and split it inside the method
#{webUtilMB.isValueIn(OtherBean.category.id, '2,3,5')}
or even a string array which is created by fn:split() on a commaseparated string
#{webUtilMB.isValueIn(OtherBean.category.id, fn:split('2,3,5', ','))}
but either way, you'd still need to parse them as integer, or to convert the passed-in integer to string.
In case you're already on EL 3.0, you could also use the new EL 3.0 collection syntax without the need for the whole EL function.
#{[2,3,5].contains(OtherBean.category.id)}

How are shared/placed the int of the ProtoMember/ProtoInclude in ProtoBuf?

I've several questions on how/where the ID of a [ProtoContract] should be declared.
Imagine the following code:
[ProtoContract]
[ProtoInclude(100, typeof(SomeClassA))]//1) CAN I USE 1 here?
public abstract class RootClass{
[ProtoMember(1)]
public int NodeId {get;set;}
}
[ProtoContract]
[ProtoInclude(200, typeof(SomeClassC)]//2) Should I declare this here or directly on the RootClass?
//3) Can I use the id 100 here?
//4) Can I use the id 1 here? or member + include share the id?
public class SomeClassA : RootClass{
[ProtoMember(1)]//5) CAN I USE 1 here? Since the parent already use it but it's a different class
public String Name{get;set;}
}
[ProtoContract]
public class SomeClassC : SomeClassA {
[ProtoMember(2)]
public int Count{get;set;}
}
[ProtoContract]
public class SomeClassD : SomeClassA {
[ProtoMember(2)] //6) Can I use 2 here? Since SomeClassC already use it and is a sibling?
public int Count{get;set;}
}
I've put several number with questions:
CAN I USE 1 here?
Should I declare this here or directly on the RootClass?
Can I use the id 100 here?
Can I use the id 1 here? or member + include share the id?
CAN I USE 1 here? Since the parent already use it but it's a different class
Can I use 2 here? Since SomeClassC already use it and is a sibling?
The thing is that we have a huge model with a lot of classes, which all herits from the same object, so I'm trying to figure out to which ID I should take care.
Short version:
the set of field numbers for a type is the union of the numbers defined against members (fields and properties), and the numbers defined for immediate subtypes (includes)
the set of field numbers must be unique within that single type - it is not required to consider base types or derived types
Longer version:
The reason for this is that subtypes are essentially mapped as optional fields:
[ProtoContract]
[ProtoInclude(100, typeof(SomeClassA))]
public abstract class RootClass{
[ProtoMember(1)]
public int NodeId {get;set;}
}
[ProtoContract]
[ProtoInclude(200, typeof(SomeClassC)]
public class SomeClassA : RootClass{
[ProtoMember(1)]
public String Name{get;set;}
}
[ProtoContract]
public class SomeClassC : SomeClassA {
[ProtoMember(2)]
public int Count{get;set;}
}
is, in terms of proto2 syntax:
message RootClass {
optional int32 NodeId = 1;
optional SomeClassA _notNamed = 100;
}
message SomeClassA {
optional string Name = 1;
optional SomeClassC _notNamed = 200;
}
message SomeClassC {
optional int32 Count = 2;
}
Note that at most 1 sub-type field will be used, so it can be considered oneof for the purposes of .proto. Any fields relating to the sub-type will be included in message SomeClassA, so there is no conflict with RootClass and they do not need to be unique. The numbers only need to be unique per message in the .proto sense.
To take the specific questions, then:
no, because that would conflict with NodeId
it should be declared on SomeClassA; protobuf-net is only expecting immediate descendants, and it keeps the numbering consistent and conveniently readable, since the field number is only required to not conflict with the members of SomeClassA
yes you can; there is no conflict
no, because that would conflict with Name
yes you can; there is no conflict
yes you can; there is no conflict - although actually protobuf-net won't even think of SomeClassD as a sibling anyway (it isn't advertised anywhere as an include) - but if there was a [ProtoInclude(201, typeof(SomeClassD))] on SomeClassA, then it would be fine. This would change our .proto to add:
optional SomeClassD _alsoNotNamed = 201;
to message SomeClassA, and add:
message SomeClassD {
optional int32 Count = 2;
}
Note that protobuf-net doesn't actually generate the .proto syntax unless you explicitly ask for it (via GetSchema<T> etc) - I'm including it purely for illustrative purposes in terms of the underlying protobuf concepts.

Deserializing primitive Gson Keys as primitives

For the following code
public class GsonTest
{
public static void main(String[] args)
{
Gson gson = new Gson();
SortedMap<Long, Number> map = new TreeMap<>();
map.put(System.currentTimeMillis(), 200l);
String json2 = gson.toJson(map);
System.out.println(json2);
}
}
I get the following output
{"1484140989691":200}
Is it possible that primitive keys are not deserialized as Strings and that I could have the following output?
{1484140989691:200}
Many thanks
{1484140989691:200}
Is not valid JSON.
Reference
An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string. A single colon comes after each name, separating the name
from the value. A single comma separates a value from a following
name. The names within an object SHOULD be unique.
object = begin-object [ member *( value-separator member ) ]
end-object
member = string name-separator value

Incorrect naming convention for fields in .proto files generated by protobuf-net?

I'm just getting started with Google Protocol Buffers and Marc Gravell's awesome protobuf-net program, and one thing I don't understand is the naming convention for the field declarations in a generated .proto file.
Here's what Google is recommending:
"Use underscore_separated_names for field names – for example, song_name."
https://developers.google.com/protocol-buffers/docs/style
"Note that method names always use camel-case naming, even if the field name in the .proto file uses lower-case with underscores (as it should)."
https://developers.google.com/protocol-buffers/docs/reference/java-generated
"Notice how these accessor methods use camel-case naming, even though the .proto file uses lowercase-with-underscores."
https://developers.google.com/protocol-buffers/docs/javatutorial
But when I use the Serializer.GetProto() method in protobuf-net on this:
[ProtoContract]
public partial class AuthEntry
{
private string _windowsAccount = "";
private string _machineNames = "*";
[ProtoMember(1)]
public string WindowsAccount
{
get { return _windowsAccount; }
set { _windowsAccount = value; }
}
[ProtoMember(2)]
public string MachineNames
{
get { return _machineNames; }
set { _machineNames = value; }
}
}
I get this:
message AuthEntry {
optional string WindowsAccount = 1;
optional string MachineNames = 2;
}
Instead of this, as I'd expected:
message AuthEntry {
optional string windows_account = 1;
optional string machine_names = 2;
}
I'm guessing it's no big deal, but just in case ...
The proto generation doesn't attempt to apply those conventions, because then it gets into the arms race of disambiguation, collisions, etc - no to mention the fun of finding word breaks in arbitrary names like CustomerIDReference (ok, that's an unlikely example, but you get the point). If you want to control that yourself - specify the Name property on either ProtoContractAttribute or ProtoMemberAttribute.

Resources