view core/src/luan/DeepCloner.java @ 216:a1b142f9c5c0

make Utils.toUrl() check for "index.html" or "index.html.luan" in dirs for jars (hack) git-svn-id: https://luan-java.googlecode.com/svn/trunk@217 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 14 Jul 2014 06:11:55 +0000
parents 24ede40ee0aa
children c6bcb8859b93
line wrap: on
line source

package luan;

import java.util.Map;
import java.util.IdentityHashMap;


public final class DeepCloner {
	private final Map<Object,Object> cloned = new IdentityHashMap<Object,Object>();

	public <T extends DeepCloneable<T>> T deepClone(T obj) {
		if( obj==null )
			return null;
		@SuppressWarnings("unchecked")
		T rtn = (T)cloned.get(obj);
		if( rtn == null ) {
			rtn = obj.shallowClone();
			cloned.put(obj,rtn);
			obj.deepenClone(rtn,this);
		}
		return rtn;
	}

	public <T> T[] deepClone(T[] obj) {
		if( obj.length == 0 )
			return obj;
		@SuppressWarnings("unchecked")
		T[] rtn = (T[])cloned.get(obj);
		if( rtn == null ) {
			rtn = obj.clone();
			cloned.put(obj,rtn);
			for( int i=0; i<rtn.length; i++ ) {
				@SuppressWarnings("unchecked")
				T t = get(rtn[i]);
				rtn[i] = t;
			}
		}
		return rtn;
	}

	public <T> T get(T obj) {
		if( !(obj instanceof DeepCloneable) )
			return obj;
		@SuppressWarnings("unchecked")
		T dc = (T)deepClone((DeepCloneable)obj);
		return dc;
	}
}