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

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

Introduction

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

Prototype

public static int toInt(final String str) 

Source Link

Document

Convert a String to an int, returning zero if the conversion fails.

If the string is null, zero is returned.

 NumberUtils.toInt(null) = 0 NumberUtils.toInt("")   = 0 NumberUtils.toInt("1")  = 1 

Usage

From source file:com.mirth.connect.connectors.tcp.TcpReceiver.java

private int getLocalPort() {
    return NumberUtils
            .toInt(replacer.replaceValues(connectorProperties.getListenerConnectorProperties().getPort(),
                    getChannelId(), getChannel().getName()));
}

From source file:com.mirth.connect.connectors.tcp.TcpReceiver.java

private int getRemotePort() {
    return NumberUtils.toInt(replacer.replaceValues(connectorProperties.getRemotePort(), getChannelId(),
            getChannel().getName()));
}

From source file:com.mirth.connect.cli.CommandLineInterface.java

private void commandExportMessages(Token[] arguments) {
    if (hasInvalidNumberOfArguments(arguments, 2)) {
        return;//  w  w  w  . j a  va 2  s  . c o  m
    }

    // file path
    String path = arguments[1].getText();
    File fXml = new File(path);

    // message filter
    MessageFilter filter = new MessageFilter();
    String channelId = arguments[2].getText();

    // export mode
    ContentType contentType = null;

    boolean includeAttachments = false;
    if (arguments.length >= 4) {
        String modeArg = arguments[3].getText();

        if (StringUtils.equals(modeArg, "raw")) {
            contentType = ContentType.RAW;
        } else if (StringUtils.equals(modeArg, "processedraw")) {
            contentType = ContentType.PROCESSED_RAW;
        } else if (StringUtils.equals(modeArg, "transformed")) {
            contentType = ContentType.TRANSFORMED;
        } else if (StringUtils.equals(modeArg, "encoded")) {
            contentType = ContentType.ENCODED;
        } else if (StringUtils.equals(modeArg, "sent")) {
            contentType = ContentType.SENT;
        } else if (StringUtils.equals(modeArg, "response")) {
            contentType = ContentType.RESPONSE;
        } else if (StringUtils.equals(modeArg, "responsetransformed")) {
            contentType = ContentType.RESPONSE_TRANSFORMED;
        } else if (StringUtils.equals(modeArg, "processedresponse")) {
            contentType = ContentType.PROCESSED_RESPONSE;
        } else if (StringUtils.equals(modeArg, "xml-attach")) {
            includeAttachments = true;
        }
    }

    // page size
    int pageSize = 100;

    if (arguments.length == 5) {
        pageSize = NumberUtils.toInt(arguments[4].getText());
    }

    int messageCount = 0;

    try {
        filter.setMaxMessageId(client.getMaxMessageId(channelId));
        MessageWriter messageWriter = null;

        try {
            out.println("Exporting messages to file: " + fXml.getPath());

            PaginatedMessageList messageList = new PaginatedMessageList();
            messageList.setChannelId(channelId);
            messageList.setClient(client);
            messageList.setIncludeContent(true);
            messageList.setMessageFilter(filter);
            messageList.setPageSize(pageSize);

            MessageWriterOptions writerOptions = new MessageWriterOptions();
            writerOptions.setBaseFolder(new File(".").getPath());
            writerOptions.setContentType(contentType);
            writerOptions.setDestinationContent(false);
            writerOptions.setEncrypt(false);
            writerOptions.setRootFolder(FilenameUtils.getFullPath(fXml.getAbsolutePath()));
            writerOptions.setFilePattern(FilenameUtils.getName(fXml.getAbsolutePath()));
            writerOptions.setArchiveFormat(null);
            writerOptions.setCompressFormat(null);
            writerOptions.setIncludeAttachments(includeAttachments);

            messageWriter = MessageWriterFactory.getInstance().getMessageWriter(writerOptions,
                    client.getEncryptor());

            AttachmentSource attachmentSource = null;
            if (writerOptions.includeAttachments()) {
                attachmentSource = new AttachmentSource() {
                    @Override
                    public List<Attachment> getMessageAttachments(Message message) throws ClientException {
                        return client.getAttachmentsByMessageId(message.getChannelId(), message.getMessageId());
                    }
                };
            }

            messageCount = new MessageExporter().exportMessages(messageList, messageWriter, attachmentSource);
            messageWriter.finishWrite();
        } catch (Exception e) {
            Throwable cause = ExceptionUtils.getRootCause(e);
            error("unable to write file(s) " + path + ": " + cause, cause);
        } finally {
            if (messageWriter != null) {
                try {
                    messageWriter.close();
                } catch (Exception e) {
                    Throwable cause = ExceptionUtils.getRootCause(e);
                    error("unable to close file(s) " + path + ": " + cause, cause);
                }
            }
        }
    } catch (Exception e) {
        Throwable cause = ExceptionUtils.getRootCause(e);
        error("Unable to retrieve max message ID: " + cause, cause);
    }

    out.println("Messages Export Complete. " + messageCount + " Messages Exported.");
}

