Example usage for org.apache.commons.lang3.math NumberUtils toLong

List of usage examples for org.apache.commons.lang3.math NumberUtils toLong

Introduction

In this page you can find the example usage for org.apache.commons.lang3.math NumberUtils toLong.

Prototype

public static long toLong(final String str, final long defaultValue) 

Source Link

Document

Convert a String to a long, returning a default value if the conversion fails.

If the string is null, the default value is returned.

 NumberUtils.toLong(null, 1L) = 1L NumberUtils.toLong("", 1L)   = 1L NumberUtils.toLong("1", 0L)  = 1L 

Usage

From source file:com.norconex.importer.handler.tagger.impl.DateFormatTagger.java

private String formatDate(String fromDate) {
    if (StringUtils.isBlank(fromDate)) {
        return null;
    }//from   w  w w.j  a  va  2  s .  c om

    //--- Parse from date ---
    Date date = null;
    if (StringUtils.isBlank(fromFormat)) {
        // From date format is EPOCH
        long millis = NumberUtils.toLong(fromDate, -1);
        if (millis == -1) {
            LOG.warn("Invalid date format found in " + fromField
                    + ". When no \"fromFormat\" is specified, the date "
                    + "value is expected to be of EPOCH format.");
            return null;
        }
        date = new Date(millis);
    } else {
        // From date is custom format
        try {
            date = new SimpleDateFormat(fromFormat).parse(fromDate);
        } catch (ParseException e) {
            LOG.warn("Invalid date format found in " + fromField + ".", e);
            return null;
        }
    }

    //--- Format to date ---
    String toDate = null;
    if (StringUtils.isBlank(toFormat)) {
        // To date foramt is EPOCH
        toDate = Long.toString(date.getTime());
    } else {
        toDate = new SimpleDateFormat(toFormat).format(date);
    }
    return toDate;
}

From source file:com.paladin.mvc.RequestContext.java

public long param(String name, long def_value) {
    return NumberUtils.toLong(param(name), def_value);
}

From source file:ch.cyberduck.core.dav.DAVAttributesFeature.java

@Override
public PathAttributes find(final Path file) throws BackgroundException {
    if (file.isRoot()) {
        return PathAttributes.EMPTY;
    }//w ww. jav  a 2 s .  c  o  m
    try {
        try {
            final List<DavResource> status = session.getClient().list(new DAVPathEncoder().encode(file));
            for (final DavResource resource : status) {
                if (resource.isDirectory()) {
                    if (!file.getType().contains(Path.Type.directory)) {
                        throw new NotfoundException(String.format("Path %s is directory", file.getAbsolute()));
                    }
                } else {
                    if (!file.getType().contains(Path.Type.file)) {
                        throw new NotfoundException(String.format("Path %s is file", file.getAbsolute()));
                    }
                }
                final PathAttributes attributes = new PathAttributes();
                if (resource.getModified() != null) {
                    attributes.setModificationDate(resource.getModified().getTime());
                }
                if (resource.getCreation() != null) {
                    attributes.setCreationDate(resource.getCreation().getTime());
                }
                if (resource.getContentLength() != null) {
                    attributes.setSize(resource.getContentLength());
                }
                if (StringUtils.isNotBlank(resource.getEtag())) {
                    attributes.setETag(resource.getEtag());
                    // Setting checksum is disabled. See #8798
                    // attributes.setChecksum(Checksum.parse(resource.getEtag()));
                }
                return attributes;
            }
            throw new NotfoundException(file.getAbsolute());
        } catch (SardineException e) {
            try {
                throw new DAVExceptionMappingService().map("Failure to read attributes of {0}", e, file);
            } catch (InteroperabilityException i) {
                // PROPFIND Method not allowed
                log.warn(String.format("Failure with PROPFIND request for %s. %s", file, i.getMessage()));
                final Map<String, String> headers = session.getClient()
                        .execute(new HttpHead(new DAVPathEncoder().encode(file)), new HeadersResponseHandler());
                final PathAttributes attributes = new PathAttributes();
                try {
                    attributes.setModificationDate(
                            dateParser.parse(headers.get(HttpHeaders.LAST_MODIFIED)).getTime());
                } catch (InvalidDateException p) {
                    log.warn(String.format("%s is not RFC 1123 format %s",
                            headers.get(HttpHeaders.LAST_MODIFIED), p.getMessage()));
                }
                if (!headers.containsKey(HttpHeaders.CONTENT_ENCODING)) {
                    // Set size unless response is compressed
                    attributes.setSize(NumberUtils.toLong(headers.get(HttpHeaders.CONTENT_LENGTH), -1));
                }
                if (headers.containsKey(HttpHeaders.ETAG)) {
                    attributes.setETag(headers.get(HttpHeaders.ETAG));
                    // Setting checksum is disabled. See #8798
                    // attributes.setChecksum(Checksum.parse(headers.get(HttpHeaders.ETAG)));
                }
                if (headers.containsKey(HttpHeaders.CONTENT_MD5)) {
                    attributes.setChecksum(Checksum.parse(headers.get(HttpHeaders.CONTENT_MD5)));
                }
                return attributes;
            }
        }
    } catch (SardineException e) {
        throw new DAVExceptionMappingService().map("Failure to read attributes of {0}", e, file);
    } catch (IOException e) {
        throw new HttpExceptionMappingService().map(e, file);
    }
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.AbstractSyslogParser.java

@Override
public void setProperty(String name, String value) {
    super.setProperty(name, value);

    if (SyslogParserProperties.PROP_SUPPRESS_LEVEL.equalsIgnoreCase(name)) {
        suppressionLevel = NumberUtils.toInt(value, DEFAULT_SUPPRESSION_LEVEL);
        logger().log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "ActivityParser.setting", name, value);
    } else if (SyslogParserProperties.PROP_SUPPRESS_CACHE_SIZE.equalsIgnoreCase(name)) {
        cacheSize = NumberUtils.toLong(value, DEFAULT_MAX_CACHE_SIZE);
        logger().log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "ActivityParser.setting", name, value);
    } else if (SyslogParserProperties.PROP_SUPPRESS_CACHE_EXPIRE.equalsIgnoreCase(name)) {
        cacheExpireDuration = NumberUtils.toLong(value, DEFAULT_CACHE_EXPIRE_DURATION);
        logger().log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "ActivityParser.setting", name, value);
    } else if (SyslogParserProperties.PROP_SUPPRESS_IGNORED_FIELDS.equalsIgnoreCase(name)) {
        if (StringUtils.isNotEmpty(value)) {
            ignoredFields = Arrays.asList(Utils.splitValue(value));
            logger().log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                    "ActivityParser.setting", name, value);
        }//from  w  w w. j av  a  2s  . co  m
    } else if (SyslogParserProperties.PROP_FLATTEN_STRUCTURED_DATA.equalsIgnoreCase(name)) {
        flattenStructuredData = Utils.toBoolean(value);
        logger().log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "ActivityParser.setting", name, value);
    }
}

