Just An Application

November 13, 2016

Java To Swift: Thirty Three And A Third Things You Might Like To Know

Filed under: Java, Java2Swift, Swift — Tags: , , — Simon Lewis @ 8:26 pm

What ?

Its not my fault. Its an epidemic. Everybody’s doing it.

Consider yourselves lucky I didn’t go with

Java To Swift: You’ll Be Astonished By What Happens Next !

Anyway Java has classes and Swift has classes, so transforming Java source into Swift source should be trivial.

Probably an afternoon’s work, if that.

Well, maybe, but probably not.

Here is the result of running my first attempt at a tool for turning Java into Swift on Boolean.java.

import Foundation


public final class java_lang_Boolean: java_lang_Object
{
    public init(_ value:JavaBoolean)
    {
        self.value = value
        
    }
    
    public convenience init(_ s:java_lang_String?)
    {
        self.init(java_lang_Boolean.parseBoolean(s))
        
    }
    
    public static func parseBoolean(s:java_lang_String?) -> JavaBoolean
    {
        return s != nil && s!.equalsIgnoreCase(javaStringConstant("true"))
    }
    
    public func booleanValue() -> JavaBoolean
    {
        return value
    }
    
    public static func valueOf(b:JavaBoolean) -> java_lang_Boolean
    {
        return b ? TRUE : FALSE
    }
    
    public static func valueOf(s:java_lang_String?) -> java_lang_Boolean
    {
        return parseBoolean(s) ? TRUE : FALSE
    }
    
    public static func toString(b:JavaBoolean) -> java_lang_String
    {
        return b ? javaStringConstant("true") : javaStringConstant("false")
    }
    
    public func toString() -> java_lang_String
    {
        return value ? javaStringConstant("true") : javaStringConstant("false")
    }
    
    public func hashCode() -> JavaInt
    {
        return java_lang_Boolean.hashCode(value)
    }
    
    public static func hashCode(value:JavaBoolean) -> JavaInt
    {
        return value ? JavaInt(1231) : JavaInt(1237)
    }
    
    public func equals(obj:java_lang_Object?) -> JavaBoolean
    {
        if obj is java_lang_Boolean
        {
            return value == (obj as! java_lang_Boolean).booleanValue()
        }
        return false
    }
    
    public static func getBoolean(name:java_lang_String?) -> JavaBoolean
    {
        var result : JavaBoolean = false
        do
        {
            result = parseBoolean(java_lang_System.getProperty(name))
        }
        catch let e where e is java_lang_IllegalArgumentException || e is java_lang_NullPointerException
        {
        }
        return result
    }
    
    public func compareTo(b:java_lang_Boolean?) -> JavaInt
    {
        return java_lang_Boolean.compare(self.value, b!.value)
    }
    
    public static func compare(x:JavaBoolean, _ y:JavaBoolean) -> JavaInt
    {
        return x == y ? JavaInt(0) : x ? JavaInt(1) : JavaInt(-1)
    }
    
    public static func logicalAnd(a:JavaBoolean, _ b:JavaBoolean) -> JavaBoolean
    {
        return a && b
    }
    
    public static func logicalOr(a:JavaBoolean, _ b:JavaBoolean) -> JavaBoolean
    {
        return a || b
    }
    
    public static func logicalXor(a:JavaBoolean, _ b:JavaBoolean) -> JavaBoolean
    {
        return a ^ b
    }
    
    public static let TRUE : java_lang_Boolean = java_lang_Boolean( true)
    
    public static let FALSE : java_lang_Boolean = java_lang_Boolean(false)
    
    private let value : JavaBoolean
    
    private static let serialVersionUID : JavaLong = JavaLong(-3665804199014368530)
    
}

Pretty convincing don’t you think ?

It even compiles with a bit of judicious pushing and shoving.

But looks can be deceptive.

The class java.lang.Boolean isn’t a particularly Java’ry class as Java classes go so it is not really a very good test.

There are a number of ways in which Java and Swift are not at all alike, so it looks as though it is probably going to be more like two afternoon’s work.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: