001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.client.js; 021 022import com.google.gwt.core.client.JavaScriptObject; 023 024import java.util.List; 025 026/** 027 * @author Ben Dol 028 */ 029public class Js { 030 031 @SuppressWarnings("unchecked") 032 public static <T> T createJsObject() { 033 return (T) JavaScriptObject.createObject(); 034 } 035 036 @SuppressWarnings("unchecked") 037 public static <T> T createJsArray() { 038 return (T) JavaScriptObject.createArray(); 039 } 040 041 /** 042 * Box a native JS array in a Java List. It does not have any performance 043 * penalty because we directly change the native array of super ArrayList 044 * implementation. 045 */ 046 public static native <T> List<T> asList(JavaScriptObject o) /*-{ 047 var l = @java.util.ArrayList::new()(); 048 l.@java.util.ArrayList::array = o; 049 return l; 050 }-*/; 051 052 public static native boolean isPrimitiveType(Object dataItem) /*-{ 053 return Object(dataItem) !== dataItem; 054 }-*/; 055 056 public static native boolean isUndefinedOrNull(Object o) /*-{ 057 return o === undefined || o === null; 058 }-*/; 059 060 public static native boolean isObject(Object o) /*-{ 061 return typeof o === "object" && o !== null; 062 }-*/; 063 064 public static native JavaScriptObject getError(String msg) /*-{ 065 return new Error(msg || ''); 066 }-*/; 067 068 public static native JavaScriptObject getUndefined() /*-{ 069 return undefined; 070 }-*/; 071 072 public static native boolean isTrue(Object o) /*-{ 073 return o; 074 }-*/; 075}