use of java.util.Optional in project buck by facebook.
the class ConditionalStepTest method testExecuteConditionalStepWhenTrue.
@Test
public void testExecuteConditionalStepWhenTrue() throws IOException, InterruptedException {
// Create a Supplier<Boolean> that returns a value once an external condition has been
// satisfied.
final AtomicReference<Optional<Boolean>> condition = new AtomicReference<>(Optional.empty());
Supplier<Boolean> conditional = () -> {
if (!condition.get().isPresent()) {
throw new IllegalStateException("condition must be satisfied before this is queried.");
} else {
return condition.get().get();
}
};
// Create a step to run when the Supplier<Boolean> is true. Exit code is always 37.
final AtomicInteger numCalls = new AtomicInteger(0);
Step stepToRunWhenSupplierIsTrue = new AbstractExecutionStep("inc") {
@Override
public StepExecutionResult execute(ExecutionContext context) {
numCalls.incrementAndGet();
return StepExecutionResult.of(37);
}
};
// Create and execute the ConditionalStep.
ConditionalStep conditionalStep = new ConditionalStep(conditional, stepToRunWhenSupplierIsTrue);
condition.set(Optional.of(true));
ExecutionContext context = TestExecutionContext.newInstance();
int exitCode = conditionalStep.execute(context).getExitCode();
assertEquals("stepToRunWhenSupplierIsTrue should have been run once.", 1, numCalls.get());
assertEquals("The exit code of stepToRunWhenSupplierIsTrue should be passed through.", 37, exitCode);
assertEquals("inc", conditionalStep.getShortName());
assertEquals("conditionally: inc", conditionalStep.getDescription(context));
}
use of java.util.Optional in project buck by facebook.
the class ConditionalStepTest method testExecuteConditionalStepWhenFalse.
@Test
public void testExecuteConditionalStepWhenFalse() throws IOException, InterruptedException {
// Create a Supplier<Boolean> that returns a value once an external condition has been
// satisfied.
final AtomicReference<Optional<Boolean>> condition = new AtomicReference<>(Optional.empty());
Supplier<Boolean> conditional = () -> {
if (!condition.get().isPresent()) {
throw new IllegalStateException("condition must be satisfied before this is queried.");
} else {
return condition.get().get();
}
};
// Create a step to run when the Supplier<Boolean> is true. Because the Supplier<Boolean> will
// be false, this step should not be run.
final AtomicInteger numCalls = new AtomicInteger(0);
Step stepToRunWhenSupplierIsTrue = new AbstractExecutionStep("inc") {
@Override
public StepExecutionResult execute(ExecutionContext context) {
numCalls.incrementAndGet();
throw new IllegalStateException("This step should not be executed.");
}
};
// Create and execute the ConditionalStep.
ConditionalStep conditionalStep = new ConditionalStep(conditional, stepToRunWhenSupplierIsTrue);
condition.set(Optional.of(false));
ExecutionContext context = TestExecutionContext.newInstance();
int exitCode = conditionalStep.execute(context).getExitCode();
assertEquals("stepToRunWhenSupplierIsTrue should not have been run.", 0, numCalls.get());
assertEquals("The exit code should be zero when the conditional is false.", 0, exitCode);
assertEquals("inc", conditionalStep.getShortName());
assertEquals("conditionally: inc", conditionalStep.getDescription(context));
}
use of java.util.Optional in project sonarqube by SonarSource.
the class BulkDeleteAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
userSession.checkLoggedIn();
List<String> uuids = request.paramAsStrings(PARAM_PROJECT_IDS);
List<String> keys = request.paramAsStrings(PARAM_PROJECTS);
String orgKey = request.param(ProjectsWsSupport.PARAM_ORGANIZATION);
try (DbSession dbSession = dbClient.openSession(false)) {
Optional<OrganizationDto> org = loadOrganizationByKey(dbSession, orgKey);
List<ComponentDto> projects = searchProjects(dbSession, uuids, keys);
projects.stream().filter(p -> !org.isPresent() || org.get().getUuid().equals(p.getOrganizationUuid())).forEach(p -> componentCleanerService.delete(dbSession, p));
}
response.noContent();
}
use of java.util.Optional in project sonarqube by SonarSource.
the class UserUpdater method addDefaultGroup.
private void addDefaultGroup(DbSession dbSession, UserDto userDto) {
String defaultGroupName = settings.getString(CoreProperties.CORE_DEFAULT_GROUP);
if (defaultGroupName == null) {
return;
}
String defOrgUuid = defaultOrganizationProvider.get().getUuid();
List<GroupDto> userGroups = dbClient.groupDao().selectByUserLogin(dbSession, userDto.getLogin());
if (!userGroups.stream().anyMatch(g -> defOrgUuid.equals(g.getOrganizationUuid()) && g.getName().equals(defaultGroupName))) {
Optional<GroupDto> groupDto = dbClient.groupDao().selectByName(dbSession, defOrgUuid, defaultGroupName);
if (!groupDto.isPresent()) {
throw new ServerException(HttpURLConnection.HTTP_INTERNAL_ERROR, format("The default group '%s' for new users does not exist. Please update the general security settings to fix this issue.", defaultGroupName));
}
dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setUserId(userDto.getId()).setGroupId(groupDto.get().getId()));
}
}
use of java.util.Optional in project fastjson by alibaba.
the class OptionalCodec method write.
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
if (object == null) {
serializer.writeNull();
return;
}
if (object instanceof Optional) {
Optional<?> optional = (Optional<?>) object;
Object value = optional.isPresent() ? optional.get() : null;
serializer.write(value);
return;
}
if (object instanceof OptionalDouble) {
OptionalDouble optional = (OptionalDouble) object;
if (optional.isPresent()) {
double value = optional.getAsDouble();
serializer.write(value);
} else {
serializer.writeNull();
}
return;
}
if (object instanceof OptionalInt) {
OptionalInt optional = (OptionalInt) object;
if (optional.isPresent()) {
int value = optional.getAsInt();
serializer.out.writeInt(value);
} else {
serializer.writeNull();
}
return;
}
if (object instanceof OptionalLong) {
OptionalLong optional = (OptionalLong) object;
if (optional.isPresent()) {
long value = optional.getAsLong();
serializer.out.writeLong(value);
} else {
serializer.writeNull();
}
return;
}
throw new JSONException("not support optional : " + object.getClass());
}
Aggregations