From source file:com.moviejukebox.model.Movie.java

public void setTop250(String top250, String source) {
    setTop250(NumberUtils.toInt(top250), source);
}

From source file:lineage2.gameserver.Config.java

/**
 * Method setField.//from  w  ww.jav  a  2  s.  c  o m
 * @param fieldName String
 * @param value String
 * @return boolean
 */
public static boolean setField(String fieldName, String value) {
    Field field = FieldUtils.getField(Config.class, fieldName);
    if (field == null) {
        return false;
    }
    try {
        if (field.getType() == boolean.class) {
            field.setBoolean(null, BooleanUtils.toBoolean(value));
        } else if (field.getType() == int.class) {
            field.setInt(null, NumberUtils.toInt(value));
        } else if (field.getType() == long.class) {
            field.setLong(null, NumberUtils.toLong(value));
        } else if (field.getType() == double.class) {
            field.setDouble(null, NumberUtils.toDouble(value));
        } else if (field.getType() == String.class) {
            field.set(null, value);
        } else {
            return false;
        }
    } catch (IllegalArgumentException e) {
        return false;
    } catch (IllegalAccessException e) {
        return false;
    }
    return true;
}

From source file:org.apache.openmeetings.backup.converter.WbConverter.java

private static int getInt(List<?> props, int idx) {
    Object o = props.get(idx);/*from   ww  w.  j a  v a  2s.  c  o  m*/
    if (o instanceof Number) {
        return ((Number) o).intValue();
    } else if (o instanceof String) {
        return NumberUtils.toInt((String) o);
    }
    return 0;
}

From source file:org.apache.sling.servlethelpers.HeaderSupport.java

public int getIntHeader(String name) {
    String value = getHeader(name);
    return NumberUtils.toInt(value);
}

From source file:org.apache.syncope.client.console.wicket.markup.html.form.AjaxSpinnerFieldPanel.java

@Override
public AjaxSpinnerFieldPanel<T> setNewModel(final List<Serializable> list) {
    setNewModel(new Model<T>() {

        private static final long serialVersionUID = 527651414610325237L;

        @Override/*from w w  w.j  a  v  a  2  s . co  m*/
        public T getObject() {
            T value = null;

            if (list != null && !list.isEmpty() && list.get(0) != null
                    && StringUtils.isNotBlank(list.get(0).toString())) {

                value = reference.equals(Integer.class)
                        ? reference.cast(NumberUtils.toInt(list.get(0).toString()))
                        : reference.equals(Long.class)
                                ? reference.cast(NumberUtils.toLong(list.get(0).toString()))
                                : reference.equals(Short.class)
                                        ? reference.cast(NumberUtils.toShort(list.get(0).toString()))
                                        : reference.equals(Float.class)
                                                ? reference.cast(NumberUtils.toFloat(list.get(0).toString()))
                                                : reference.equals(byte.class)
                                                        ? reference.cast(
                                                                NumberUtils.toByte(list.get(0).toString()))
                                                        : reference.cast(
                                                                NumberUtils.toDouble(list.get(0).toString()));
            }

            return value;
        }

        @Override
        public void setObject(final T object) {
            list.clear();
            if (object != null) {
                list.add(object.toString());
            }
        }
    });

    return this;
}

