下面列出了com.google.common.collect.MutableClassToInstanceMap#create ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a copy of this configuration only including the given fragments (which the current
* configuration is assumed to have).
*/
public BuildConfiguration clone(
FragmentClassSet fragmentClasses,
RuleClassProvider ruleClassProvider,
BuildOptions defaultBuildOptions) {
ClassToInstanceMap<Fragment> fragmentsMap = MutableClassToInstanceMap.create();
for (Fragment fragment : fragments.values()) {
if (fragmentClasses.fragmentClasses().contains(fragment.getClass())) {
fragmentsMap.put(fragment.getClass(), fragment);
}
}
BuildOptions options =
buildOptions.trim(getOptionsClasses(fragmentsMap.keySet(), ruleClassProvider));
BuildConfiguration newConfig =
new BuildConfiguration(
getDirectories(),
fragmentsMap,
options,
BuildOptions.diffForReconstruction(defaultBuildOptions, options),
reservedActionMnemonics,
actionEnv,
mainRepositoryName.strippedName());
return newConfig;
}
@Test
public void classToINstanceMap_example () {
Person person = new Person("Jackson");
Jobs jobs = new Jobs("IT person");
Address address = new Address("505 Williams Street");
ClassToInstanceMap<Object> classToInstanceMap = MutableClassToInstanceMap.create();
classToInstanceMap.put(Person.class, person);
classToInstanceMap.put(Jobs.class, jobs);
classToInstanceMap.put(Address.class, address);
logger.info(classToInstanceMap);
assertEquals("IT person", classToInstanceMap.getInstance(Jobs.class).getJobName());
}
/** Constructs the registry configured by this builder. */
public ModuleActionContextRegistry build() throws AbruptExitException {
HashSet<Class<?>> usedTypes = new HashSet<>();
MutableClassToInstanceMap<ActionContext> contextToInstance =
MutableClassToInstanceMap.create();
for (ActionContextInformation<?> actionContextInformation : actionContexts) {
Class<? extends ActionContext> identifyingType = actionContextInformation.identifyingType();
if (typeToRestriction.containsKey(identifyingType)) {
String restriction = typeToRestriction.get(identifyingType);
if (!actionContextInformation.commandLineIdentifiers().contains(restriction)
&& !restriction.isEmpty()) {
continue;
}
}
usedTypes.add(identifyingType);
actionContextInformation.addToMap(contextToInstance);
}
Sets.SetView<Class<?>> unusedRestrictions =
Sets.difference(typeToRestriction.keySet(), usedTypes);
if (!unusedRestrictions.isEmpty()) {
throw new AbruptExitException(
DetailedExitCode.of(
FailureDetail.newBuilder()
.setMessage(getMissingIdentifierErrorMessage(unusedRestrictions).toString())
.setExecutionOptions(
FailureDetails.ExecutionOptions.newBuilder()
.setCode(Code.RESTRICTION_UNMATCHED_TO_ACTION_CONTEXT))
.build()));
}
return new ModuleActionContextRegistry(ImmutableClassToInstanceMap.copyOf(contextToInstance));
}
@Test
public void givenClassToInstanceMap_whenPutCalled_returnPreviousElementUpperBound() {
ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
map.put(Save.class, new Save());
// Put again to get previous value returned
Action action = map.put(Save.class, new Save());
assertTrue(action instanceof Save);
// Use putInstance to avoid casting
Save save = map.putInstance(Save.class, new Save());
}
private ClassToInstanceMap<Annotation> toMap(Collection<? extends Annotation> annotations) {
ClassToInstanceMap<Annotation> annotationMap = MutableClassToInstanceMap.create();
for (Annotation annotation : annotations) {
if (annotation == null) {
continue;
}
annotationMap.put(annotation.annotationType(), annotation);
}
return annotationMap;
}
void init(Set<? extends TypeElement> annotations, RoundEnvironment round, ProcessingEnvironment processing) {
this.components = MutableClassToInstanceMap.create();
this.processing = processing;
this.round = round;
this.annotations = ImmutableSet.copyOf(annotations);
this.initialized = true;
}
public ServiceRegistry() {
this.services=MutableClassToInstanceMap.<Service>create();
this.builders=new LinkedHashMap<Class<?>, ServiceBuilder<?>>();
this.delegate=RuntimeDelegate.getInstance();
}
private TemplateManagerBuilder() {
this.handlerClasses=Lists.newArrayList();
this.handlers=MutableClassToInstanceMap.<ResourceHandler>create();
}
@Test
public void whenCreateCalled_thenCreateEmptyMutableMap() {
ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
assertTrue(map.isEmpty());
}