Example usage for org.apache.commons.lang3 StringUtils isNumeric

List of usage examples for org.apache.commons.lang3 StringUtils isNumeric

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils isNumeric.

Prototype

public static boolean isNumeric(final CharSequence cs) 

Source Link

Document

Checks if the CharSequence contains only Unicode digits.

Usage

From source file:org.yamj.core.tools.MetadataTools.java

/**
 * Convert a string to date to/*  ww w.  j a v  a  2s. co  m*/
 *
 * @param dateToParse
 * @return
 */
public static Date parseToDate(String dateToParse) {
    Date parsedDate = null;

    String parseDate = StringUtils.normalizeSpace(dateToParse);
    if (StringUtils.isNotBlank(parseDate)) {
        try {
            DateTime dateTime;
            if (parseDate.length() == 4 && StringUtils.isNumeric(parseDate)) {
                // assume just the year an append "-01-01" to the end
                dateTime = new DateTime(parseDate + "-01-01");
            } else {
                // look for the date as "dd MMMM yyyy (Country)" and remove the country
                Matcher m = DATE_COUNTRY.matcher(dateToParse);
                if (m.find()) {
                    parseDate = m.group(1);
                }
                dateTime = new DateTime(parseDate);
            }
            parsedDate = dateTime.toDate();
        } catch (Exception ex) {
            LOG.debug("Failed to parse date '{}', error: {}", dateToParse, ex.getMessage());
            LOG.trace("Error", ex);
        }
    }

    return parsedDate;
}

From source file:org.yamj.core.tools.MetadataTools.java

/**
 * Locate a 4 digit year in a date string.
 *
 * @param date/*from   w  ww .j  a v a 2  s. com*/
 * @return
 */
public static int extractYearAsInt(String date) {
    if (StringUtils.isBlank(date)) {
        return -1;
    }
    if (StringUtils.isNumeric(date) && (date.length() == 4)) {
        return NumberUtils.toInt(date, -1);
    }

    int year = -1;
    Matcher m = YEAR_PATTERN.matcher(date);
    if (m.find()) {
        year = NumberUtils.toInt(m.group(1));
    }

    return year;
}

From source file:org.yamj.jetty.Start.java

private static int convertToInt(String toConvert, int defaultValue) {
    if (StringUtils.isNumeric(toConvert)) {
        return Integer.parseInt(toConvert);
    } else {/*from  w  w  w  . j  av a  2  s.  co  m*/
        return defaultValue;
    }
}

From source file:pcgen.core.prereq.PreMult.java

@Override
public String toHtmlString(final Prerequisite prereq) {
    final PrerequisiteTestFactory factory = PrerequisiteTestFactory.getInstance();

    StringBuilder str = new StringBuilder(250);
    String delimiter = ""; //$NON-NLS-1$
    for (Prerequisite element : prereq.getPrerequisites()) {
        final PrerequisiteTest test = factory.getTest(element.getKind());
        if (test == null) {
            Logging.errorPrintLocalised("PreMult.cannot_find_subformatter", element.getKind()); //$NON-NLS-1$
        } else {/* w  w w.j  a  va  2  s. c  o m*/
            str.append(delimiter);
            if (test instanceof PreMult && !delimiter.equals("")) {
                str.append("##BR##");
            }
            str.append(test.toHtmlString(element));
            delimiter = LanguageBundle.getString("PreMult.html_delimiter"); //$NON-NLS-1$
        }
    }

    // Handle some special cases - all required, one required or none required
    int numRequired = -1;
    if (StringUtils.isNumeric(prereq.getOperand())) {
        numRequired = Integer.parseInt(prereq.getOperand());
    }
    if ((prereq.getOperator() == PrerequisiteOperator.GTEQ || prereq.getOperator() == PrerequisiteOperator.GT
            || prereq.getOperator() == PrerequisiteOperator.EQ)
            && numRequired == prereq.getPrerequisites().size()) {
        return LanguageBundle.getFormattedString("PreMult.toHtmlAllOf", //$NON-NLS-1$
                str.toString());
    }
    if ((prereq.getOperator() == PrerequisiteOperator.GTEQ || prereq.getOperator() == PrerequisiteOperator.EQ)
            && numRequired == 1) {
        return LanguageBundle.getFormattedString("PreMult.toHtmlEither", //$NON-NLS-1$
                str.toString());
    }
    if ((prereq.getOperator() == PrerequisiteOperator.LT && numRequired == 1)
            || ((prereq.getOperator() == PrerequisiteOperator.EQ
                    || prereq.getOperator() == PrerequisiteOperator.LTEQ) && numRequired == 0)) {
        return LanguageBundle.getFormattedString("PreMult.toHtmlNone", //$NON-NLS-1$
                str.toString());
    }

    return LanguageBundle.getFormattedString("PreMult.toHtml", //$NON-NLS-1$
            prereq.getOperator().toDisplayString(), prereq.getOperand(), str.toString());

}

From source file:plugin.exporttokens.CampaignHistoryToken.java

