GWT覆盖深层复制 [英] GWT Overlay deep copy

查看:181
本文介绍了GWT覆盖深层复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找一个检查GWT覆盖和克隆的函数或库它。它必须能够克隆包含的数组或对象。



谢谢

解决方案

<基于Lineman78的答案并考虑到其他answer 我创建了以下函数:

  public class JsoUtils {

@SuppressWarnings(unchecked)
public static< T extends JavaScriptObject> T deepCopy(T obj)
{
return(T)deepCopyImpl(obj);


private static native JavaScriptObject deepCopyImpl(JavaScriptObject obj)/ * - {
$ b $ if(obj == null)return obj;

var copy;

if(obj instanceof Date){
// Handle Date
copy = new Date();
copy.setTime(obj.getTime());
} else if(obj instanceof Array){
// Handle Array
copy = [];
for(var i = 0,len = obj.length; i< len; i ++){
if(obj [i] == null || typeof obj [i]!=object )copy [i] = obj [i];
else copy [i] = @ com.amindea.noah.client.utils.JsoUtils :: deepCopyImpl(Lcom / google / gwt / core / client / JavaScriptObject;)(obj [i]);
}
} else {
//处理对象
copy = {};
for(var attr in obj){
if(obj.hasOwnProperty(attr)){
if(obj [attr] == null || typeof obj [attr]!=object )copy [attr] = obj [attr];
else copy [attr] = @ com.amindea.noah.client.utils.JsoUtils :: deepCopyImpl(Lcom / google / gwt / core / client / JavaScriptObject;)(obj [attr]);
}
}
}
返回副本;
} - * /;






它支持对象,数组,日期,字符串,数字或布尔值。正如 A.所解释的那样。 Levy ,只要对象和数组中的数据形成树结构,该函数就会工作。


What is the best way to make a deep copy of a gwt overlay type?

I'm looking for a function or library that inspects a GWT overlay and clones it. It must be able to clone contained arrays or objects.

Thanks

解决方案

Based on Lineman78's answer and taking into consideration this other answer from A. Levy I created the following function:

public class JsoUtils {

    @SuppressWarnings("unchecked")
    public static <T extends JavaScriptObject> T deepCopy(T obj)
    {
        return (T) deepCopyImpl(obj);
    }

    private static native JavaScriptObject deepCopyImpl(JavaScriptObject obj) /*-{

        if (obj == null) return obj;

        var copy;        

        if (obj instanceof Date) {
            // Handle Date
            copy = new Date();
            copy.setTime(obj.getTime());
        } else if (obj instanceof Array) {
             // Handle Array
            copy = [];
            for (var i = 0, len = obj.length; i < len; i++) {
                if (obj[i] == null || typeof obj[i] != "object") copy[i] = obj[i];
                else copy[i] = @com.amindea.noah.client.utils.JsoUtils::deepCopyImpl(Lcom/google/gwt/core/client/JavaScriptObject;)(obj[i]);
            }
        } else {
            // Handle Object
            copy = {};
            for (var attr in obj) {
                if (obj.hasOwnProperty(attr)) {
                    if (obj[attr] == null || typeof obj[attr] != "object") copy[attr] = obj[attr];
                    else copy[attr] = @com.amindea.noah.client.utils.JsoUtils::deepCopyImpl(Lcom/google/gwt/core/client/JavaScriptObject;)(obj[attr]);
                }
            }
        }        
        return copy;
    }-*/;

} 

It supports deep copy of Object, Array, Date, String, Number, or Boolean. As explained by A. Levy the function will work as long as the data in the objects and arrays form a tree structure.

这篇关于GWT覆盖深层复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