From source file:com.mirth.connect.connectors.file.FileReceiver.java

@Override
public void onDeploy() throws ConnectorTaskException {
    this.connectorProperties = (FileReceiverProperties) SerializationUtils.clone(getConnectorProperties());

    if (connectorProperties.isBinary() && isProcessBatch()) {
        throw new ConnectorTaskException("Batch processing is not supported for binary data.");
    }//w w  w .ja v  a2s.  c  om

    this.charsetEncoding = CharsetUtils.getEncoding(connectorProperties.getCharsetEncoding(),
            System.getProperty("ca.uhn.hl7v2.llp.charset"));

    // Load the default configuration
    String configurationClass = configurationController.getProperty(connectorProperties.getProtocol(),
            "fileConfigurationClass");

    try {
        configuration = (FileConfiguration) Class.forName(configurationClass).newInstance();
    } catch (Exception e) {
        logger.trace("could not find custom configuration class, using default");
        configuration = new DefaultFileConfiguration();
    }

    try {
        configuration.configureConnectorDeploy(this, connectorProperties);
    } catch (Exception e) {
        throw new ConnectorTaskException(e);
    }

    this.moveToDirectory = connectorProperties.getMoveToDirectory();
    this.moveToFileName = connectorProperties.getMoveToFileName();
    this.errorMoveToDirectory = connectorProperties.getErrorMoveToDirectory();
    this.errorMoveToFileName = connectorProperties.getErrorMoveToFileName();

    fileSizeMinimum = NumberUtils.toLong(connectorProperties.getFileSizeMinimum(), 0);
    fileSizeMaximum = NumberUtils.toLong(connectorProperties.getFileSizeMaximum(), 0);

    eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(), getSourceName(),
            ConnectionStatusEventType.IDLE));
}

From source file:io.wcm.handler.url.suffix.SuffixParser.java

private long getLong(String key, long defaultValue) {
    String value = findSuffixPartByKey(key);
    if (value == null) {
        return defaultValue;
    }/*from   ww w  .  j a  v  a 2 s . c  o m*/
    return NumberUtils.toLong(value, defaultValue);
}

From source file:com.tealcube.minecraft.bukkit.config.MasterConfiguration.java

public long getLong(String key, long fallback) {
    if (settingMap == null || !settingMap.containsKey(key)) {
        return fallback;
    }//from   w  w  w .jav  a  2  s .  c  o m
    Object val = settingMap.get(key);
    if (val instanceof Long) {
        return (Long) val;
    }
    if (val instanceof String) {
        return NumberUtils.toLong((String) val, fallback);
    }
    return fallback;
}

