现在的位置: 首页 > 综合 > 正文

源码解读心得 - net.sf.hibernate.util.ArrayHelper

2011年01月03日 ⁄ 综合 ⁄ 共 4366字 ⁄ 字号 评论关闭

 

//$Id: ArrayHelper.java,v 1.6.2.5 2003/12/13 12:25:12 oneovthafew Exp $
package net.sf.hibernate.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.lang.reflect.Array;

import net.sf.hibernate.type.Type;
/**
* 此类封装了Array的一些相关操作
*/

public final class ArrayHelper {
    

    
// 转换数组为字符串数组
    public static String[] toStringArray(Object[] objects) {
        
int length=objects.length;
        String[] result 
= new String[length];
        
for (int i=0; i<length; i++{
            result[i] 
= objects[i].toString();
        }

        
return result;
    }

    

    
// 使用str来填充数组中的每一个元素
    
// 注意,此处只是填充str的引用,并不是复制。
    public static String[] fillArray(String str, int length) {
        String[] result 
= new String[length];
        Arrays.fill(result, str);
        
return result;
    }

    

    
// 转换集合为字符串数组
    public static String[] toStringArray(Collection coll) {
        
return toStringArray( coll.toArray() );
    }

    

    
// 转换集合为int数组
    public static int[] toIntArray(Collection coll) {
        Iterator iter 
= coll.iterator();
        
int[] arr = new int[ coll.size() ];
        
int i=0;
        
while( iter.hasNext() ) {
            arr[i
++= ( (Integer) iter.next() ).intValue();
        }

        
return arr;
    }

    

    
// 将array复制到to,此处与fillArray有区别
    public static Object[] typecast(Object[] array, Object[] to) {
        
return java.util.Arrays.asList(array).toArray(to);
    }

    
    
//Arrays.asList doesn't do primitive arrays
    
// 将数组对象转换成List
    public static List toList(Object array) {
        
if ( array instanceof Object[] ) return Arrays.asList( (Object[]) array ); //faster?
        int size = Array.getLength(array);
        ArrayList list 
= new ArrayList(size);
        
for (int i=0; i<size; i++{
            list.add( Array.
get(array, i) );
        }

        
return list;
    }

    

    
// 取字符串数组的其中一部分
    public static String[] slice(String[] strings, int begin, int length) {
        String[] result 
= new String[length];
        
for ( int i=0; i<length; i++ ) {
            result[i] 
= strings[begin+i];
        }

        
return result;
    }

    


    
// 取数组中的一部分
    public static Object[] slice(Object[] objects, int begin, int length) {
        Object[] result 
= new Object[length];
        
for ( int i=0; i<length; i++ ) {
            result[i] 
= objects[begin+i];
        }

        
return result;
    }

    

    
// 从Iterator转换到List
    public static List toList(Iterator iter) {
        List list 
= new ArrayList();
        
while ( iter.hasNext() ) {
            list.add( iter.next() );
        }

        
return list;
    }

    
    
// 合并两个数组
    public static String[] join(String[] x, String[] y) {
        String[] result 
= new String[ x.length + y.length ];
        
for ( int i=0; i<x.length; i++ ) result[i] = x[i];
        
for ( int i=0; i<y.length; i++ ) result[i+x.length] = y[i];
        
return result;        
    }

    

    
// 合并两个数组
    public static int[] join(int[] x, int[] y) {
        
int[] result = new int[ x.length + y.length ];
        
for ( int i=0; i<x.length; i++ ) result[i] = x[i];
        
for ( int i=0; i<y.length; i++ ) result[i+x.length] = y[i];
        
return result;        
    }


    
private ArrayHelper() {}
    

    
// 转换数组为字符串,其格式为:[os1,os2,]
    public static String asString(Object os[]) {
        StringBuffer sb 
= new StringBuffer();
        sb.append(
"[");
        
for (int i = 0; i < os.length; i++{
            sb.append(os[i]);
            
if(i<os.length-1) sb.append(",");            
        }

        sb.append(
"]");
        
return sb.toString();
    }

    

    
// 判断数组中的数是否全是负数
    public static boolean isAllNegative(int[] array) {
        
for ( int i=0; i<array.length; i++ ) {
            
if ( array[i] >=0 ) return false;
        }

        
return true;
    }

    

    
// 将array中的全部对象都添加到collection中
    public static void addAll(Collection collection, Object[] array) {
        
for ( int i=0; i<array.length; i++ ) {
            collection.add( array[i] );
        }

    }

    

    
/**
     *下面是一些常量
     
*/

    
public static final String[] EMPTY_STRING_ARRAY = {};        // 空字符串数组
    public static final Class[] EMPTY_CLASS_ARRAY = {};            // 空Class数组
    public static final Object[] EMPTY_OBJECT_ARRAY = {};        // 空对象数组
    public static final Type[] EMPTY_TYPE_ARRAY = {};            // 空Type数组
}

心得:
1、关注fillArray与typecast的区别
2、public static List toList(Object array)有古怪,实现的方法有点重复

抱歉!评论已关闭.