Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Check an Object to See its Type and Return A Casted Object

I have method to which I pass an object. In this method I check it's type and depending on the type I do something with it and return a Long. I have tried every which way I can think of to do this and I always get several compiler errors telling me it expects a certain object but gets another. Can someone please explain to me what I am doing wrong and guide me in the right direction? What I have tried thus far is below:

  override def getInteger(obj:Object) = {
    if (obj.isInstanceOf[Object]) null
    else if (obj.isInstanceOf[Number]) 
      (obj:Number).longValue()
    else if (obj.isInstanceOf[Boolean]) 
      if (obj:Boolean) 1 else 0
    else if (obj.isInstanceOf[String]) 
      if ((obj:String).length == 0 | (obj:String) == "null") 
        null
      else
          try {
            Long.parse(obj:String)
          } catch {
            case e: Exception => throw new ValueConverterException("value \"" + obj.toString() + "\" of type " + obj.getClass().getName() + " is not convertible to Long")        
          }
  }
like image 851
Russ Bradberry Avatar asked Apr 17 '10 17:04

Russ Bradberry


People also ask

How do you check what type an object is?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.

What is object type casting?

Type Casting is a feature in Java using which the form or type of a variable or object is cast into some other kind or Object, and the process of conversion from one type to another is called Type Casting.

How do you check if an object can be cast Java?

Java provides the instanceof operator to test if an object is of a certain type, or a subclass of that type. The program can then choose to cast or not cast that object accordingly. Object obj = Calendar. getInstance(); long time = 0; if(obj instanceof Calendar) { time = ((Calendar)obj).

Does casting change the static type?

It only changes when you assign a new object. (It never changes for a given object, no object instance can change its class). The cast bridges the two: It checks the runtime type and then allows you to declare that type.


3 Answers

Pattern matching would make it much more nicer.

def getInteger(obj: Any) = obj match {
  case n: Number => n.longValue
  case b: Boolean => if(b) 1 else 0
  case s: String if s.length != 0 && s != "null" => s.toLong
  case _ => null
}
like image 103
missingfaktor Avatar answered Oct 06 '22 02:10

missingfaktor


This code cries out for using a match:

obj match {
  case n: Number => n.longValue
  case b: Boolean => if (b) 1 else 0
  case s: String => if ((s eq null) || s.length == 0) null else {
            // try ... catch ... etc.
          }
  case o: Object => null
}

Followed my own advice from my comment to my original reply...

like image 45
Randall Schulz Avatar answered Oct 06 '22 03:10

Randall Schulz


This might be a start:

def getInteger (o : Any) : Long = o match {
     case (o: Boolean) => if (o) 1 else 0       
     case (l: Long) => l                        
     case (s: String) => java.lang.Long.parseLong (s)   
     case _ => 0L 
}  

(I don't have something to override, and skipped the try/catch for brevity)

like image 45
user unknown Avatar answered Oct 06 '22 03:10

user unknown