本文整理汇总了Java中com.google.common.base.Suppliers类的典型用法代码示例。如果您正苦于以下问题:Java Suppliers类的具体用法?Java Suppliers怎么用?Java Suppliers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Suppliers类属于com.google.common.base包,在下文中一共展示了Suppliers类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRenaming_exceptionalReturn
import com.google.common.base.Suppliers; //导入依赖的package包/类
@GwtIncompatible // threads
public void testRenaming_exceptionalReturn() throws Exception {
String oldName = Thread.currentThread().getName();
final Supplier<String> newName = Suppliers.ofInstance("MyCrazyThreadName");
class MyException extends Exception {}
Callable<Void> callable = new Callable<Void>() {
@Override public Void call() throws Exception {
assertEquals(Thread.currentThread().getName(), newName.get());
throw new MyException();
}
};
try {
Callables.threadRenaming(callable, newName).call();
fail();
} catch (MyException expected) {}
assertEquals(oldName, Thread.currentThread().getName());
}
示例2: testRenaming_noPermissions
import com.google.common.base.Suppliers; //导入依赖的package包/类
@GwtIncompatible // threads
public void testRenaming_noPermissions() throws Exception {
System.setSecurityManager(new SecurityManager() {
@Override public void checkAccess(Thread t) {
throw new SecurityException();
}
@Override public void checkPermission(Permission perm) {
// Do nothing so we can clear the security manager at the end
}
});
try {
final String oldName = Thread.currentThread().getName();
Supplier<String> newName = Suppliers.ofInstance("MyCrazyThreadName");
Callable<Void> callable = new Callable<Void>() {
@Override public Void call() throws Exception {
assertEquals(Thread.currentThread().getName(), oldName);
return null;
}
};
Callables.threadRenaming(callable, newName).call();
assertEquals(oldName, Thread.currentThread().getName());
} finally {
System.setSecurityManager(null);
}
}
示例3: newAccessSupplier
import com.google.common.base.Suppliers; //导入依赖的package包/类
private <T> Supplier<Set<T>> newAccessSupplier(final JavaClass owner, final Function<JavaClass, Set<T>> doWithEachClass) {
return Suppliers.memoize(new Supplier<Set<T>>() {
@Override
public Set<T> get() {
ImmutableSet.Builder<T> result = ImmutableSet.builder();
for (final JavaClass javaClass : getPossibleTargetClassesForAccess()) {
result.addAll(doWithEachClass.apply(javaClass));
}
return result.build();
}
private Set<JavaClass> getPossibleTargetClassesForAccess() {
return ImmutableSet.<JavaClass>builder()
.add(owner)
.addAll(owner.getAllSubClasses())
.build();
}
});
}
示例4: completeMembers
import com.google.common.base.Suppliers; //导入依赖的package包/类
void completeMembers(final ImportContext context) {
fields = context.createFields(this);
methods = context.createMethods(this);
constructors = context.createConstructors(this);
staticInitializer = context.createStaticInitializer(this);
codeUnits = ImmutableSet.<JavaCodeUnit>builder()
.addAll(methods).addAll(constructors).addAll(staticInitializer.asSet())
.build();
members = ImmutableSet.<JavaMember>builder()
.addAll(fields)
.addAll(methods)
.addAll(constructors)
.build();
this.annotations = Suppliers.memoize(new Supplier<Map<String, JavaAnnotation>>() {
@Override
public Map<String, JavaAnnotation> get() {
return context.createAnnotations(JavaClass.this);
}
});
}
示例5: imports_shadowed_and_superclass_field_access
import com.google.common.base.Suppliers; //导入依赖的package包/类
@Test
public void imports_shadowed_and_superclass_field_access() throws Exception {
ImportedClasses classes = classesIn("testexamples/hierarchicalfieldaccess");
JavaClass classThatAccessesFieldOfSuperClass = classes.get(AccessToSuperAndSubClassField.class);
JavaClass superClassWithAccessedField = classes.get(SuperClassWithAccessedField.class);
JavaClass subClassWithAccessedField = classes.get(SubClassWithAccessedField.class);
Set<JavaFieldAccess> accesses = classThatAccessesFieldOfSuperClass.getFieldAccessesFromSelf();
assertThat(accesses).hasSize(2);
JavaField field = superClassWithAccessedField.getField("field");
FieldAccessTarget expectedSuperClassFieldAccess = new FieldAccessTargetBuilder()
.withOwner(subClassWithAccessedField)
.withName(field.getName())
.withType(field.getType())
.withField(Suppliers.ofInstance(Optional.of(field)))
.build();
assertThatAccess(getOnly(accesses, "field", GET))
.isFrom("accessSuperClassField")
.isTo(expectedSuperClassFieldAccess)
.inLineNumber(5);
assertThatAccess(getOnly(accesses, "maskedField", GET))
.isFrom("accessSubClassField")
.isTo(subClassWithAccessedField.getField("maskedField"))
.inLineNumber(9);
}
示例6: GSuiteGroupAuthorizationFilter
import com.google.common.base.Suppliers; //导入依赖的package包/类
public GSuiteGroupAuthorizationFilter(final GSuiteDirectoryService gsuiteDirService, AppConfiguration config) {
this.config = config;
this.externalAccountsCache = Suppliers.memoizeWithExpiration(
() -> {
String allowGroup = config.getExternalAccountsGroup();
Set<String> result = Collections.emptySet();
try {
GroupMembership membership = gsuiteDirService.getGroupMembers(allowGroup);
result = membership.getMembers() == null ? Collections.emptySet()
: membership.getMembers().stream().map(m -> m.getEmail()).collect(Collectors.toSet());
} catch (ResourceNotFoundException e) {
log.warn("Group for external accounts {} does not exists", allowGroup);
}
return result;
}, 15, TimeUnit.MINUTES);
}
示例7: DefaultTopology
import com.google.common.base.Suppliers; //导入依赖的package包/类
/**
* Creates a topology descriptor attributed to the specified provider.
*
* @param providerId identity of the provider
* @param description data describing the new topology
* @param broadcastFunction broadcast point function
*/
public DefaultTopology(ProviderId providerId, GraphDescription description,
Function<ConnectPoint, Boolean> broadcastFunction) {
super(providerId);
this.broadcastFunction = broadcastFunction;
this.time = description.timestamp();
this.creationTime = description.creationTime();
// Build the graph
this.graph = new DefaultTopologyGraph(description.vertexes(),
description.edges());
this.clusterResults = Suppliers.memoize(() -> searchForClusters());
this.clusters = Suppliers.memoize(() -> buildTopologyClusters());
this.clusterIndexes = Suppliers.memoize(() -> buildIndexes());
this.hopCountWeight = new HopCountLinkWeight(graph.getVertexes().size());
this.broadcastSets = Suppliers.memoize(() -> buildBroadcastSets());
this.infrastructurePoints = Suppliers.memoize(() -> findInfrastructurePoints());
this.computeCost = Math.max(0, System.nanoTime() - time);
}
示例8: main
import com.google.common.base.Suppliers; //导入依赖的package包/类
public static void main(String[] args) throws InterruptedException {
// {{start:memoize}}
log.info("Memoized");
Supplier<String> memoized = Suppliers.memoize(SuppliersExamples::helloWorldSupplier);
log.info(memoized.get());
log.info(memoized.get());
// {{end:memoize}}
// {{start:memoizeWithExpiration}}
log.info("Memoized with Expiration");
Supplier<String> memoizedExpiring = Suppliers.memoizeWithExpiration(
SuppliersExamples::helloWorldSupplier, 50, TimeUnit.MILLISECONDS);
log.info(memoizedExpiring.get());
log.info(memoizedExpiring.get());
log.info("sleeping");
TimeUnit.MILLISECONDS.sleep(100);
log.info(memoizedExpiring.get());
log.info(memoizedExpiring.get());
log.info("sleeping");
TimeUnit.MILLISECONDS.sleep(100);
log.info(memoizedExpiring.get());
log.info(memoizedExpiring.get());
// {{end:memoizeWithExpiration}}
}
示例9: ConfigService
import com.google.common.base.Suppliers; //导入依赖的package包/类
public ConfigService(List<ConfigurationProvider> configurationProviders, Authorizer authorizer, long maximumCacheSize, long cacheTtlMillis) {
this.authorizer = authorizer;
this.objectMapper = new ObjectMapper();
if (configurationProviders == null || configurationProviders.size() == 0) {
throw new IllegalArgumentException("Expected at least one configuration provider");
}
this.configurationProviderInfo = Suppliers.memoizeWithExpiration(() -> initContentTypeInfo(configurationProviders), cacheTtlMillis, TimeUnit.MILLISECONDS);
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
if (maximumCacheSize >= 0) {
cacheBuilder = cacheBuilder.maximumSize(maximumCacheSize);
}
if (cacheTtlMillis >= 0) {
cacheBuilder = cacheBuilder.refreshAfterWrite(cacheTtlMillis, TimeUnit.MILLISECONDS);
}
this.configurationCache = cacheBuilder
.build(new CacheLoader<ConfigurationProviderKey, ConfigurationProviderValue>() {
@Override
public ConfigurationProviderValue load(ConfigurationProviderKey key) throws Exception {
return initConfigurationProviderValue(key);
}
});
}
示例10: badCssUrl_resultsInError
import com.google.common.base.Suppliers; //导入依赖的package包/类
@Test
public void badCssUrl_resultsInError() throws Exception {
save(fs.getPath("/fs/path/index.html"), "<link rel=\"stylesheet\" href=\"index.css\">");
save(fs.getPath("/fs/path/index.css"), "body { background: url(hello.jpg); }");
assertThat(
validator.validate(
Webfiles.newBuilder()
.addSrc(WebfilesSource.newBuilder()
.setPath("/fs/path/index.html")
.setWebpath("/web/path/index.html")
.build())
.addSrc(WebfilesSource.newBuilder()
.setPath("/fs/path/index.css")
.setWebpath("/web/path/index.css")
.build())
.build(),
ImmutableList.<Webfiles>of(),
Suppliers.ofInstance(ImmutableList.<Webfiles>of())))
.isNotEmpty();
}
示例11: create
import com.google.common.base.Suppliers; //导入依赖的package包/类
@VisibleForTesting
static AttributeAggregate create(
final Supplier<Iterable<IScheduledTask>> taskSupplier,
final AttributeStore attributeStore) {
final Function<String, Iterable<IAttribute>> getHostAttributes =
host -> {
// Note: this assumes we have access to attributes for hosts where all active tasks
// reside.
requireNonNull(host);
return attributeStore.getHostAttributes(host).get().getAttributes();
};
return create(Suppliers.compose(
tasks -> FluentIterable.from(tasks)
.transform(Tasks::scheduledToSlaveHost)
.transformAndConcat(getHostAttributes),
taskSupplier));
}
示例12: absoluteReferenceToImgInSrcs_printsError
import com.google.common.base.Suppliers; //导入依赖的package包/类
@Test
public void absoluteReferenceToImgInSrcs_printsError() throws Exception {
save(fs.getPath("/fs/path/index.html"), "<img src=\"/a/b/c\">");
assertThat(
validator.validate(
Webfiles.newBuilder()
.addSrc(WebfilesSource.newBuilder()
.setPath("/fs/path/index.html")
.setWebpath("/web/path/index.html")
.build())
.build(),
ImmutableList.<Webfiles>of(),
Suppliers.ofInstance(ImmutableList.<Webfiles>of())))
.containsEntry(
WebfilesValidator.ABSOLUTE_PATH_ERROR,
"/fs/path/index.html: Please use relative path for asset: /a/b/c");
}
示例13: relativeReferenceToUndeclaredAsset_printsError
import com.google.common.base.Suppliers; //导入依赖的package包/类
@Test
public void relativeReferenceToUndeclaredAsset_printsError() throws Exception {
save(fs.getPath("/fs/path/index.html"), "<img src=\"hello.jpg\">");
save(fs.getPath("/fs/path/hello.jpg"), "oh my goth");
assertThat(
validator.validate(
Webfiles.newBuilder()
.addSrc(WebfilesSource.newBuilder()
.setPath("/fs/path/index.html")
.setWebpath("/web/path/index.html")
.build())
.build(),
ImmutableList.<Webfiles>of(),
Suppliers.ofInstance(ImmutableList.<Webfiles>of())))
.containsEntry(
WebfilesValidator.STRICT_DEPENDENCIES_ERROR,
"/fs/path/index.html: Referenced hello.jpg (/web/path/hello.jpg)"
+ " without depending on a web_library() rule providing it");
}
示例14: TUPackingInfo
import com.google.common.base.Suppliers; //导入依赖的package包/类
TUPackingInfo(final I_M_HU tuHU)
{
Check.assumeNotNull(tuHU, "Parameter tuHU is not null");
this.tuHU = tuHU;
huProductStorageSupplier = Suppliers.memoize(() -> {
final List<IHUProductStorage> productStorages = Services.get(IHandlingUnitsBL.class)
.getStorageFactory()
.getStorage(tuHU)
.getProductStorages();
if (productStorages.size() == 1)
{
return productStorages.get(0);
}
else
{
return null;
}
});
}
示例15: AggregatedTUPackingInfo
import com.google.common.base.Suppliers; //导入依赖的package包/类
public AggregatedTUPackingInfo(final I_M_HU aggregatedTU)
{
this.aggregatedTU = aggregatedTU;
huProductStorageSupplier = Suppliers.memoize(() -> {
final List<IHUProductStorage> productStorages = Services.get(IHandlingUnitsBL.class)
.getStorageFactory()
.getStorage(aggregatedTU)
.getProductStorages();
if (productStorages.size() == 1)
{
return productStorages.get(0);
}
else
{
return null;
}
});
}