Example usage for org.apache.commons.lang ObjectUtils defaultIfNull

List of usage examples for org.apache.commons.lang ObjectUtils defaultIfNull

Introduction

In this page you can find the example usage for org.apache.commons.lang ObjectUtils defaultIfNull.

Prototype

public static <T> T defaultIfNull(T object, T defaultValue) 

Source Link

Document

Returns a default value if the object passed is null.

 ObjectUtils.defaultIfNull(null, null)      = null ObjectUtils.defaultIfNull(null, "")        = "" ObjectUtils.defaultIfNull(null, "zz")      = "zz" ObjectUtils.defaultIfNull("abc", *)        = "abc" ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE 

Usage

From source file:org.sonar.server.qualityprofile.QProfileBackuper.java

/**
 * @param reader     the XML backup// ww w . j ava2  s  .c  o m
 * @param toProfileName the target profile. If <code>null</code>, then use the
 *                   lang and name declared in the backup
 */
public BulkChangeResult restore(Reader reader, @Nullable QProfileName toProfileName) {
    try {
        String profileLang = null;
        String profileName = null;
        List<RuleActivation> ruleActivations = Lists.newArrayList();
        SMInputFactory inputFactory = initStax();
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        rootC.advance(); // <profile>
        if (!rootC.getLocalName().equals("profile")) {
            throw new IllegalArgumentException("Backup XML is not valid. Root element must be <profile>.");
        }
        SMInputCursor cursor = rootC.childElementCursor();
        while (cursor.getNext() != null) {
            String nodeName = cursor.getLocalName();
            if (StringUtils.equals("name", nodeName)) {
                profileName = StringUtils.trim(cursor.collectDescendantText(false));

            } else if (StringUtils.equals("language", nodeName)) {
                profileLang = StringUtils.trim(cursor.collectDescendantText(false));

            } else if (StringUtils.equals("rules", nodeName)) {
                SMInputCursor rulesCursor = cursor.childElementCursor("rule");
                ruleActivations = parseRuleActivations(rulesCursor);
            }
        }

        QProfileName target = (QProfileName) ObjectUtils.defaultIfNull(toProfileName,
                new QProfileName(profileLang, profileName));
        return reset.reset(target, ruleActivations);
    } catch (XMLStreamException e) {
        throw new IllegalStateException("Fail to restore Quality profile backup", e);
    }
}

From source file:org.sonar.server.source.SourceService.java

/**
 * Raw lines of source file./*w w w .j  av a2 s.c  o  m*/
 */
public List<String> getLinesAsTxt(String fileUuid, @Nullable Integer fromParam, @Nullable Integer toParam) {
    int from = (Integer) ObjectUtils.defaultIfNull(fromParam, 1);
    int to = (Integer) ObjectUtils.defaultIfNull(toParam, Integer.MAX_VALUE);
    List<String> lines = Lists.newArrayList();
    for (SourceLineDoc lineDoc : sourceLineIndex.getLines(fileUuid, from, to)) {
        lines.add(lineDoc.source());
    }
    return lines;
}

From source file:org.sonar.server.source.SourceService.java

/**
 * Decorated lines of source file.//ww  w .java 2 s .  co m
 */
public List<String> getLinesAsHtml(String fileUuid, @Nullable Integer fromParam, @Nullable Integer toParam) {
    int from = (Integer) ObjectUtils.defaultIfNull(fromParam, 1);
    int to = (Integer) ObjectUtils.defaultIfNull(toParam, Integer.MAX_VALUE);
    List<String> lines = Lists.newArrayList();
    for (SourceLineDoc lineDoc : sourceLineIndex.getLines(fileUuid, from, to)) {
        lines.add(sourceDecorator.getDecoratedSourceAsHtml(lineDoc.source(), lineDoc.highlighting(),
                lineDoc.symbols()));
    }
    return lines;
}

From source file:org.sonar.server.source.ws.LinesAction.java

@Override
public void handle(Request request, Response response) {
    String fileUuid = request.mandatoryParam("uuid");
    ComponentDto component = componentService.getByUuid(fileUuid);
    UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, component.key());

    int from = Math.max(request.mandatoryParamAsInt("from"), 1);
    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);

    List<SourceLineDoc> sourceLines = sourceLineIndex.getLines(fileUuid, from, to);
    if (sourceLines.isEmpty()) {
        throw new NotFoundException("File '" + fileUuid + "' has no sources");
    }/*from   w  ww .  jav  a  2 s. co m*/

    JsonWriter json = response.newJsonWriter().beginObject();
    writeSource(sourceLines, from, json);

    json.endObject().close();
}

