You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/19 11:59:27 UTC

cvs commit: jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/util SerializeUtils.java

hchar       2005/01/19 02:59:27

  Added:       auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/util
                        SerializeUtils.java
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/util/SerializeUtils.java
  
  Index: SerializeUtils.java
  ===================================================================
  /*
   * SerializeUtils.java
   *
   * Created on 17 January 2005, 06:52
   */
  
  package net.sf.yajcache.util;
  
  import java.io.Serializable;
  import java.lang.reflect.Array;
  import org.apache.commons.lang.SerializationUtils;
  
  /**
   * @author Hanson Char
   */
  public enum SerializeUtils {
      inst;
      /**
       * Duplicates the given object.
       *
       * @return a duplicate of the given Serializable object,
       * short-cutting the deep clone process if possible.
       */
      public <V extends Serializable> V dup(V obj) {
          Class k = null;
          
          if (obj == null 
          ||  ClassUtils.inst.isImmutable(k=obj.getClass()))
              return obj;
          Class t = k.getComponentType();
          
          if (t != null) {
              // an array.
              if (ClassUtils.inst.isImmutable(t))
              {
                  // array elements are immutable.
                  // short cut via shallow clone.
                  return this.cloneArray(obj);
              }
          }
          // deep clone.
          return (V)SerializationUtils.clone(obj);
      }
      private <A> A cloneArray(A a) {
          int len = Array.getLength(a);
  	Object result = Array.newInstance(a.getClass().getComponentType(), len);
          System.arraycopy(a, 0, result, 0, len);
          return (A)result;
      }
  //    public Class<?> getLeaveComponentType(Class<?> k) {
  //        if (k == null)
  //            return k;
  //        if (k.isArray()) {
  //            return this.getLeaveComponentType(k.getComponentType());
  //        }
  //        return k.getClass();
  //    }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org