Java Code Examples for org.apache.commons.lang.ObjectUtils#defaultIfNull()

The following examples show how to use org.apache.commons.lang.ObjectUtils#defaultIfNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EBusTelegramParser.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Evaluates the compiled script of a entry.
 * 
 * @param entry The configuration entry to evaluate
 * @param scopeValues All known values for script scope
 * @return The computed value
 * @throws ScriptException
 */
private Object evaluateScript(Entry<String, TelegramValue> entry, Map<String, Object> scopeValues)
        throws ScriptException {

    Object value = null;

    // executes compiled script
    if (entry.getValue().getCsript() != null) {
        CompiledScript cscript = entry.getValue().getCsript();

        // Add global variables thisValue and keyName to JavaScript context
        Bindings bindings = cscript.getEngine().createBindings();
        bindings.putAll(scopeValues);
        value = cscript.eval(bindings);
    }

    // try to convert the returned value to BigDecimal
    value = ObjectUtils.defaultIfNull(NumberUtils.toBigDecimal(value), value);

    // round to two digits, maybe not optimal for any result
    if (value instanceof BigDecimal) {
        ((BigDecimal) value).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    return value;
}
 
Example 2
Source File: InputSocket.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void init(LogFeederProps logFeederProperties) throws Exception {
  super.init(logFeederProperties);
  port = (int) ObjectUtils.defaultIfNull(getInputDescriptor().getPort(), 0);
  if (port == 0) {
    throw new IllegalArgumentException(String.format("Port needs to be set for socket input (type: %s)", getInputDescriptor().getType()));
  }

  protocol = (String) ObjectUtils.defaultIfNull(getInputDescriptor().getProtocol(), "tcp");
  secure = (boolean) ObjectUtils.defaultIfNull(getInputDescriptor().isSecure(), false);
  log4j = (boolean) ObjectUtils.defaultIfNull(getInputDescriptor().isLog4j(), false);
}
 
Example 3
Source File: Profiler.java    From atlas with Apache License 2.0 5 votes vote down vote up
private Entry(Object message, Profiler.Entry parentEntry, Profiler.Entry firstEntry) {
    this.subEntries = new ArrayList(4);
    this.message = message;
    this.startTime = System.currentTimeMillis();
    this.parentEntry = parentEntry;
    this.firstEntry = (Profiler.Entry)ObjectUtils.defaultIfNull(firstEntry, this);
    this.baseTime = firstEntry == null ? 0L : firstEntry.startTime;
}
 
Example 4
Source File: SearchGuardRoles.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public SearchGuardRoles load(Map<String, Object> source) {
    if(source == null) {
        return this;
    }
    
    RolesBuilder builder = new RolesBuilder();

    for (String key : source.keySet()) {
        RoleBuilder roleBuilder = new RoleBuilder(key);

        // get out cluster and indices
        Map<String, Object> role = (Map<String, Object>) source.get(key);

        List<String> cluster = (List<String>) ObjectUtils.defaultIfNull(role.get(CLUSTER_HEADER), Collections.EMPTY_LIST);
        roleBuilder.setClusters(cluster);
        if(role.containsKey(EXPIRES)) {
            roleBuilder.expires((String)role.get(EXPIRES));
        }

        Map<String, Map<String, List<String>>> indices = (Map<String, Map<String, List<String>>>) ObjectUtils
                .defaultIfNull(role.get(INDICES_HEADER), new HashMap<>());

        for (String index : indices.keySet()) {
            for (String type : indices.get(index).keySet()) {
                List<String> actions = indices.get(index).get(type);
                roleBuilder.setActions(index, type, actions);
            }
        }

        builder.addRole(roleBuilder.build());
    }

    addAll(builder.build());
    return this;
}
 
Example 5
Source File: AbstractProductFilter.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * Adapt list of  {@link FilteredNavigationRecord} into Blocks.
 *
 * @param records navigation records
 * @return list of pairs
 */
protected List<Block> adaptNavigationRecords(final List<FilteredNavigationRecord> records) {

    final List<Block> navBlocks = new ArrayList<>();

    String head = StringUtils.EMPTY;
    Block currentBlock = null;

    for (FilteredNavigationRecord navigationRecord : records) {
        if (!navigationRecord.getName().equalsIgnoreCase(head)) {
            currentBlock = new Block(
                    navigationRecord.getCode(),
                    navigationRecord.getTemplate(),
                    (String) ObjectUtils.defaultIfNull(navigationRecord.getDisplayName(), navigationRecord.getName())
            );

            navBlocks.add(currentBlock);
            head = navigationRecord.getName();
        }
        currentBlock.getValues().add(new Value(
                    adaptValueForLinkLabel(navigationRecord.getValue(), navigationRecord.getDisplayValue()),
                    navigationRecord.getCount(),
                    getNavigationParameter(navigationRecord.getCode(), navigationRecord.getValue())
        ));
    }
    return navBlocks;
}
 
Example 6
Source File: MaintenanceUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Determines if there is another maintenance document that has a lock on the same key as the given document, and
 * therefore will block the maintenance document from being submitted
 *
 * @param document - maintenance document instance to check locking for
 * @param throwExceptionIfLocked - indicates if an exception should be thrown in the case of found locking document,
 * if false only an error will be added
 */
public static void checkForLockingDocument(MaintenanceDocument document, final boolean throwExceptionIfLocked) {
    LOG.info("starting checkForLockingDocument (by MaintenanceDocument)");

    // get the docHeaderId of the blocking docs, if any are locked and blocking
    //String blockingDocId = getMaintenanceDocumentService().getLockingDocumentId(document);
    final String blockingDocId = document.getNewMaintainableObject().getLockingDocumentId();

    Maintainable maintainable = (Maintainable) ObjectUtils.defaultIfNull(document.getOldMaintainableObject(), document.getNewMaintainableObject());
    checkDocumentBlockingDocumentId(blockingDocId, throwExceptionIfLocked);
}
 
Example 7
Source File: Project.java    From openshift-elasticsearch-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return "Project [name=" + ObjectUtils.defaultIfNull(name,"<null>") + ", uid=" + ObjectUtils.defaultIfNull(uid,"<null>") + "]";
}