From source file:org.apache.syncope.client.console.wicket.markup.html.form.SpinnerFieldPanel.java

@Override
public SpinnerFieldPanel<T> setNewModel(final List<Serializable> list) {
    setNewModel(new Model<T>() {

        private static final long serialVersionUID = 527651414610325237L;

        @Override//from   w  w  w  .jav  a 2s.com
        public T getObject() {
            T value = null;

            if (list != null && !list.isEmpty() && StringUtils.hasText(list.get(0).toString())) {
                value = reference.equals(Integer.class)
                        ? reference.cast(NumberUtils.toInt(list.get(0).toString()))
                        : reference.equals(Long.class)
                                ? reference.cast(NumberUtils.toLong(list.get(0).toString()))
                                : reference.equals(Short.class)
                                        ? reference.cast(NumberUtils.toShort(list.get(0).toString()))
                                        : reference.equals(Float.class)
                                                ? reference.cast(NumberUtils.toFloat(list.get(0).toString()))
                                                : reference.equals(byte.class)
                                                        ? reference.cast(
                                                                NumberUtils.toByte(list.get(0).toString()))
                                                        : reference.cast(
                                                                NumberUtils.toDouble(list.get(0).toString()));
            }

            return value;
        }

        @Override
        public void setObject(final T object) {
            list.clear();
            if (object != null) {
                list.add(object.toString());
            }
        }
    });

    return this;
}

From source file:org.craftercms.commons.mongo.MongoClientFactory.java

@Override
protected MongoClient createInstance() throws Exception {

    if (StringUtils.isBlank(connectionString)) {
        logger.info("No connection string specified, connecting to {}:{}", connectionString, DEFAULT_MONGO_HOST,
                DEFAULT_MONGO_PORT);/*from www .  j a va 2 s . co m*/

        return new MongoClient(new ServerAddress(DEFAULT_MONGO_HOST, DEFAULT_MONGO_PORT));
    }

    StringTokenizer st = new StringTokenizer(connectionString, ",");
    List<ServerAddress> addressList = new ArrayList<>();

    while (st.hasMoreElements()) {
        String server = st.nextElement().toString();

        logger.debug("Processing first server found with string {}", server);

        String[] serverAndPort = server.split(":");
        if (serverAndPort.length == 2) {
            logger.debug("Server string defines host {} and port {}", serverAndPort[0], serverAndPort[1]);

            if (StringUtils.isBlank(serverAndPort[0])) {
                throw new IllegalArgumentException("Given host can't be empty");
            }

            int portNumber = NumberUtils.toInt(serverAndPort[1]);
            if (portNumber == 0) {
                throw new IllegalArgumentException("Given port number " + portNumber + " is not valid");
            }

            addressList.add(new ServerAddress(serverAndPort[0], portNumber));
        } else if (serverAndPort.length == 1) {
            logger.debug("Server string defines host {} only. Using default port ", serverAndPort[0]);

            if (StringUtils.isBlank(serverAndPort[0])) {
                throw new IllegalArgumentException("Given host can't be empty");
            }

            addressList.add(new ServerAddress(serverAndPort[0], DEFAULT_MONGO_PORT));
        } else {
            throw new IllegalArgumentException("Given connection string is not valid");
        }
    }

    logger.debug("Creating MongoClient with addresses: {}", addressList);

    if (options != null) {
        return new MongoClient(addressList, options);
    } else {
        return new MongoClient(addressList);
    }
}