I have the following code:
object Order extends Enumeration("asc", "desc") {
type OrderType = Value
val asc, desc = Value
}
And i use it:
val someStr:String = "someStr"
val order = Order.withName(someStr)
This gives me the enum of the input string, but if i send string "asc1" i get Exception:
NoSuchElementException: None.get (ProductRequest.scala
My question is - Can i iterate the values and check if the strings exists? This way i can throw better detailed exception..
I was thinking i can iterate Order.values -> but could not find something useful
Thanks
EnumUtils class of Apache Commons Lang. The EnumUtils class has a method called isValidEnum whichChecks if the specified name is a valid enum for the class. It returns a boolean true if the String is contained in the Enum class, and a boolean false otherwise.
In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false.
No they cannot. They are limited to numeric values of the underlying enum type.
Your could define your Enumeration as:
object Order extends Enumeration {
type OrderType = Value
val asc = Value("asc")
val desc = Value("desc")
def isOrderType(s: String) = values.exists(_.toString == s)
}
And use it:
Order.isOrderType("asc") //> res0: Boolean = true
Order.isOrderType("foo") //> res1: Boolean = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With