@Override
public String getToken(String tokenSource, PlayerCharacter pc, ExportHandler eh) {
    StringTokenizer aTok = new StringTokenizer(tokenSource, ".");
    aTok.nextToken();/* ww w .  jav a2 s  . co m*/

    Visibility visibility = Visibility.VISIBLE;
    String entryIndex = aTok.nextToken();
    if (!StringUtils.isNumeric(entryIndex)) {
        if (entryIndex.equals("ALL")) {
            visibility = Visibility.ALL;
        } else if (entryIndex.equals("HIDDEN")) {
            visibility = Visibility.HIDDEN;
        } else if (!entryIndex.equals("VISIBLE")) {
            Logging.log(Logging.LST_ERROR, "Invalid visibility entry '" + entryIndex
                    + "'. Should be one of ALL, VISIBLE or HIDDEN. Token was " + tokenSource);
            return "";
        }

        entryIndex = aTok.nextToken();
    }

    if (!StringUtils.isNumeric(entryIndex)) {
        Logging.log(Logging.LST_ERROR,
                "Invalid position entry '" + entryIndex + "', it should be a number. Token was " + tokenSource);
        return "";
    }

    int index = Integer.parseInt(entryIndex);
    ChronicleEntry entry = getTargetChronicleEntry(index, visibility, pc.getDisplay());
    if (entry == null) {
        return "";
    }
    String token = (aTok.hasMoreTokens()) ? aTok.nextToken() : "TEXT";
    String value = getChronicleValue(entry, token.toUpperCase());
    if (value == null) {
        Logging.log(Logging.LST_ERROR, "Invalid property '" + token + "'. Token was " + tokenSource);
        return "";
    }
    return value;
}

From source file:qic.ui.GuildPanel.java

public GuildPanel() {
    super(new BorderLayout(1, 1));
    textArea.setFont(new Font("Consolas", Font.TRUETYPE_FONT, 12));
    add(new JScrollPane(textArea), BorderLayout.CENTER);
    JPanel southPanel = new JPanel();
    JButton guildBtn = new JButton("Append with Guildmates");
    JButton saveBtn = new JButton("Save");
    JTextField guildUrl = new JTextField(50);
    // https://www.pathofexile.com/guild/profile/162231
    southPanel.add(new JLabel("Guild Profile No./Url"));
    southPanel.add(guildUrl);// www  . j  a  va 2  s  .  c  o m
    southPanel.add(guildBtn);
    southPanel.add(new JLabel("Discount: "
            + Config.getPropety(Config.GUILD_DISCOUNT_STRING, Config.GUILD_DISCOUNT_STRING_DEFAULT)));
    add(southPanel, BorderLayout.SOUTH);
    southPanel.add(saveBtn);
    loadConfigToTextArea();

    guildBtn.addActionListener(e -> {
        String url = guildUrl.getText();
        if (!url.isEmpty()) {
            Worker<List<String>> worker = new Worker<List<String>>(() -> {
                List<String> members = Collections.emptyList();
                String urlFinal = StringUtils.isNumeric(url)
                        ? "https://www.pathofexile.com/guild/profile/" + url
                        : url;
                try {
                    members = GuildPageScraper.scrapeMembers(urlFinal);
                } catch (Exception ex) {
                    logger.error("Error while scraping guild page: " + urlFinal);
                    showError(ex);
                }
                return members;
            }, guildNames -> {
                if (!guildNames.isEmpty()) {
                    guildNames.stream().forEach(name -> {
                        String ls = textArea.getText().isEmpty() ? "" : System.lineSeparator();
                        textArea.append(ls + name);
                    });
                }
            });
            worker.execute();
        }
    });

    saveBtn.addActionListener(e -> save());
}

From source file:rs.metropolitan.data_changer.base.ToDouble.java

@Override
public Double changeString(String data) {
    try {/*from w  ww  .j a v  a  2 s. c  o  m*/
        String dot = ".";
        String beforeDot = StringUtils.substringBefore(data, dot);
        String afterDot = StringUtils.substringBefore(data, dot);
        if (StringUtils.isNumeric(beforeDot) && StringUtils.isNumeric(afterDot)
                && StringUtils.countMatches(data, dot) == 1) {
            return new Double(data);
        }
        return null;
    } catch (NumberFormatException ex) {
        System.err.print(ex.getLocalizedMessage());
        return null;
    } catch (Exception ex) {
        System.err.print(ex.getLocalizedMessage());
        return null;
    }
}

From source file:rs.metropolitan.data_changer.base.ToInteger.java

@Override
public Integer changeString(String data) {
    try {// w ww. j a  va  2  s .  c o  m
        String dot = ".";
        if (StringUtils.isNumeric(data) && StringUtils.countMatches(data, dot) == 0) {
            return new Integer(data);
        }
        return null;
    } catch (NumberFormatException ex) {
        System.err.print(ex.getLocalizedMessage());
        return null;
    } catch (Exception ex) {
        System.err.print(ex.getLocalizedMessage());
        return null;
    }
}

From source file:ru.org.linux.group.GroupDao.java

/**
 *      ?   ./*from   w  w  w. ja  v a2  s  .  c o  m*/
 *
 * @param section  ?.
 * @param name    ? 
 * @return  
 */
public Optional<Group> getGroupOpt(Section section, String name, Boolean allowNumber) {
    try {
        int id;

        if (allowNumber && StringUtils.isNumeric(name)) {
            id = jdbcTemplate.queryForObject("SELECT id FROM groups WHERE section=? AND id=?", Integer.class,
                    section.getId(), Integer.parseInt(name));
        } else {
            id = jdbcTemplate.queryForObject("SELECT id FROM groups WHERE section=? AND urlname=?",
                    Integer.class, section.getId(), name);
        }

        return Optional.of(getGroup(id));
    } catch (EmptyResultDataAccessException ex) {
        logger.debug("Group '{}' not found in section {}", name, section.getUrlName());
        return Optional.empty();
    }
}