ArrayUtil.java org::apache::iceberg::util::ArrayUtil org::apache::iceberg::util /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.util; importjava.lang.reflect.Array; importjava.util.Collections; importjava.util.List; importjava.util.stream.Collectors; importjava.util.stream.IntStream; importjava.util.stream.LongStream; importorg.apache.iceberg.relocated.com.google.common.primitives.Longs; publicclassArrayUtil{ privateArrayUtil(){} publicstaticfinalboolean[]EMPTY_BOOLEAN_ARRAY=newboolean[0]; publicstaticfinalbyte[]EMPTY_BYTE_ARRAY=newbyte[0]; publicstaticfinalshort[]EMPTY_SHORT_ARRAY=newshort[0]; publicstaticfinalint[]EMPTY_INT_ARRAY=newint[0]; publicstaticfinallong[]EMPTY_LONG_ARRAY=newlong[0]; publicstaticfinalfloat[]EMPTY_FLOAT_ARRAY=newfloat[0]; publicstaticfinaldouble[]EMPTY_DOUBLE_ARRAY=newdouble[0]; publicstaticList<Integer>toIntList(int[]ints){ if(ints!=null){ returnIntStream.of(ints).boxed().collect(Collectors.toList()); }else{ returnnull; } } publicstaticint[]toIntArray(List<Integer>ints){ if(ints!=null){ returnints.stream().mapToInt(v->v).toArray(); }else{ returnnull; } } publicstaticList<Long>toLongList(long[]longs){ if(longs!=null){ returnLongStream.of(longs).boxed().collect(Collectors.toList()); }else{ returnnull; } } publicstaticList<Long>toUnmodifiableLongList(long[]longs){ if(longs!=null){ returnCollections.unmodifiableList(Longs.asList(longs)); }else{ returnnull; } } publicstaticlong[]toLongArray(List<Long>longs){ if(longs!=null){ returnlongs.stream().mapToLong(v->v).toArray(); }else{ returnnull; } } publicstaticboolean[]toPrimitive(finalBoolean[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_BOOLEAN_ARRAY; } finalboolean[]result=newboolean[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].booleanValue(); } returnresult; } publicstaticbyte[]toPrimitive(finalByte[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_BYTE_ARRAY; } finalbyte[]result=newbyte[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].byteValue(); } returnresult; } publicstaticshort[]toPrimitive(finalShort[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_SHORT_ARRAY; } finalshort[]result=newshort[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].shortValue(); } returnresult; } publicstaticint[]toPrimitive(finalInteger[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_INT_ARRAY; } finalint[]result=newint[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].intValue(); } returnresult; } publicstaticlong[]toPrimitive(finalLong[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_LONG_ARRAY; } finallong[]result=newlong[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].longValue(); } returnresult; } publicstaticfloat[]toPrimitive(finalFloat[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_FLOAT_ARRAY; } finalfloat[]result=newfloat[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].floatValue(); } returnresult; } publicstaticdouble[]toPrimitive(finalDouble[]array){ if(array==null){ returnnull; }elseif(array.length==0){ returnEMPTY_DOUBLE_ARRAY; } finaldouble[]result=newdouble[array.length]; for(inti=0;i<array.length;i++){ result[i]=array[i].doubleValue(); } returnresult; } publicstatic<T>T[]add(finalT[]array,finalTelement){ Class<?>type; if(array!=null){ type=array.getClass().getComponentType(); }elseif(element!=null){ type=element.getClass(); }else{ thrownewIllegalArgumentException("Argumentscannotbothbenull"); } @SuppressWarnings("unchecked")//typemustbeT finalT[]newArray=(T[])copyArrayGrow1(array,type); newArray[newArray.length-1]=element; returnnewArray; } privatestaticObjectcopyArrayGrow1(finalObjectarray,finalClass<?>newArrayComponentType){ if(array!=null){ finalintarrayLength=Array.getLength(array); finalObjectnewArray= Array.newInstance(array.getClass().getComponentType(),arrayLength+1); System.arraycopy(array,0,newArray,0,arrayLength); returnnewArray; } returnArray.newInstance(newArrayComponentType,1); } publicstaticbooleanisStrictlyAscending(long[]array){ for(intindex=1;index<array.length;index++){ if(array[index]<=array[index-1]){ returnfalse; } } returntrue; } @SuppressWarnings("unchecked") publicstatic<T>T[]concat(Class<T>type,T[]...arrays){ T[]result=(T[])Array.newInstance(type,totalLength(arrays)); intcurrentLength=0; for(T[]array:arrays){ intlength=array.length; if(length>0){ System.arraycopy(array,0,result,currentLength,length); currentLength+=length; } } returnresult; } privatestaticinttotalLength(Object[][]arrays){ inttotalLength=0; for(Object[]array:arrays){ totalLength+=array.length; } returntotalLength; } }