import org.sonarqube.ws.WsBatch.WsProjectResponse;
import org.sonarqube.ws.WsBatch.WsProjectResponse.FileData.Builder;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
private static WsProjectResponse buildResponse(ProjectRepositories data) {
WsProjectResponse.Builder response = WsProjectResponse.newBuilder();
- setLastAnalysisDate(response, data);
+ setNullable(data.lastAnalysisDate(), response::setLastAnalysisDate, Date::getTime);
response.setTimestamp(data.timestamp());
response.getMutableFileDataByModuleAndPath()
.putAll(buildFileDataByModuleAndPath(data));
return response.build();
}
- private static void setLastAnalysisDate(WsProjectResponse.Builder response, ProjectRepositories data) {
- Date lastAnalysisDate = data.lastAnalysisDate();
- if (lastAnalysisDate != null) {
- response.setLastAnalysisDate(lastAnalysisDate.getTime());
- }
- }
-
private static Map<String, WsProjectResponse.FileDataByPath> buildFileDataByModuleAndPath(ProjectRepositories data) {
Map<String, WsProjectResponse.FileDataByPath> fileDataByModuleAndPathResponse = new HashMap<>();
for (Map.Entry<String, Map<String, FileData>> moduleAndFileDataByPathEntry : data.fileDataByModuleAndPath().entrySet()) {
private static WsProjectResponse.FileData toFileDataResponse(FileData fileData) {
Builder fileDataBuilder = WsProjectResponse.FileData.newBuilder();
- if (fileData.hash() != null) {
- fileDataBuilder.setHash(fileData.hash());
- }
- if (fileData.revision() != null) {
- fileDataBuilder.setRevision(fileData.revision());
- }
-
+ setNullable(fileData.hash(), fileDataBuilder::setHash);
+ setNullable(fileData.revision(), fileDataBuilder::setRevision);
return fileDataBuilder.build();
}
}
import org.sonar.db.component.ComponentDto;
import org.sonarqube.ws.WsComponents;
-import static com.google.common.base.Strings.isNullOrEmpty;
+import static com.google.common.base.Strings.emptyToNull;
+import static org.sonar.core.util.Protobuf.setNullable;
class ComponentDtoToWsComponent {
private ComponentDtoToWsComponent() {
.setKey(dto.key())
.setName(dto.name())
.setQualifier(dto.qualifier());
- if (!isNullOrEmpty(dto.path())) {
- wsComponent.setPath(dto.path());
- }
- if (!isNullOrEmpty(dto.description())) {
- wsComponent.setDescription(dto.description());
- }
- if (!isNullOrEmpty(dto.language())) {
- wsComponent.setLanguage(dto.language());
- }
-
+ setNullable(emptyToNull(dto.path()), wsComponent::setPath);
+ setNullable(emptyToNull(dto.description()), wsComponent::setDescription);
+ setNullable(emptyToNull(dto.language()), wsComponent::setLanguage);
return wsComponent;
}
import org.sonarqube.ws.client.component.SearchWsRequest;
import static com.google.common.collect.FluentIterable.from;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
.setKey(dto.key())
.setName(dto.name())
.setQualifier(dto.qualifier());
- if (dto.language() != null) {
- builder.setLanguage(dto.language());
- }
-
+ setNullable(dto.language(), builder::setLanguage);
return builder.build();
}
}
.map(bucketToFacetValue)
.forEach(wsFacet::addValues);
} else {
- wsFacet.addAllValues(Collections.<Common.FacetValue>emptyList());
+ wsFacet.addAllValues(Collections.emptyList());
}
return wsFacet.build();
import org.sonarqube.ws.Common;
import org.sonarqube.ws.Issues;
+import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Strings.nullToEmpty;
+import static org.sonar.core.util.Protobuf.setNullable;
public class SearchResponseFormat {
private void formatIssue(Issues.Issue.Builder issueBuilder, IssueDto dto, SearchResponseData data) {
issueBuilder.setKey(dto.getKey());
- Common.RuleType type = Common.RuleType.valueOf(dto.getType());
- if (type != null) {
- issueBuilder.setType(type);
- }
+ setNullable(dto.getType(), issueBuilder::setType, Common.RuleType::valueOf);
+
ComponentDto component = data.getComponentByUuid(dto.getComponentUuid());
issueBuilder.setComponent(component.key());
// Only used for the compatibility with the Java WS Client <= 4.4 used by Eclipse
}
issueBuilder.setRule(dto.getRuleKey().toString());
issueBuilder.setSeverity(Common.Severity.valueOf(dto.getSeverity()));
- if (!Strings.isNullOrEmpty(dto.getAssignee())) {
- issueBuilder.setAssignee(dto.getAssignee());
- }
- if (!Strings.isNullOrEmpty(dto.getResolution())) {
- issueBuilder.setResolution(dto.getResolution());
- }
+ setNullable(emptyToNull(dto.getAssignee()), issueBuilder::setAssignee);
+ setNullable(emptyToNull(dto.getResolution()), issueBuilder::setResolution);
issueBuilder.setStatus(dto.getStatus());
issueBuilder.setMessage(nullToEmpty(dto.getMessage()));
issueBuilder.addAllTags(dto.getTags());
issueBuilder.setDebt(effortValue);
issueBuilder.setEffort(effortValue);
}
- Integer line = dto.getLine();
- if (line != null) {
- issueBuilder.setLine(line);
- }
+ setNullable(dto.getLine(), issueBuilder::setLine);
completeIssueLocations(dto, issueBuilder);
issueBuilder.setAuthor(nullToEmpty(dto.getAuthorLogin()));
- Date date = dto.getIssueCreationDate();
- if (date != null) {
- issueBuilder.setCreationDate(DateUtils.formatDateTime(date));
- }
- date = dto.getIssueUpdateDate();
- if (date != null) {
- issueBuilder.setUpdateDate(DateUtils.formatDateTime(date));
- }
- date = dto.getIssueCloseDate();
- if (date != null) {
- issueBuilder.setCloseDate(DateUtils.formatDateTime(date));
- }
+ setNullable(dto.getIssueCreationDate(), issueBuilder::setCreationDate, DateUtils::formatDateTime);
+ setNullable(dto.getIssueUpdateDate(), issueBuilder::setUpdateDate, DateUtils::formatDateTime);
+ setNullable(dto.getIssueCloseDate(), issueBuilder::setCloseDate, DateUtils::formatDateTime);
}
private void completeIssueLocations(IssueDto dto, Issues.Issue.Builder issueBuilder) {
DbIssues.Locations locations = dto.parseLocations();
- if (locations != null) {
- if (locations.hasTextRange()) {
- DbCommons.TextRange textRange = locations.getTextRange();
- issueBuilder.setTextRange(convertTextRange(textRange));
- }
- for (DbIssues.Flow flow : locations.getFlowList()) {
- Issues.Flow.Builder targetFlow = Issues.Flow.newBuilder();
- for (DbIssues.Location flowLocation : flow.getLocationList()) {
- targetFlow.addLocations(convertLocation(flowLocation));
- }
- issueBuilder.addFlows(targetFlow);
+ if (locations == null) {
+ return;
+ }
+ if (locations.hasTextRange()) {
+ DbCommons.TextRange textRange = locations.getTextRange();
+ issueBuilder.setTextRange(convertTextRange(textRange));
+ }
+ for (DbIssues.Flow flow : locations.getFlowList()) {
+ Issues.Flow.Builder targetFlow = Issues.Flow.newBuilder();
+ for (DbIssues.Location flowLocation : flow.getLocationList()) {
+ targetFlow.addLocations(convertLocation(flowLocation));
}
+ issueBuilder.addFlows(targetFlow);
}
}
// On a root project, parentProjectId is null but projectId is equal to itself, which make no sense.
if (!uuid.equals(dto.getRootUuid())) {
ComponentDto project = data.getComponentByUuid(dto.projectUuid());
- if (project != null) {
- builder.setProjectId(project.getId());
- }
+ setNullable(project, builder::setProjectId, ComponentDto::getId);
+
ComponentDto subProject = data.getComponentByUuid(dto.getRootUuid());
- if (subProject != null) {
- builder.setSubProjectId(subProject.getId());
- }
+ setNullable(subProject, builder::setSubProjectId, ComponentDto::getId);
}
result.add(builder.build());
}
valueBuilder.build();
}
} else {
- wsFacet.addAllValues(Collections.<Common.FacetValue>emptyList());
+ wsFacet.addAllValues(Collections.emptyList());
}
wsFacets.addFacets(wsFacet);
}
import org.sonarqube.ws.Organizations;
import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.core.util.Protobuf.setNullable;
/**
* Factorizes code and constants between Organization WS's actions.
.clear()
.setName(dto.getName())
.setKey(dto.getKey());
- if (dto.getDescription() != null) {
- builder.setDescription(dto.getDescription());
- }
- if (dto.getUrl() != null) {
- builder.setUrl(dto.getUrl());
- }
- if (dto.getAvatarUrl() != null) {
- builder.setAvatar(dto.getAvatarUrl());
- }
+ setNullable(dto.getDescription(), builder::setDescription);
+ setNullable(dto.getUrl(), builder::setUrl);
+ setNullable(dto.getAvatarUrl(), builder::setAvatar);
return builder.build();
}
}
import org.sonarqube.ws.WsPermissions.WsGroupsResponse;
import static java.util.Collections.emptyList;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
if (group.getId() != 0L) {
wsGroup.setId(String.valueOf(group.getId()));
}
- if (group.getDescription() != null) {
- wsGroup.setDescription(group.getDescription());
- }
+ setNullable(group.getDescription(), wsGroup::setDescription);
wsGroup.addAllPermissions(permissionsByGroupId.get(group.getId()));
});
import org.sonarqube.ws.WsPermissions.UsersWsResponse;
import static java.util.Collections.emptyList;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
WsPermissions.User.Builder userResponse = response.addUsersBuilder()
.setLogin(user.getLogin())
.addAllPermissions(permissionsByUserId.get(user.getId()));
-
- if (user.getEmail() != null) {
- userResponse.setEmail(user.getEmail());
- }
- if (user.getName() != null) {
- userResponse.setName(user.getName());
- }
+ setNullable(user.getEmail(), userResponse::setEmail);
+ setNullable(user.getName(), userResponse::setName);
});
response.getPagingBuilder()
import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonarqube.ws.WsPermissions.PermissionTemplate;
+import static org.sonar.core.util.Protobuf.setNullable;
+
public class PermissionTemplateDtoToPermissionTemplateResponse {
private PermissionTemplateDtoToPermissionTemplateResponse() {
.setName(permissionTemplate.getName())
.setCreatedAt(DateUtils.formatDateTime(permissionTemplate.getCreatedAt()))
.setUpdatedAt(DateUtils.formatDateTime(permissionTemplate.getUpdatedAt()));
- if (permissionTemplate.getDescription() != null) {
- permissionTemplateBuilder.setDescription(permissionTemplate.getDescription());
- }
- if (permissionTemplate.getKeyPattern() != null) {
- permissionTemplateBuilder.setProjectKeyPattern(permissionTemplate.getKeyPattern());
- }
+ setNullable(permissionTemplate.getDescription(), permissionTemplateBuilder::setDescription);
+ setNullable(permissionTemplate.getKeyPattern(), permissionTemplateBuilder::setProjectKeyPattern);
return permissionTemplateBuilder.build();
}
}
import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
.setName(templateDto.getName())
.setCreatedAt(formatDateTime(templateDto.getCreatedAt()))
.setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
- if (templateDto.getKeyPattern() != null) {
- templateBuilder.setProjectKeyPattern(templateDto.getKeyPattern());
- }
- if (templateDto.getDescription() != null) {
- templateBuilder.setDescription(templateDto.getDescription());
- }
+ setNullable(templateDto.getKeyPattern(), templateBuilder::setProjectKeyPattern);
+ setNullable(templateDto.getDescription(), templateBuilder::setDescription);
for (String permission : ProjectPermissions.ALL) {
templateBuilder.addPermissions(
permissionResponse
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
if (group.getId() != 0L) {
wsGroup.setId(String.valueOf(group.getId()));
}
- if (group.getDescription() != null) {
- wsGroup.setDescription(group.getDescription());
- }
+ setNullable(group.getDescription(), wsGroup::setDescription);
wsGroup.addAllPermissions(permissionsByGroupId.get(group.getId()));
});
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
WsPermissions.User.Builder userResponse = responseBuilder.addUsersBuilder()
.setLogin(user.getLogin())
.addAllPermissions(permissionsByUserId.get(user.getId()));
- if (user.getEmail() != null) {
- userResponse.setEmail(user.getEmail());
- }
- if (user.getName() != null) {
- userResponse.setName(user.getName());
- }
+ setNullable(user.getEmail(), userResponse::setEmail);
+ setNullable(user.getName(), userResponse::setName);
});
responseBuilder.getPagingBuilder()
.setPageIndex(paging.pageIndex())
import org.sonarqube.ws.WsProjects.SearchMyProjectsWsResponse.Project;
import org.sonarqube.ws.client.project.SearchMyProjectsRequest;
+import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
if (qualityGate.isPresent()) {
project.setQualityGate(qualityGate.get());
}
- if (!isNullOrEmpty(dto.description())) {
- project.setDescription(dto.description());
- }
+ setNullable(emptyToNull(dto.description()), project::setDescription);
data.projectLinksFor(dto.uuid()).stream()
.map(ProjectLinkDtoToWs.INSTANCE)
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.function.Function;
+import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
/**
IOUtils.closeQuietly(input);
}
}
+
+ /**
+ * Call a setter method of {@link com.google.protobuf.GeneratedMessage.Builder} if the parameter
+ * is not null. Do nothing if parameter is null.
+ * <br/>
+ * This utility method is convenient as the setter methods of Protobuf 2 do not accept
+ * {@code null} parameters. It avoid increasing complexity with "if not null" conditions.
+ * <br/>
+ * Example:
+ * <pre>
+ * setNullable(dto.getLine(), issueBuilder::setLine);
+ * </pre>
+ */
+ public static <PARAM> void setNullable(@Nullable PARAM parameter, Function<PARAM, ?> setter) {
+ if (parameter != null) {
+ setter.apply(parameter);
+ }
+ }
+
+ /**
+ * Same as {@link #setNullable(Object, Function)} but the parameter is converted by the function "{@code paramConverter}"
+ * before being applied to setter. If the converter returns {@code null}, then setter method
+ * is not called.
+ * <br/>
+ * Example:
+ * <pre>
+ * setNullable(dto.getIssueCreationDate(), issueBuilder::setCreationDate, DateUtils::formatDateTime);
+ * </pre>
+ * @see #setNullable(Object, Function)
+ */
+ public static <PARAM, PARAM2> void setNullable(@Nullable PARAM param, Function<PARAM2, ?> setter,
+ Function<PARAM, PARAM2> paramConverter) {
+ if (param != null) {
+ PARAM2 output = paramConverter.apply(param);
+ setNullable(output, setter);
+ }
+ }
}