下面列出了org.mockito.Spy#java.util.AbstractList 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List toList () {
if (size==0) {
return Collections.emptyList();
} else if (size==1) {
return Collections.singletonList(data[0]);
}
return new AbstractList() {
public Object get(int index) {
return FastArray.this.get(index);
}
public int size() {
return size;
}
};
}
@GwtIncompatible // invokeAccessibleNonThrowingMethod
private static List<StackTraceElement> jlaStackTrace(final Throwable t) {
checkNotNull(t);
/*
* TODO(cpovirk): Consider optimizing iterator() to catch IOOBE instead of doing bounds checks.
*
* TODO(cpovirk): Consider the UnsignedBytes pattern if it performs faster and doesn't cause
* AOSP grief.
*/
return new AbstractList<StackTraceElement>() {
@Override
public StackTraceElement get(int n) {
return (StackTraceElement) invokeAccessibleNonThrowingMethod(getStackTraceElementMethod, jla, t, n);
}
@Override
public int size() {
return (Integer) invokeAccessibleNonThrowingMethod(getStackTraceDepthMethod, jla, t);
}
};
}
@TLFunctionAnnotation("Tries to match entire input with specified pattern.")
public static final List<String> matchGroups(TLFunctionCallContext context, String input, String pattern) {
if(input == null){
return null;
}
final Matcher m = ((TLRegexpCache)context.getCache()).getCachedPattern(context, pattern).matcher(input);
if (m.matches()) {
return new AbstractList<String>() {
@Override
public String get(int index) {
return m.group(index);
}
@Override
public int size() {
return m.groupCount() + 1; // group 0 is not included in groupCount()
}
};
} else {
return null;
}
}
@Override
public List<String> get(final int row) {
rangeCheckRow(row, size());
return new AbstractList<String>() {
@Override
public String get(final int column) {
rangeCheckColumn(column, size());
return raw.get(column).get(row);
}
@Override
public int size() {
return height();
}
};
}
/**
* Creates a list that returns every {@code n}th element of a list,
* starting at element {@code k}.
*
* <p>It is OK if the list is empty or its size is not a multiple of
* {@code n}.</p>
*
* <p>For instance, {@code quotientList(list, 2, 0)} returns the even
* elements of a list, and {@code quotientList(list, 2, 1)} returns the odd
* elements. Those lists are the same length only if list has even size.</p>
*/
public static <E> List<E> quotientList(
final List<E> list, final int n, final int k) {
if (n <= 0 || k < 0 || k >= n) {
throw new IllegalArgumentException(
"n must be positive; k must be between 0 and n - 1");
}
final int size = (list.size() + n - k - 1) / n;
return new AbstractList<E>() {
public E get(int index) {
return list.get(index * n + k);
}
public int size() {
return size;
}
};
}
/**
* Report errors for unused propOrder entries.
*/
public void checkUnusedProperties() {
for( int i=0; i<used.length; i++ )
if(used[i]==null) {
String unusedName = propOrder[i];
String nearest = EditDistance.findNearest(unusedName, new AbstractList<String>() {
public String get(int index) {
return properties.get(index).getName();
}
public int size() {
return properties.size();
}
});
boolean isOverriding = (i > (properties.size()-1)) ? false : properties.get(i).hasAnnotation(OverrideAnnotationOf.class);
if (!isOverriding) {
builder.reportError(new IllegalAnnotationException(
Messages.PROPERTY_ORDER_CONTAINS_UNUSED_ENTRY.format(unusedName,nearest),ClassInfoImpl.this));
}
}
}
@GwtIncompatible // invokeAccessibleNonThrowingMethod
private static List<StackTraceElement> jlaStackTrace(final Throwable t) {
checkNotNull(t);
/*
* TODO(cpovirk): Consider optimizing iterator() to catch IOOBE instead of doing bounds checks.
*
* TODO(cpovirk): Consider the UnsignedBytes pattern if it performs faster and doesn't cause
* AOSP grief.
*/
return new AbstractList<StackTraceElement>() {
@Override
public StackTraceElement get(int n) {
return (StackTraceElement) invokeAccessibleNonThrowingMethod(getStackTraceElementMethod, jla, t, n);
}
@Override
public int size() {
return (Integer) invokeAccessibleNonThrowingMethod(getStackTraceDepthMethod, jla, t);
}
};
}
@Nonnull @Override public List<? extends MethodParameter> getParameters() {
final java.lang.reflect.Method method = this.method;
return new AbstractList<MethodParameter>() {
private final Class[] parameters = method.getParameterTypes();
@Override public MethodParameter get(final int index) {
return new BaseMethodParameter() {
@Nonnull @Override public Set<? extends Annotation> getAnnotations() {
return ImmutableSet.of();
}
@Nullable @Override public String getName() {
return null;
}
@Nonnull @Override public String getType() {
return ReflectionUtils.javaToDexName(parameters[index].getName());
}
};
}
@Override public int size() {
return parameters.length;
}
};
}
/**
* Converts two lists into a list of {@link IntPair}s.
*
* <p>The length of the combined list is the lesser of the lengths of the
* source lists. But typically the source lists will be the same length.</p>
*
* @param lefts Left list
* @param rights Right list
* @param strict Whether to fail if lists have different size
* @return List of pairs
*/
public static List<IntPair> zip(
final List<? extends Number> lefts,
final List<? extends Number> rights,
boolean strict) {
final int size;
if (strict) {
if (lefts.size() != rights.size()) {
throw new AssertionError();
}
size = lefts.size();
} else {
size = Math.min(lefts.size(), rights.size());
}
return new AbstractList<IntPair>() {
public IntPair get(int index) {
return IntPair.of(lefts.get(index).intValue(),
rights.get(index).intValue());
}
public int size() {
return size;
}
};
}
/**
* Initialize global settings for DN
*/
protected void initGlobalSetting(Configuration conf,
AbstractList<File> dataDirs) throws IOException {
this.dataDirs = dataDirs;
this.conf = conf;
storage = new DataStorage(this);
// global DN settings
initConfig(conf);
registerMXBean();
initDataXceiver(conf);
startInfoServer(conf);
initIpcServer(conf);
myMetrics = new DataNodeMetrics(conf, storage.getStorageID());
}
/**
* {@link Headers#get(Object)} and convert each element of {@link List} to a {@link String}.
* @param name the name of the header to retrieve
* @return a {@link List} of header values or an empty {@link List} if no values are found.
*/
public static <K, V> List<String> getAllAsString(Headers<K, V, ?> headers, K name) {
final List<V> allNames = headers.getAll(name);
return new AbstractList<String>() {
@Override
public String get(int index) {
V value = allNames.get(index);
return value != null ? value.toString() : null;
}
@Override
public int size() {
return allNames.size();
}
};
}
public void addExperimentalDataObjectsToJson(AbstractList<ExperimentalDataObject> expObjects) {
createDefaultMotionObjects();
// create default top Group for motion
JSONObject topJson = motionObjectsRoot;
if (topJson.get("children") == null) {
topJson.put("children", new JSONArray());
}
JSONArray motObjectsChildren = (JSONArray) topJson.get("children");
for (ExperimentalDataObject nextExpObject : expObjects) {
if (mapComponentToUUID.get(nextExpObject)!= null)
continue;
ArrayList<UUID> comp_uuids = new ArrayList<UUID>();
motObjectsChildren.add(nextExpObject.createDecorationJson(comp_uuids, this));
mapComponentToUUID.put(nextExpObject, comp_uuids);
mapUUIDToComponent.put(comp_uuids.get(0), nextExpObject);
}
}
/**
* Combines a list of parser positions to create a position which spans
* from the beginning of the first to the end of the last.
*/
private static SqlParserPos sum_(final List<SqlParserPos> positions) {
switch (positions.size()) {
case 0:
throw new AssertionError();
case 1:
return positions.get(0);
default:
final List<SqlParserPos> poses = new AbstractList<SqlParserPos>() {
public SqlParserPos get(int index) {
return positions.get(index + 1);
}
public int size() {
return positions.size() - 1;
}
};
final SqlParserPos p = positions.get(0);
return sum(poses, p.lineNumber, p.columnNumber, p.endLineNumber,
p.endColumnNumber);
}
}
@Test
public void getAllSuperclasses() {
assertEquals(null, ClassUtil.getAllSuperclasses(null));
assertTrue(ClassUtil.getAllSuperclasses(Object.class).isEmpty());
assertTrue(ClassUtil.getAllSuperclasses(String.class).isEmpty());
List<Class<?>> supers = ClassUtil.getAllSuperclasses(ArrayList.class);
assertTrue(supers.contains(AbstractList.class));
assertTrue(supers.contains(AbstractCollection.class));
assertFalse(supers.contains(List.class));
assertFalse(supers.contains(Object.class));
assertTrue(ClassUtil.getAllSuperclasses(int.class).isEmpty());
assertTrue(ClassUtil.getAllSuperclasses(int[].class).isEmpty());
assertTrue(ClassUtil.getAllSuperclasses(Integer.class).contains(Number.class));
assertTrue(ClassUtil.getAllSuperclasses(Integer[].class).isEmpty());
}
@Nonnull @Override public List<? extends MethodParameter> getParameters() {
final java.lang.reflect.Method method = this.method;
return new AbstractList<MethodParameter>() {
private final Class[] parameters = method.getParameterTypes();
@Override public MethodParameter get(final int index) {
return new BaseMethodParameter() {
@Nonnull @Override public Set<? extends Annotation> getAnnotations() {
return ImmutableSet.of();
}
@Nullable @Override public String getName() {
return null;
}
@Nonnull @Override public String getType() {
return ReflectionUtils.javaToDexName(parameters[index].getName());
}
};
}
@Override public int size() {
return parameters.length;
}
};
}
/** Helper for {@link #getColumnStrategies()}. */
public static List<ColumnStrategy> columnStrategies(final RelOptTable table) {
final int fieldCount = table.getRowType().getFieldCount();
final InitializerExpressionFactory ief =
Util.first(table.unwrap(InitializerExpressionFactory.class),
NullInitializerExpressionFactory.INSTANCE);
return new AbstractList<ColumnStrategy>() {
public int size() {
return fieldCount;
}
public ColumnStrategy get(int index) {
return ief.generationStrategy(table, index);
}
};
}
/**
* Creates a list that returns every {@code n}th element of a list,
* starting at element {@code k}.
*
* <p>It is OK if the list is empty or its size is not a multiple of
* {@code n}.</p>
*
* <p>For instance, {@code quotientList(list, 2, 0)} returns the even
* elements of a list, and {@code quotientList(list, 2, 1)} returns the odd
* elements. Those lists are the same length only if list has even size.</p>
*/
public static <E> List<E> quotientList(
final List<E> list, final int n, final int k) {
if (n <= 0 || k < 0 || k >= n) {
throw new IllegalArgumentException(
"n must be positive; k must be between 0 and n - 1");
}
final int size = (list.size() + n - k - 1) / n;
return new AbstractList<E>() {
public E get(int index) {
return list.get(index * n + k);
}
public int size() {
return size;
}
};
}
@Override
public List<String> get(final int row) {
rangeCheckRow(row, size());
return new AbstractList<String>() {
@Override
public String get(final int column) {
rangeCheckColumn(column, size());
return raw.get(fromRow + row).get(fromColumn + column);
}
@Override
public int size() {
return toColumn - fromColumn;
}
};
}
/**
* Returns a new unmodifiable view that is the merging of two lists
* @param <T> the type the lists hold
* @param left the left portion of the merged view
* @param right the right portion of the merged view
* @return a list view that contains bot the left and right lists
*/
public static <T> List<T> mergedView(final List<T> left, final List<T> right)
{
List<T> merged = new AbstractList<T>()
{
@Override
public T get(int index)
{
if(index < left.size())
return left.get(index);
else if(index-left.size() < right.size())
return right.get(index-left.size());
else
throw new IndexOutOfBoundsException("List of lengt " + size() + " has no index " + index);
}
@Override
public int size()
{
return left.size() + right.size();
}
};
return merged;
}
private static Iterable<Object> iterable(final Object first, final Object second, final Object[] rest) {
checkNotNull(rest);
return new AbstractList<Object>() {
@Override
public int size() {
return rest.length + 2;
}
@Override
public Object get(int index) {
switch (index) {
case 0:
return first;
case 1:
return second;
default:
return rest[index - 2];
}
}
};
}
/**
* Creates a stream from a JSON array.
*
* @param <T>
* the stream type
* @param array
* the JSON array to create a stream from
* @return a stream of JSON values
*/
public static <T extends JsonValue> Stream<T> stream(JsonArray array) {
if (array == null) {
return Stream.empty();
}
return new AbstractList<T>() {
@Override
public T get(int index) {
return array.get(index);
}
@Override
public int size() {
return array.length();
}
}.stream();
}
/**
* Converts two lists into a list of {@link IntPair}s.
*
* <p>The length of the combined list is the lesser of the lengths of the
* source lists. But typically the source lists will be the same length.</p>
*
* @param lefts Left list
* @param rights Right list
* @param strict Whether to fail if lists have different size
* @return List of pairs
*/
public static List<IntPair> zip(
final List<? extends Number> lefts,
final List<? extends Number> rights,
boolean strict) {
final int size;
if (strict) {
if (lefts.size() != rights.size()) {
throw new AssertionError();
}
size = lefts.size();
} else {
size = Math.min(lefts.size(), rights.size());
}
return new AbstractList<IntPair>() {
public IntPair get(int index) {
return IntPair.of(lefts.get(index).intValue(),
rights.get(index).intValue());
}
public int size() {
return size;
}
};
}
/** Creates a view onto this bit set as a list of integers.
*
* <p>The {@code cardinality} and {@code get} methods are both O(n), but
* the iterator is efficient. The list is memory efficient, and the CPU cost
* breaks even (versus {@link #toList}) if you intend to scan it only once. */
public List<Integer> asList() {
return new AbstractList<Integer>() {
@Override public Integer get(int index) {
return nth(index);
}
@Override public int size() {
return cardinality();
}
@Nonnull @Override public Iterator<Integer> iterator() {
return ImmutableBitSet.this.iterator();
}
};
}
@Override
public List getValues() {
final List origValues = buffer.getValues();
return new AbstractList() {
@Override
public Object get(int index) {
return origValues.get(rawOffset(index));
}
@Override
public int size() {
return getLength();
}
};
}
@Override
protected void processFsCommand(String[] cmdTokens) throws IOException {
filter.validate(PigCommandFilter.Command.FS);
if(mExplain == null) { // process only if not in "explain" mode
executeBatch();
int retCode = -1;
try {
retCode = shell.run(cmdTokens);
} catch (Exception e) {
throw new IOException(e);
}
if (retCode != 0 && !mInteractive) {
String s = LoadFunc.join(
(AbstractList<String>) Arrays.asList(cmdTokens), " ");
throw new IOException("fs command '" + s
+ "' failed. Please check output logs for details");
}
} else {
log.warn("'fs' statement is ignored while processing 'explain -script' or '-check'");
}
}
public static VTable select(final VTable table, final ListInt indexes) {
List<String> names = columnNames(table);
List<Class<?>> types = columnTypes(table);
List<Object> data = new AbstractList<Object>() {
@Override
public Object get(int index) {
return selectColumnData(table, index, indexes);
}
@Override
public int size() {
return table.getColumnCount();
}
};
return ValueFactory.newVTable(types, names, data);
}
/**
* Inverts the order of the elements in a
* sequence.
*/
public static <TSource> Enumerable<TSource> reverse(
Enumerable<TSource> source) {
final List<TSource> list = toList(source);
final int n = list.size();
return Linq4j.asEnumerable(
new AbstractList<TSource>() {
public TSource get(int index) {
return list.get(n - 1 - index);
}
public int size() {
return n;
}
});
}
/**
* Presents a view of the {@link RexWinAggCall} list as a list of
* {@link AggregateCall}.
*/
public List<AggregateCall> getAggregateCalls(Window windowRel) {
final List<String> fieldNames =
Util.skip(windowRel.getRowType().getFieldNames(),
windowRel.getInput().getRowType().getFieldCount());
return new AbstractList<AggregateCall>() {
public int size() {
return aggCalls.size();
}
public AggregateCall get(int index) {
final RexWinAggCall aggCall = aggCalls.get(index);
final SqlAggFunction op = (SqlAggFunction) aggCall.getOperator();
return AggregateCall.create(op, aggCall.distinct, false,
aggCall.ignoreNulls, getProjectOrdinals(aggCall.getOperands()),
-1, RelCollations.EMPTY,
aggCall.getType(), fieldNames.get(aggCall.ordinal));
}
};
}
private List<Expression> fieldReferences(
final Expression parameter, final List<Integer> fields) {
return new AbstractList<Expression>() {
public Expression get(int index) {
return fieldReference(parameter, fields.get(index));
}
public int size() {
return fields.size();
}
};
}
@Test
public void implement_predicate() {
List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().implement(classWithNameOf(Collection.class)))
.on(ArrayList.class, List.class, Iterable.class);
assertThatMembers(members).matchInAnyOrderMembersOf(ArrayList.class);
members = filterResultOf(members().that().areDeclaredInClassesThat().implement(classWithNameOf(AbstractList.class)))
.on(ArrayList.class, List.class, Iterable.class);
assertThat(members).isEmpty();
}