jlp
jlp

Reputation: 1706

How to check if a scala Option type is None in Java code

I have Scala class that will generate an Option[StructType] value which will be used in a Java function. In that java function, I need to check if this Option[StructType] is Scala None or not. How do I do that?

Scala class:

class Person(columns : String) {
    val recordStruct : Option[StructType] = {
        if ( columns != null && !columns.isEmpty()) {
          Some(new StructType(fields.map(field => 
                              StructField(field, StringType, true)).toArray))
        } else {
            None
        }
    }   
}

Java function:

StructType structure =  person.recordStruct().get();

// how to check if structure is None (in scala) ????

if (structure is None) {
    // ...
}

Upvotes: 1

Views: 5571

Answers (1)

Tzach Zohar
Tzach Zohar

Reputation: 37822

Option<StructType> maybeStructure = person.recordStruct();
if (maybeStructure.isEmpty()) { 
    // do something if None
} else {
    StructType structure =  person.recordStruct().get();
    // now you can use structure...
}

Upvotes: 6

Related Questions