From source file:ch.cyberduck.core.dav.DAVAttributesFinderFeature.java

@Override
public PathAttributes find(final Path file) throws BackgroundException {
    if (file.isRoot()) {
        return PathAttributes.EMPTY;
    }/*w w w  .j  a v  a2 s.co m*/
    try {
        try {
            final List<DavResource> status = session.getClient().list(new DAVPathEncoder().encode(file), 1,
                    Collections.<QName>emptySet());
            for (final DavResource resource : status) {
                if (resource.isDirectory()) {
                    if (!file.getType().contains(Path.Type.directory)) {
                        throw new NotfoundException(String.format("Path %s is directory", file.getAbsolute()));
                    }
                } else {
                    if (!file.getType().contains(Path.Type.file)) {
                        throw new NotfoundException(String.format("Path %s is file", file.getAbsolute()));
                    }
                }
                final PathAttributes attributes = new PathAttributes();
                if (resource.getModified() != null) {
                    attributes.setModificationDate(resource.getModified().getTime());
                }
                if (resource.getCreation() != null) {
                    attributes.setCreationDate(resource.getCreation().getTime());
                }
                if (resource.getContentLength() != null) {
                    attributes.setSize(resource.getContentLength());
                }
                if (StringUtils.isNotBlank(resource.getEtag())) {
                    attributes.setETag(resource.getEtag());
                    // Setting checksum is disabled. See #8798
                    // attributes.setChecksum(Checksum.parse(resource.getEtag()));
                }
                if (StringUtils.isNotBlank(resource.getDisplayName())) {
                    attributes.setDisplayname(resource.getDisplayName());
                }
                if (StringUtils.isNotBlank(resource.getDisplayName())) {
                    attributes.setDisplayname(resource.getDisplayName());
                }
                return attributes;
            }
            throw new NotfoundException(file.getAbsolute());
        } catch (SardineException e) {
            try {
                throw new DAVExceptionMappingService().map("Failure to read attributes of {0}", e, file);
            } catch (InteroperabilityException i) {
                // PROPFIND Method not allowed
                log.warn(String.format("Failure with PROPFIND request for %s. %s", file, i.getMessage()));
                final Map<String, String> headers = session.getClient()
                        .execute(new HttpHead(new DAVPathEncoder().encode(file)), new HeadersResponseHandler());
                final PathAttributes attributes = new PathAttributes();
                try {
                    attributes.setModificationDate(
                            dateParser.parse(headers.get(HttpHeaders.LAST_MODIFIED)).getTime());
                } catch (InvalidDateException p) {
                    log.warn(String.format("%s is not RFC 1123 format %s",
                            headers.get(HttpHeaders.LAST_MODIFIED), p.getMessage()));
                }
                if (!headers.containsKey(HttpHeaders.CONTENT_ENCODING)) {
                    // Set size unless response is compressed
                    attributes.setSize(NumberUtils.toLong(headers.get(HttpHeaders.CONTENT_LENGTH), -1));
                }
                if (headers.containsKey(HttpHeaders.ETAG)) {
                    attributes.setETag(headers.get(HttpHeaders.ETAG));
                    // Setting checksum is disabled. See #8798
                    // attributes.setChecksum(Checksum.parse(headers.get(HttpHeaders.ETAG)));
                }
                if (headers.containsKey(HttpHeaders.CONTENT_MD5)) {
                    attributes.setChecksum(Checksum.parse(headers.get(HttpHeaders.CONTENT_MD5)));
                }
                return attributes;
            }
        }
    } catch (SardineException e) {
        throw new DAVExceptionMappingService().map("Failure to read attributes of {0}", e, file);
    } catch (IOException e) {
        throw new HttpExceptionMappingService().map(e, file);
    }
}

From source file:io.wcm.sling.commons.request.RequestParam.java

/**
 * Returns a request parameter as long.//w w  w .  ja  v a2 s  .  co  m
 * @param request Request.
 * @param param Parameter name.
 * @param defaultValue Default value.
 * @return Parameter value or default value if it does not exist or is not a number.
 */
public static long getLong(ServletRequest request, String param, long defaultValue) {
    String value = request.getParameter(param);
    return NumberUtils.toLong(value, defaultValue);
}

From source file:com.navercorp.pinpoint.collector.config.CollectorConfiguration.java

protected static long readLong(Properties properties, String propertyName, long defaultValue) {
    final String value = properties.getProperty(propertyName);
    final long result = NumberUtils.toLong(value, defaultValue);
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("{}={}", propertyName, result);
    }/*from  w w w  .  j ava2 s.  c o  m*/
    return result;
}