本文整理匯總了Java中com.google.common.collect.ObjectArrays.newArray方法的典型用法代碼示例。如果您正苦於以下問題:Java ObjectArrays.newArray方法的具體用法?Java ObjectArrays.newArray怎麽用?Java ObjectArrays.newArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.collect.ObjectArrays
的用法示例。
在下文中一共展示了ObjectArrays.newArray方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: remove
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
/**
* Removes the first occurrence of a given value from an array, if it is present in the array.
* The array is assumed to be unordered.
*
* @param array
* to remove the value from; may be {@code null}
* @param value
* to remove; must not be {@code null}
* @return an array not containing the first occurrence of value, but containing all other elements of
* the original array. If the original array does not contain the given value, the returned
* array is == identical to the array passed in.
*/
public T[] remove(final T[] array, final T value) {
if (array == null) {
return null;
}
int i = find(array, value);
if (i == 0 && array.length == 1) {
return null;
}
if (i >= 0) {
// Found it: remove value. i is guaranteed to be < array.length here.
T[] newArray = ObjectArrays.newArray(componentType, array.length - 1);
if (i > 0) {
System.arraycopy(array, 0, newArray, 0, i);
}
if (i + 1 < array.length) {
System.arraycopy(array, i + 1, newArray, i, array.length - i - 1);
}
return newArray;
}
return array;
}
示例2: readSignal
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
protected <T> T[] readSignal(Class<T> t, OffsetLength offsetLength) throws ReconException {
if (offsetLength.getOffset() == 0 || offsetLength.getLength() == 0) {
throw new ReconException("Cannot read signal as offset and length invalid " + offsetLength);
}
try {
byte[] bytes = new byte[offsetLength.getLength()];
if (offsetLength.getLength() == resource.read(offsetLength.getOffset(), bytes)) {
if (isCompressed()) {
bytes = Compression.decompress(bytes);
}
BufferUnpacker unpacker = getMessagePack().createBufferUnpacker(bytes);
int arrayLength = unpacker.readArrayBegin();
T[] out = ObjectArrays.newArray(t, arrayLength);
for (int i = 0; i < arrayLength; i++) {
out[i] = (T) readObject(unpacker);
}
unpacker.readArrayEnd();
unpacker.close();
return out;
}
throw new ReconException("Failed to read signal at location");
} catch (IOException ex) {
throw new ReconException("Failed to read signal", ex);
}
}
示例3: getHandles
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
static Object[] getHandles(NMSPathPoint[] points) {
Object[] handles = ObjectArrays.newArray(NMSPathPoint.nmsPathPoint, points.length);
for (int i = 0; i < points.length; i++) {
handles[i] = points[i].getHandle();
}
return handles;
}
示例4: convertArrayWithDeserializer
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
public static <T> T[] convertArrayWithDeserializer(ScriptObjectMirror mirror, String key, IScriptObjectDeserializer deserializer, Class<T> toClass) {
try {
Object val = mirror.get(key);
return convertArrayWithDeserializer(val, deserializer, toClass);
} catch (Exception e) {
LogHelper.error("Error deserializing array of " + toClass.getName(), e);
return ObjectArrays.newArray(toClass, 0);
}
}
示例5: arrayIf
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
@Override protected <T> Object arrayIf(T[] resolvedP, boolean isPrimitive) {
int l = Math.max(2, r.nextInt(5));
if (isPrimitive) {
this.clazz = Primitives.wrap(this.clazz);
}
Object[] rArr = ObjectArrays.newArray(this.clazz, l);
for (int i = 0; i < rArr.length; i++) {
rArr[i] = get(this.clazz);
}
if (isPrimitive) {
return Arrays2.unwraps(rArr);
}
return rArr;
}
示例6: getElements
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
@Override
public Object[] getElements(Object inputElement) {
if (topLevelElement == null) {
return ObjectArrays.newArray(Object.class, 0);
}
return getElements();
}
示例7: initialize_string_array_java_with_guava
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
@Test
public void initialize_string_array_java_with_guava () {
String[] nflNorthStadiums = ObjectArrays.newArray(String.class, 4);
nflNorthStadiums[0] = "Lambeau Field";
nflNorthStadiums[1] = "Soldier Field";
nflNorthStadiums[2] = "Mall of America Fielddagger";
nflNorthStadiums[3] = "Ford Fielddagger";
assertTrue(nflNorthStadiums.length == 4);
}
示例8: initialize_string_array_java_with_guava_reference_type
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
@Test
public void initialize_string_array_java_with_guava_reference_type () {
String[] nflStadiums = {""};
String[] nflNorthStadiums = ObjectArrays.newArray(nflStadiums, 4);
nflNorthStadiums[0] = "Lambeau Field";
nflNorthStadiums[1] = "Soldier Field";
nflNorthStadiums[2] = "Mall of America Fielddagger";
nflNorthStadiums[3] = "Ford Fielddagger";
assertEquals(4, nflNorthStadiums.length);
}
示例9: DirList3
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
public DirList3(Entry3[] entries, boolean eof) {
this.entries = ObjectArrays.newArray(entries, entries.length);
System.arraycopy(this.entries, 0, entries, 0, entries.length);
this.eof = eof;
}
示例10: DirListPlus3
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
public DirListPlus3(EntryPlus3[] entries, boolean eof) {
this.entries = ObjectArrays.newArray(entries, entries.length);
System.arraycopy(this.entries, 0, entries, 0, entries.length);
this.eof = eof;
}
示例11: arrayIf
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
@Override protected <T> Object arrayIf(T[] resolvedP, boolean isPrimitive) {
return ObjectArrays.newArray(this.clazz, 0);
}
示例12: wraps
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
/**
* Converts the given target as an array of primitive to object.
*
* @param target
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T[] wraps(Object target, Class<?> expectClazz) {
if (null == target) {
return null;
}
Class<?> clazz = target.getClass();
if (null != expectClazz) {
expectClazz = expectClazz.isArray() ? expectClazz.getComponentType() : expectClazz;
Function<Object, T> func = (Function<Object, T>) Funcs.getParseFunction(expectClazz);
if (null != func) {
expectClazz = Primitives.wrap(expectClazz);
T[] expectArray = null;
//target is array
if (clazz.isArray()) {
Object[] providArray = wraps(target);
expectArray = (T[]) ObjectArrays.newArray(expectClazz, providArray.length);
for (int i = 0; i < providArray.length; i++) {
expectArray[i] = func.apply(providArray[i]);
}
}
//target is not array
else {
expectArray = (T[]) ObjectArrays.newArray(expectClazz, 1);
expectArray[0] = func.apply(target);
}
return expectArray;
}
}
if (!clazz.isArray()) {
T[] a = (T[]) ObjectArrays.newArray(clazz, 1);
a[0] = (T) target;
return a;
}
return new StructBehavior<T[]>(target) {
@Override protected T[] primitiveIs(boolean primitive) { return primitive ? toBeContinued() : noneMatched(); }
@Override protected T[] booleanIf() { return (T[]) wrap((boolean[]) this.delegate); }
@Override protected T[] byteIf() { return (T[]) wrap((byte[]) this.delegate); }
@Override protected T[] characterIf() { return (T[]) wrap((char[]) this.delegate); }
@Override protected T[] doubleIf() { return (T[]) wrap((double[]) this.delegate); }
@Override protected T[] floatIf() { return (T[]) wrap((float[]) this.delegate); }
@Override protected T[] integerIf() { return (T[]) wrap((int[]) this.delegate); }
@Override protected T[] longIf() { return (T[]) wrap((long[]) this.delegate); }
@Override protected T[] shortIf() { return (T[]) wrap((short[]) this.delegate); }
@Override protected T[] nullIf() { return null; }
@Override protected T[] noneMatched() { return (T[]) this.delegate; }
}.doDetect();
}
示例13: toArray
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
public Object[] toArray(Object[] a) {
return ObjectArrays.newArray(a, 0);
}
示例14: toArray
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
/**
* Returns an array containing all of the elements in this queue, in
* proper sequence; the runtime type of the returned array is that of
* the specified array. If the queue fits in the specified array, it
* is returned therein. Otherwise, a new array is allocated with the
* runtime type of the specified array and the size of this queue.
*
* <p>If this queue fits in the specified array with room to spare
* (i.e., the array has more elements than this queue), the element in
* the array immediately following the end of the queue is set to
* <tt>null</tt>.
*
* <p>Like the {@link #toArray()} method, this method acts as bridge between
* array-based and collection-based APIs. Further, this method allows
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
* <p>Suppose <tt>x</tt> is a queue known to contain only strings.
* The following code can be used to dump the queue into a newly
* allocated array of <tt>String</tt>:
*
* <pre>
* String[] y = x.toArray(new String[0]);</pre>
*
* <p>Note that <tt>toArray(new Object[0])</tt> is identical in function to
* <tt>toArray()</tt>.
*
* @param a the array into which the elements of the queue are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose
* @return an array containing all of the elements in this queue
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this queue
* @throws NullPointerException if the specified array is null
*/
@Override public <T> T[] toArray(T[] a) {
final E[] items = this.items;
final Monitor monitor = this.monitor;
monitor.enter();
try {
if (a.length < count)
a = ObjectArrays.newArray(a, count);
int k = 0;
int i = takeIndex;
while (k < count) {
// This cast is not itself safe, but the following statement
// will fail if the runtime type of items[i] is not assignable
// to the runtime type of a[k++], which is all that the method
// contract requires (see @throws ArrayStoreException above).
@SuppressWarnings("unchecked")
T t = (T) items[i];
a[k++] = t;
i = inc(i);
}
if (a.length > count)
a[count] = null;
return a;
} finally {
monitor.leave();
}
}
示例15: removeAll
import com.google.common.collect.ObjectArrays; //導入方法依賴的package包/類
/**
* Remove from array {@code o1} all elements which exists in {@code o2} if {@code retain} flag is
* set to {@code false}. If argument {@code retain} is set to {@code true}, then in result
* collection will be common elements from both arrays.
*
* @param o1 input array
* @param o2 array, elements of which should be removed from the {@code o2} array
* @param retain true if operation should be performed with retain algorithm, false means that
* from {@code o1} should be removed all elements that contains in {@code o2}
* @param <T> type of given {@code o1} and {@code o2}
* @return new array, which contains elements based on operation type
* @throws IllegalArgumentException in case if given arrays null
* @since 4.3.0
*/
public static <T> T[] removeAll(T[] o1, T[] o2, boolean retain) {
checkArgument(o1 != null && o2 != null, "Input arrays are null");
T[] retained = retain ? ObjectArrays.newArray(o1, 0) : o1;
for (int index = 0; index < o1.length; index++) {
if (indexOf(o2, o1[index]) != -1) {
retained = retain ? add(retained, o1[index]) : remove(retained, o1[index]);
}
}
return retained;
}