From source file:org.sonar.server.source.ws.ScmAction.java

@Override
public void handle(Request request, Response response) {
    String fileKey = request.mandatoryParam("key");
    int from = Math.max(request.mandatoryParamAsInt("from"), 1);
    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
    boolean commitsByLine = request.mandatoryParamAsBoolean("commits_by_line");

    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
        userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
        Iterable<DbFileSources.Line> sourceLines = checkFoundWithOptional(
                sourceService.getLines(dbSession, file.uuid(), from, to), "File '%s' has no sources", fileKey);
        JsonWriter json = response.newJsonWriter().beginObject();
        writeSource(sourceLines, commitsByLine, json);
        json.endObject().close();/*from  ww  w. ja va  2 s  .  co  m*/
    }
}

From source file:org.sonar.server.source.ws.ShowAction.java

@Override
public void handle(Request request, Response response) {
    String fileKey = request.mandatoryParam("key");
    int from = Math.max(request.paramAsInt("from"), 1);
    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);

    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
        userSession.checkComponentPermission(UserRole.CODEVIEWER, file);

        Iterable<String> linesHtml = checkFoundWithOptional(
                sourceService.getLinesAsHtml(dbSession, file.uuid(), from, to), "No source found for file '%s'",
                fileKey);/* ww  w.ja v a 2 s  . c  o  m*/
        JsonWriter json = response.newJsonWriter().beginObject();
        writeSource(linesHtml, from, json);
        json.endObject().close();

    }
}

From source file:org.sonar.server.test.ws.CoverageShowAction.java

@Override
public void handle(Request request, Response response) {
    String fileKey = request.mandatoryParam(KEY);
    coverageService.checkPermission(fileKey);

    int from = Math.max(request.mandatoryParamAsInt(FROM), 1);
    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt(TO), Integer.MAX_VALUE);
    CoverageService.TYPE type = CoverageService.TYPE.valueOf(request.mandatoryParam(TYPE));

    JsonWriter json = response.newJsonWriter().beginObject();

    Map<Integer, Integer> hits = coverageService.getHits(fileKey, type);
    if (!hits.isEmpty()) {
        Map<Integer, Integer> testCases = coverageService.getTestCases(fileKey, type);
        Map<Integer, Integer> conditions = coverageService.getConditions(fileKey, type);
        Map<Integer, Integer> coveredConditions = coverageService.getCoveredConditions(fileKey, type);
        writeCoverage(hits, testCases, conditions, coveredConditions, from, to, json);
    }/*  ww  w.j  a  va2s. com*/

    json.endObject().close();
}

From source file:org.yes.cart.domain.dto.impl.ProductSearchResultDTOImpl.java

/** {@inheritDoc} */
public String getName(final String locale) {
    return (String) ObjectUtils.defaultIfNull(this.i18NModelName.getValue(locale), name);
}

From source file:org.yes.cart.domain.dto.impl.ProductSearchResultDTOImpl.java

/** {@inheritDoc} */
public String getDescription(final String locale) {
    return (String) ObjectUtils.defaultIfNull(this.i18NModelDescription.getValue(locale), description);
}

From source file:org.yes.cart.web.application.StorefrontApplication.java

/**
 * {@inheritDoc}//www .java 2s  .  c o m
 */
protected void init() {

    enableResourceAccess();

    super.init();

    // dynamic shop markup support via specific resource finder
    getResourceSettings().setResourceFinder(this);
    getResourceSettings().setResourceStreamLocator(new ResourceStreamLocator(this));

    setRequestCycleProvider(this);

    // wicket-groovy dynamic pages support
    //getApplicationSettings().setClassResolver(new GroovyClassResolver(this));

    configureMarkupSettings();

    getComponentInstantiationListeners().add(getSpringComponentInjector());

    getRequestCycleListeners().add(new StorefrontRequestCycleListener());

    mountPages();
    mountResources();

    if ("true".equalsIgnoreCase(getInitParameter("secureMode"))) {

        final HttpsConfig httpsConfig = new HttpsConfig(
                Integer.valueOf((String) ObjectUtils.defaultIfNull(getInitParameter("unsecurePort"), "8080")),
                Integer.valueOf((String) ObjectUtils.defaultIfNull(getInitParameter("securePort"), "8443")));

        final HttpsMapper httpsMapper = new HttpsMapper(getRootRequestMapper(), httpsConfig);

        setRootRequestMapper(httpsMapper);

    }

}