本文整理汇总了Java中com.google.common.collect.Iterables.getOnlyElement方法的典型用法代码示例。如果您正苦于以下问题:Java Iterables.getOnlyElement方法的具体用法?Java Iterables.getOnlyElement怎么用?Java Iterables.getOnlyElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.collect.Iterables
的用法示例。
在下文中一共展示了Iterables.getOnlyElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitClassOrInterface
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@Override
protected boolean visitClassOrInterface(FunctionType type) {
Type currentJavaType = getCurrentJavaType();
List<TypeReference> generics = convertTypeParametersDefinition(type);
currentJavaType.getTypeParameters().addAll(generics);
// If the type is an external type and have extension point, we need to retrofit the generics on
// the extension type as well
if (currentJavaType.isExtern()
&& getJavaTypeRegistry().containsExtensionType(currentJavaType)) {
Type extensionType = getJavaTypeRegistry().getExtensionType(currentJavaType);
extensionType.getTypeParameters().addAll(generics);
// parametrize the extension clause
TypeReference currentExtensionReference =
Iterables.getOnlyElement(extensionType.getInheritedTypes());
checkState(currentExtensionReference instanceof JavaTypeReference);
extensionType.getInheritedTypes().clear();
extensionType
.getInheritedTypes()
.add(new ParametrizedTypeReference(currentExtensionReference, generics));
}
return true;
}
示例2: testEntryReflectsIteratorRemove
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
@MultisetFeature.Require(ENTRIES_ARE_VIEWS)
public void testEntryReflectsIteratorRemove() {
initThreeCopies();
assertEquals(3, getMultiset().count(e0()));
Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
assertEquals(3, entry.getCount());
Iterator<E> itr = getMultiset().iterator();
itr.next();
itr.remove();
assertEquals(2, entry.getCount());
itr.next();
itr.remove();
itr.next();
itr.remove();
assertEquals(0, entry.getCount());
}
示例3: testWildcardedListListener
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@Test
public void testWildcardedListListener() throws Exception {
final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
deleteTx(BAR_PATH).submit().checkedGet();
final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
}
示例4: testWriteThroughEntry
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
public void testWriteThroughEntry() {
for (LoadingCache<Object, Object> cache : caches()) {
cache.getUnchecked(1);
Entry<Object, Object> entry = Iterables.getOnlyElement(cache.asMap().entrySet());
cache.invalidate(1);
assertEquals(0, cache.size());
entry.setValue(3);
assertEquals(1, cache.size());
assertEquals(3, cache.getIfPresent(1));
checkValidState(cache);
try {
entry.setValue(null);
fail();
} catch (NullPointerException expected) {
}
checkValidState(cache);
}
}
示例5: testForwardingObject
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
/**
* Ensures that all interface methods of {@code forwarderClass} are forwarded to the
* {@link ForwardingObject#delegate}. {@code forwarderClass} is assumed to only implement one
* interface.
*/
static <T extends ForwardingObject> void testForwardingObject(final Class<T> forwarderClass) {
@SuppressWarnings("unchecked") // super interface type of T
Class<? super T> interfaceType = (Class<? super T>)
Iterables.getOnlyElement(Arrays.asList(forwarderClass.getInterfaces()));
new ForwardingWrapperTester()
.testForwarding(
interfaceType,
new Function<Object, T>() {
@Override
public T apply(Object delegate) {
T mock = mock(forwarderClass, CALLS_REAL_METHODS.get());
try {
T stubber = doReturn(delegate).when(mock);
DELEGATE_METHOD.invoke(stubber);
} catch (Exception e) {
throw new RuntimeException(e);
}
return mock;
}
});
}
示例6: visitSingleMemberAnnotation
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
boolean visitSingleMemberAnnotation(AnnotationTree node) {
if (node.getArguments().size() != 1) {
return false;
}
ExpressionTree value = Iterables.getOnlyElement(node.getArguments());
if (value.getKind() == ASSIGNMENT) {
return false;
}
boolean isArrayInitializer = value.getKind() == NEW_ARRAY;
builder.open(isArrayInitializer ? ZERO : plusFour);
token("@");
scan(node.getAnnotationType(), null);
token("(");
if (!isArrayInitializer) {
builder.breakOp();
}
scan(value, null);
builder.close();
token(")");
return true;
}
示例7: invoke
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
/**
* When a factory method is invoked, we create a child injector that binds all parameters, then
* use that to get an instance of the return type.
*/
@Override
public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
Provider<?> provider = getBindingFromNewInjector(method, args).getProvider();
try {
return provider.get();
} catch (ProvisionException e) {
// if this is an exception declared by the factory method, throw it as-is
if (e.getErrorMessages().size() == 1) {
Message onlyError = Iterables.getOnlyElement(e.getErrorMessages());
Throwable cause = onlyError.getCause();
if (cause != null && canRethrow(method, cause)) {
throw cause;
}
}
throw e;
}
}
示例8: determineManagedPropertyType
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
private static ModelType<?> determineManagedPropertyType(StructBindingExtractionContext<?> extractionContext, String propertyName, Multimap<PropertyAccessorType, StructMethodBinding> accessorBindings) {
Collection<StructMethodBinding> isGetter = accessorBindings.get(IS_GETTER);
for (StructMethodBinding isGetterBinding : isGetter) {
if (!((ManagedPropertyMethodBinding) isGetterBinding).getDeclaredPropertyType().equals(ModelType.of(Boolean.TYPE))) {
WeaklyTypeReferencingMethod<?, ?> isGetterMethod = isGetterBinding.getViewMethod();
extractionContext.add(isGetterMethod, String.format("it should either return 'boolean', or its name should be '%s()'",
"get" + isGetterMethod.getName().substring(2)));
}
}
Set<ModelType<?>> potentialPropertyTypes = Sets.newLinkedHashSet();
for (StructMethodBinding binding : accessorBindings.values()) {
if (binding.getAccessorType() == SETTER) {
continue;
}
ManagedPropertyMethodBinding propertyBinding = (ManagedPropertyMethodBinding) binding;
potentialPropertyTypes.add(propertyBinding.getDeclaredPropertyType());
}
Collection<ModelType<?>> convergingPropertyTypes = findConvergingTypes(potentialPropertyTypes);
if (convergingPropertyTypes.size() != 1) {
extractionContext.add(propertyName, String.format("it must have a consistent type, but it's defined as %s",
Joiner.on(", ").join(ModelTypes.getDisplayNames(convergingPropertyTypes))));
return convergingPropertyTypes.iterator().next();
}
ModelType<?> propertyType = Iterables.getOnlyElement(convergingPropertyTypes);
for (StructMethodBinding setterBinding : accessorBindings.get(SETTER)) {
ManagedPropertyMethodBinding propertySetterBinding = (ManagedPropertyMethodBinding) setterBinding;
ModelType<?> declaredSetterType = propertySetterBinding.getDeclaredPropertyType();
if (!declaredSetterType.equals(propertyType)) {
extractionContext.add(setterBinding.getViewMethod(), String.format("it should take parameter with type '%s'",
propertyType.getDisplayName()));
}
}
return propertyType;
}
示例9: parseUnit
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
private Unit parseUnit(String templateText) throws Exception {
ParsingResult<Object> result = new ReportingParseRunner<>(parser.Unit()).run(templateText);
if (result.hasErrors()) {
String errors = ErrorUtils.printParseErrors(result.parseErrors);
throw new Exception(errors);
}
return (Unit) Iterables.getOnlyElement(result.valueStack);
}
示例10: getThriftServiceAnnotation
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
public static ThriftService getThriftServiceAnnotation(Class<?> serviceClass)
{
Set<ThriftService> serviceAnnotations = getEffectiveClassAnnotations(serviceClass, ThriftService.class);
checkArgument(!serviceAnnotations.isEmpty(), "Service class %s is not annotated with @ThriftService", serviceClass.getName());
checkArgument(serviceAnnotations.size() == 1, "Service class %s has multiple conflicting @ThriftService annotations: %s", serviceClass.getName(), serviceAnnotations);
return Iterables.getOnlyElement(serviceAnnotations);
}
示例11: testEntryViewReflectsRemove
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_REMOVE)
@MultisetFeature.Require(ENTRIES_ARE_VIEWS)
public void testEntryViewReflectsRemove() {
initThreeCopies();
assertEquals(3, getMultiset().count(e0()));
Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
assertEquals(3, entry.getCount());
assertTrue(getMultiset().remove(e0()));
assertEquals(2, entry.getCount());
assertTrue(getMultiset().elementSet().remove(e0()));
assertEquals(0, entry.getCount());
}
示例12: testEntryReflectsClear
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_REMOVE)
@MultisetFeature.Require(ENTRIES_ARE_VIEWS)
public void testEntryReflectsClear() {
initThreeCopies();
assertEquals(3, getMultiset().count(e0()));
Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
assertEquals(3, entry.getCount());
getMultiset().clear();
assertEquals(0, entry.getCount());
}
示例13: createAndVerifyTop
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
final Collection<DataTreeModification<Top>> events = listener.nextEvent();
assertFalse("Non empty collection should be received.",events.isEmpty());
final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
}
示例14: getField
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@Override
public Field getField(String fieldName, int index) {
Field field;
if (isSingleTyped()) {
field = Iterables.getOnlyElement(fields, null);
} else {
field = index < fields.size() ? fields.get(index) : null;
}
return field;
}
示例15: testAsMapEntrySetRemovePropagatesToMultimap
import com.google.common.collect.Iterables; //导入方法依赖的package包/类
@CollectionSize.Require(SEVERAL)
@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
public void testAsMapEntrySetRemovePropagatesToMultimap() {
resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
Entry<K, Collection<V>> asMapEntry0 = Iterables.getOnlyElement(asMapEntrySet);
assertTrue(multimap().put(k1(), v4()));
assertTrue(asMapEntrySet.remove(asMapEntry0));
assertEquals(1, multimap().size());
assertContentsInOrder(multimap().keySet(), k1());
}