1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.apple.jobjc;
  26 
  27 import java.util.concurrent.Callable;
  28 
  29 import com.apple.jobjc.foundation.NSNumber;
  30 import com.apple.jobjc.foundation.NSString;
  31 
  32 public class Utils {
  33     JObjCRuntime runtime;
  34     Utils(JObjCRuntime runtime){ this.runtime = runtime; }
  35 
  36     private static Utils utils;
  37     public static Utils get() {
  38         JObjCRuntime runtime = JObjCRuntime.getInstance(); // enforce security check
  39         return utils != null ? utils : (utils = new Utils(runtime));
  40     }
  41 
  42     private Strings strings_;
  43     public Strings strings() {
  44         return strings_ != null ? strings_ : (strings_ = new Strings(runtime));
  45     }
  46 
  47     private Numbers numbers_;
  48     public Numbers numbers() {
  49         return numbers_ != null ? numbers_ : (numbers_ = new Numbers(runtime));
  50     }
  51 
  52     private Threads threads_;
  53     public Threads threads() {
  54         return threads_ != null ? threads_ : (threads_ = new Threads(runtime));
  55     }
  56 
  57     public static class Strings {
  58         private static native long getNativeNSStringForJavaString(final String javaString);
  59         private static native String getNativeJavaStringForNSString(final long nsString);
  60 
  61         JObjCRuntime runtime;
  62         Strings(JObjCRuntime runtime) { this.runtime = runtime; }
  63 
  64         public NSString nsString(final String str) {
  65             if (str == null) return null;
  66             final long nsString = getNativeNSStringForJavaString(str);
  67             return ID.createNewObjCObjectForClass(NSString.class, nsString, runtime);
  68         }
  69 
  70         public String javaString(final NSString str) {
  71             if (str == null) return null;
  72             return getNativeJavaStringForNSString(((ID)str).ptr);
  73         }
  74 
  75 //        static public CString cStringForJavaString(final String str) {
  76 //            return null;
  77 //        }
  78     }
  79 
  80     public static class Numbers {
  81         private static native long getNativeNSNumberForJavaNumber(final Number num);
  82         private static native Number getNativeJavaNumberForNSNumber(final long num);
  83 
  84         JObjCRuntime runtime;
  85         Numbers(JObjCRuntime runtime) { this.runtime = runtime; }
  86 
  87         public NSNumber nsNumber(final Number num) {
  88             if (num == null) return null;
  89             final long nsNumber = getNativeNSNumberForJavaNumber(num);
  90             return ID.createNewObjCObjectForClass(NSNumber.class, nsNumber, runtime);
  91         }
  92 
  93         public Number javaNumber(final NSNumber num) {
  94             if (num == null) return null;
  95             return getNativeJavaNumberForNSNumber(((ID)num).ptr);
  96         }
  97     }
  98 
  99     public static class Threads {
 100         private static native void performRunnableOnMainThread(final Runnable runnable, final boolean wait);
 101         private static native <V> V performCallableOnMainThread(final Callable<V> callable) throws Exception;
 102 
 103         JObjCRuntime runtime;
 104         Threads(JObjCRuntime runtime) { this.runtime = runtime; }
 105 
 106         /**
 107          * Perform callable on main thread. Exceptions that are thrown on the main thread are ignored.
 108          */
 109         public void performOnMainThread(final Runnable runnable, final boolean wait) {
 110             performRunnableOnMainThread(runnable, wait);
 111         }
 112 
 113         /**
 114          * Perform callable on main thread, block until done, and return the result.
 115          * This also catches any exceptions on the main thread, brings them back and throws them to the caller.
 116          */
 117         public <V> V performOnMainThread(final Callable<V> callable) throws Exception{
 118             return performCallableOnMainThread(callable);
 119         }
 120     }
 121 }