use of com.google.common.base.Optional in project bazel by bazelbuild.
the class LocalRepositoryLookupFunction method compute.
// Implementation note: Although LocalRepositoryLookupValue.NOT_FOUND exists, it should never be
// returned from this method.
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException {
RootedPath directory = (RootedPath) skyKey.argument();
// before this can be called if that is incorrect.
if (directory.getRelativePath().equals(PathFragment.EMPTY_FRAGMENT)) {
return LocalRepositoryLookupValue.mainRepository();
}
// Does this directory contain a WORKSPACE file?
Optional<Boolean> maybeWorkspaceFileExists = maybeGetWorkspaceFileExistence(env, directory);
if (!maybeWorkspaceFileExists.isPresent()) {
return null;
} else if (maybeWorkspaceFileExists.get()) {
Optional<LocalRepositoryLookupValue> maybeRepository = maybeCheckWorkspaceForRepository(env, directory);
if (!maybeRepository.isPresent()) {
return null;
}
LocalRepositoryLookupValue repository = maybeRepository.get();
// If the repository that was discovered doesn't exist, continue recursing.
if (repository.exists()) {
return repository;
}
}
// If we haven't found a repository yet, check the parent directory.
RootedPath parentDirectory = RootedPath.toRootedPath(directory.getRoot(), directory.getRelativePath().getParentDirectory());
return env.getValue(LocalRepositoryLookupValue.key(parentDirectory));
}
use of com.google.common.base.Optional in project bazel by bazelbuild.
the class AndroidResourceClassWriter method assignTypeIdsForPublic.
private Map<ResourceType, Integer> assignTypeIdsForPublic() {
Map<ResourceType, Integer> allocatedTypeIds = new EnumMap<>(ResourceType.class);
if (publicIds.isEmpty()) {
return allocatedTypeIds;
}
// Keep track of the reverse mapping from Int -> Type for validation.
Map<Integer, ResourceType> assignedIds = new HashMap<>();
for (Map.Entry<ResourceType, SortedMap<String, Optional<Integer>>> publicTypeEntry : publicIds.entrySet()) {
ResourceType currentType = publicTypeEntry.getKey();
Integer reservedTypeSlot = null;
String previousResource = null;
for (Map.Entry<String, Optional<Integer>> publicEntry : publicTypeEntry.getValue().entrySet()) {
Optional<Integer> reservedId = publicEntry.getValue();
if (!reservedId.isPresent()) {
continue;
}
Integer typePortion = extractTypeId(reservedId.get());
if (reservedTypeSlot == null) {
reservedTypeSlot = typePortion;
previousResource = publicEntry.getKey();
} else {
if (!reservedTypeSlot.equals(typePortion)) {
logger.warning(String.format("%s has conflicting type codes for its public identifiers (%s=%s vs %s=%s)", currentType.getName(), previousResource, reservedTypeSlot, publicEntry.getKey(), typePortion));
}
}
}
if (currentType == ResourceType.ATTR && reservedTypeSlot != null && !reservedTypeSlot.equals(ATTR_TYPE_ID)) {
logger.warning(String.format("Cannot force ATTR to have type code other than 0x%02x (got 0x%02x from %s)", ATTR_TYPE_ID, reservedTypeSlot, previousResource));
}
allocatedTypeIds.put(currentType, reservedTypeSlot);
ResourceType alreadyAssigned = assignedIds.put(reservedTypeSlot, currentType);
if (alreadyAssigned != null) {
logger.warning(String.format("Multiple type names declared for public type identifier 0x%x (%s vs %s)", reservedTypeSlot, alreadyAssigned, currentType));
}
}
return allocatedTypeIds;
}
use of com.google.common.base.Optional in project auto by google.
the class BasicAnnotationProcessor method validElements.
/**
* Returns the valid annotated elements contained in all of the deferred elements. If none are
* found for a deferred element, defers it again.
*/
private ImmutableSetMultimap<Class<? extends Annotation>, Element> validElements(ImmutableMap<String, Optional<? extends Element>> deferredElements, RoundEnvironment roundEnv) {
ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> deferredElementsByAnnotationBuilder = ImmutableSetMultimap.builder();
for (Entry<String, Optional<? extends Element>> deferredTypeElementEntry : deferredElements.entrySet()) {
Optional<? extends Element> deferredElement = deferredTypeElementEntry.getValue();
if (deferredElement.isPresent()) {
findAnnotatedElements(deferredElement.get(), getSupportedAnnotationClasses(), deferredElementsByAnnotationBuilder);
} else {
deferredElementNames.add(ElementName.forTypeName(deferredTypeElementEntry.getKey()));
}
}
ImmutableSetMultimap<Class<? extends Annotation>, Element> deferredElementsByAnnotation = deferredElementsByAnnotationBuilder.build();
ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> validElements = ImmutableSetMultimap.builder();
Set<ElementName> validElementNames = new LinkedHashSet<ElementName>();
// Look at the elements we've found and the new elements from this round and validate them.
for (Class<? extends Annotation> annotationClass : getSupportedAnnotationClasses()) {
// This should just call roundEnv.getElementsAnnotatedWith(Class) directly, but there is a bug
// in some versions of eclipse that cause that method to crash.
TypeElement annotationType = elements.getTypeElement(annotationClass.getCanonicalName());
Set<? extends Element> elementsAnnotatedWith = (annotationType == null) ? ImmutableSet.<Element>of() : roundEnv.getElementsAnnotatedWith(annotationType);
for (Element annotatedElement : Sets.union(elementsAnnotatedWith, deferredElementsByAnnotation.get(annotationClass))) {
if (annotatedElement.getKind().equals(PACKAGE)) {
PackageElement annotatedPackageElement = (PackageElement) annotatedElement;
ElementName annotatedPackageName = ElementName.forPackageName(annotatedPackageElement.getQualifiedName().toString());
boolean validPackage = validElementNames.contains(annotatedPackageName) || (!deferredElementNames.contains(annotatedPackageName) && validateElement(annotatedPackageElement));
if (validPackage) {
validElements.put(annotationClass, annotatedPackageElement);
validElementNames.add(annotatedPackageName);
} else {
deferredElementNames.add(annotatedPackageName);
}
} else {
TypeElement enclosingType = getEnclosingType(annotatedElement);
ElementName enclosingTypeName = ElementName.forTypeName(enclosingType.getQualifiedName().toString());
boolean validEnclosingType = validElementNames.contains(enclosingTypeName) || (!deferredElementNames.contains(enclosingTypeName) && validateElement(enclosingType));
if (validEnclosingType) {
validElements.put(annotationClass, annotatedElement);
validElementNames.add(enclosingTypeName);
} else {
deferredElementNames.add(enclosingTypeName);
}
}
}
}
return validElements.build();
}
use of com.google.common.base.Optional in project hadoop by apache.
the class TestReconfiguration method testAsyncReconfigure.
@Test
public void testAsyncReconfigure() throws ReconfigurationException, IOException, InterruptedException {
AsyncReconfigurableDummy dummy = spy(new AsyncReconfigurableDummy(conf1));
List<PropertyChange> changes = Lists.newArrayList();
changes.add(new PropertyChange("name1", "new1", "old1"));
changes.add(new PropertyChange("name2", "new2", "old2"));
changes.add(new PropertyChange("name3", "new3", "old3"));
doReturn(changes).when(dummy).getChangedProperties(any(Configuration.class), any(Configuration.class));
doReturn(true).when(dummy).isPropertyReconfigurable(eq("name1"));
doReturn(false).when(dummy).isPropertyReconfigurable(eq("name2"));
doReturn(true).when(dummy).isPropertyReconfigurable(eq("name3"));
doReturn("dummy").when(dummy).reconfigurePropertyImpl(eq("name1"), anyString());
doReturn("dummy").when(dummy).reconfigurePropertyImpl(eq("name2"), anyString());
doThrow(new ReconfigurationException("NAME3", "NEW3", "OLD3", new IOException("io exception"))).when(dummy).reconfigurePropertyImpl(eq("name3"), anyString());
dummy.startReconfigurationTask();
waitAsyncReconfigureTaskFinish(dummy);
ReconfigurationTaskStatus status = dummy.getReconfigurationTaskStatus();
assertEquals(2, status.getStatus().size());
for (Map.Entry<PropertyChange, Optional<String>> result : status.getStatus().entrySet()) {
PropertyChange change = result.getKey();
if (change.prop.equals("name1")) {
assertFalse(result.getValue().isPresent());
} else if (change.prop.equals("name2")) {
assertThat(result.getValue().get(), containsString("Property name2 is not reconfigurable"));
} else if (change.prop.equals("name3")) {
assertThat(result.getValue().get(), containsString("io exception"));
} else {
fail("Unknown property: " + change.prop);
}
}
}
use of com.google.common.base.Optional in project core-java by SpineEventEngine.
the class MessageRouting method doRoute.
/**
* Sets a custom route for the passed message class.
*
* <p>Such a mapping may be required when...
* <ul>
* <li>A message should be matched to more than one entity.
* <li>The type of an message producer ID (stored in the message context) differs from the
* type of entity identifiers.
* </ul>
*
* <p>If there is no specific route for the class of the passed message, the routing will use
* the {@linkplain #getDefault() default route}.
*
* @param messageClass the class of messages to route
* @param via the instance of the route to be used
* @param <M> the type of the message
* @throws IllegalStateException if the route for this message class is already set
*/
<M extends Message> MessageRouting<C, K, R> doRoute(Class<M> messageClass, Route<Message, C, R> via) throws IllegalStateException {
checkNotNull(messageClass);
checkNotNull(via);
final Optional route = doGet(messageClass);
if (route.isPresent()) {
throw newIllegalStateException("The route for the message class %s already set. " + "Please remove the route (%s) before setting new route.", messageClass.getName(), route.get());
}
final K cls = toMessageClass(messageClass);
routes.put(cls, via);
return this;
}
Aggregations