java arrayutils_GrowingArrayUtils.java

/*

* Copyright (C) 2014 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.android.internal.util;

/**

* A helper class that aims to provide comparable growth performance to ArrayList, but on primitive

* arrays. Common array operations are implemented for efficient use in dynamic containers.

*

* All methods in this class assume that the length of an array is equivalent to its capacity and

* NOT the number of elements in the array. The current size of the array is always passed in as a

* parameter.

*

* @hide

*/

public final class GrowingArrayUtils {

/**

* Appends an element to the end of the array, growing the array if there is no more room.

* @param array The array to which to append the element. This must NOT be null.

* @param currentSize The number of elements in the array. Must be less than or equal to

* array.length.

* @param element The element to append.

* @return the array to which the element was appended. This may be different than the given

* array.

*/

public static T[] append(T[] array, int currentSize, T element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

@SuppressWarnings("unchecked")

T[] newArray = ArrayUtils.newUnpaddedArray(

(Class) array.getClass().getComponentType(), growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive int version of {@link #append(Object[], int, Object)}.

*/

public static int[] append(int[] array, int currentSize, int element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

int[] newArray = ArrayUtils.newUnpaddedIntArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive long version of {@link #append(Object[], int, Object)}.

*/

public static long[] append(long[] array, int currentSize, long element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

long[] newArray = ArrayUtils.newUnpaddedLongArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive boolean version of {@link #append(Object[], int, Object)}.

*/

public static boolean[] append(boolean[] array, int currentSize, boolean element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

boolean[] newArray = ArrayUtils.newUnpaddedBooleanArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive float version of {@link #append(Object[], int, Object)}.

*/

public static float[] append(float[] array, int currentSize, float element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

float[] newArray = ArrayUtils.newUnpaddedFloatArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Inserts an element into the array at the specified index, growing the array if there is no

* more room.

*

* @param array The array to which to append the element. Must NOT be null.

* @param currentSize The number of elements in the array. Must be less than or equal to

* array.length.

* @param element The element to insert.

* @return the array to which the element was appended. This may be different than the given

* array.

*/

public static T[] insert(T[] array, int currentSize, int index, T element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

@SuppressWarnings("unchecked")

T[] newArray = ArrayUtils.newUnpaddedArray((Class)array.getClass().getComponentType(),

growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Primitive int version of {@link #insert(Object[], int, int, Object)}.

*/

public static int[] insert(int[] array, int currentSize, int index, int element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

int[] newArray = ArrayUtils.newUnpaddedIntArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Primitive long version of {@link #insert(Object[], int, int, Object)}.

*/

public static long[] insert(long[] array, int currentSize, int index, long element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

long[] newArray = ArrayUtils.newUnpaddedLongArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Primitive boolean version of {@link #insert(Object[], int, int, Object)}.

*/

public static boolean[] insert(boolean[] array, int currentSize, int index, boolean element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

boolean[] newArray = ArrayUtils.newUnpaddedBooleanArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Given the current size of an array, returns an ideal size to which the array should grow.

* This is typically double the given size, but should not be relied upon to do so in the

* future.

*/

public static int growSize(int currentSize) {

return currentSize <= 4 ? 8 : currentSize * 2;

}

// Uninstantiable

private GrowingArrayUtils() {}

}

Java程序

|

212行

|

7.84 KB

/*

* Copyright (C) 2014 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.android.internal.util;

/**

* A helper class that aims to provide comparable growth performance to ArrayList, but on primitive

* arrays. Common array operations are implemented for efficient use in dynamic containers.

*

* All methods in this class assume that the length of an array is equivalent to its capacity and

* NOT the number of elements in the array. The current size of the array is always passed in as a

* parameter.

*

* @hide

*/

public final class GrowingArrayUtils {

/**

* Appends an element to the end of the array, growing the array if there is no more room.

* @param array The array to which to append the element. This must NOT be null.

* @param currentSize The number of elements in the array. Must be less than or equal to

* array.length.

* @param element The element to append.

* @return the array to which the element was appended. This may be different than the given

* array.

*/

public static T[] append(T[] array, int currentSize, T element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

@SuppressWarnings("unchecked")

T[] newArray = ArrayUtils.newUnpaddedArray(

(Class) array.getClass().getComponentType(), growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive int version of {@link #append(Object[], int, Object)}.

*/

public static int[] append(int[] array, int currentSize, int element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

int[] newArray = ArrayUtils.newUnpaddedIntArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive long version of {@link #append(Object[], int, Object)}.

*/

public static long[] append(long[] array, int currentSize, long element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

long[] newArray = ArrayUtils.newUnpaddedLongArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive boolean version of {@link #append(Object[], int, Object)}.

*/

public static boolean[] append(boolean[] array, int currentSize, boolean element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

boolean[] newArray = ArrayUtils.newUnpaddedBooleanArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Primitive float version of {@link #append(Object[], int, Object)}.

*/

public static float[] append(float[] array, int currentSize, float element) {

assert currentSize <= array.length;

if (currentSize + 1 > array.length) {

float[] newArray = ArrayUtils.newUnpaddedFloatArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, currentSize);

array = newArray;

}

array[currentSize] = element;

return array;

}

/**

* Inserts an element into the array at the specified index, growing the array if there is no

* more room.

*

* @param array The array to which to append the element. Must NOT be null.

* @param currentSize The number of elements in the array. Must be less than or equal to

* array.length.

* @param element The element to insert.

* @return the array to which the element was appended. This may be different than the given

* array.

*/

public static T[] insert(T[] array, int currentSize, int index, T element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

@SuppressWarnings("unchecked")

T[] newArray = ArrayUtils.newUnpaddedArray((Class)array.getClass().getComponentType(),

growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Primitive int version of {@link #insert(Object[], int, int, Object)}.

*/

public static int[] insert(int[] array, int currentSize, int index, int element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

int[] newArray = ArrayUtils.newUnpaddedIntArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Primitive long version of {@link #insert(Object[], int, int, Object)}.

*/

public static long[] insert(long[] array, int currentSize, int index, long element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

long[] newArray = ArrayUtils.newUnpaddedLongArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Primitive boolean version of {@link #insert(Object[], int, int, Object)}.

*/

public static boolean[] insert(boolean[] array, int currentSize, int index, boolean element) {

assert currentSize <= array.length;

if (currentSize + 1 <= array.length) {

System.arraycopy(array, index, array, index + 1, currentSize - index);

array[index] = element;

return array;

}

boolean[] newArray = ArrayUtils.newUnpaddedBooleanArray(growSize(currentSize));

System.arraycopy(array, 0, newArray, 0, index);

newArray[index] = element;

System.arraycopy(array, index, newArray, index + 1, array.length - index);

return newArray;

}

/**

* Given the current size of an array, returns an ideal size to which the array should grow.

* This is typically double the given size, but should not be relied upon to do so in the

* future.

*/

public static int growSize(int currentSize) {

return currentSize <= 4 ? 8 : currentSize * 2;

}

// Uninstantiable

private GrowingArrayUtils() {}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.Clas sUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值