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

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

Introduction

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

Prototype

public static float toFloat(final String str, final float defaultValue) 

Source Link

Document

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

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

 NumberUtils.toFloat(null, 1.1f)   = 1.0f NumberUtils.toFloat("", 1.1f)     = 1.1f NumberUtils.toFloat("1.5", 0.0f)  = 1.5f 

Usage

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public void averageDistinctDateRange(KpiVO kpi, String frequency) throws Exception {
    BscReportSupportUtils.loadExpression();
    for (DateRangeScoreVO dateScore : kpi.getDateRangeScores()) {
        List<Float> scores = new ArrayList<Float>();
        float score = 0.0f;
        int size = 0;
        for (BbMeasureData measureData : kpi.getMeasureDatas()) {
            String date = dateScore.getDate().replaceAll("/", "");
            if (!this.isDateRange(date, frequency, measureData)) {
                continue;
            }//from   w w w.j  a va2  s  .  co  m
            BscMeasureData data = new BscMeasureData();
            data.setActual(measureData.getActual());
            data.setTarget(measureData.getTarget());
            try {
                Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
                if (value == null) {
                    continue;
                }
                if (!NumberUtils.isNumber(String.valueOf(value))) {
                    continue;
                }
                float nowScore = NumberUtils.toFloat(String.valueOf(value), 0.0f);
                if (!scores.contains(nowScore)) {
                    scores.add(nowScore);
                    score += nowScore;
                    size++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (score != 0.0f && size > 0) {
            score = score / size;
        }
        dateScore.setScore(score);
        dateScore.setFontColor(BscScoreColorUtils.getFontColor(score));
        dateScore.setBgColor(BscScoreColorUtils.getBackgroundColor(score));
        dateScore.setImgIcon(BscReportSupportUtils.getHtmlIcon(kpi, score));
    }
}

From source file:com.moviejukebox.plugin.ScriptableScraperPlugin.java

private boolean updateMediaInfo(Movie movie) {
    Collection<SectionContentSS> sections = ssData.getSections("action", "get_details");
    if (!sections.isEmpty()) {
        runSections(sections, "get_details");

        SectionContentSS section = sections.iterator().next();

        // Title//w  w w  . j  a  v a 2s  .  c om
        String value = section.getVariable("movie.title");
        if (StringTools.isValidString(value)) {
            movie.setTitle(value, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Year
        value = section.getVariable("movie.year");
        if (StringTools.isValidString(value)) {
            movie.setYear(value, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Plot
        value = section.getVariable("movie.summary");
        if (StringTools.isValidString(value)) {
            movie.setPlot(value, SCRIPTABLESCRAPER_PLUGIN_ID);
            movie.setOutline(value, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Genres
        value = section.getVariable("movie.genres");
        if (StringTools.isValidString(value)) {
            List<String> newGenres = new LinkedList<>();
            for (String genre : Arrays.asList(value.split("\\|"))) {
                newGenres.add(genre);
                if (newGenres.size() == maxGenres) {
                    break;
                }
            }

            movie.setGenres(newGenres, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Rating
        value = section.getVariable("movie.score");
        if (StringTools.isValidString(value)) {
            movie.addRating(SCRIPTABLESCRAPER_PLUGIN_ID,
                    StringTools.parseRating(NumberUtils.toFloat(value.replace(',', '.'), -1) / 10));
        }

        // Top 250
        value = section.getVariable("movie.top250");
        if (StringTools.isValidString(value)) {
            movie.setTop250(value, IMDB_PLUGIN_ID);
        }

        // Director
        updatePersonInfo(movie, "directors", section.getVariable("movie.directors"), maxDirectors);

        // Writer
        updatePersonInfo(movie, "writers", section.getVariable("movie.writers"), maxWriters);

        // Actors
        updatePersonInfo(movie, "actors", section.getVariable("movie.actors"), maxActors);

        // Studio/Company
        value = section.getVariable("movie.studios");
        if (StringTools.isValidString(value)) {
            movie.setCompany(value.replaceAll("\\|", Movie.SPACE_SLASH_SPACE), SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Run time
        value = section.getVariable("movie.runtime");
        if (StringTools.isValidString(value)) {
            movie.setRuntime(value, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Tagline
        value = section.getVariable("movie.tagline");
        if (StringTools.isValidString(value)) {
            movie.setTagline(value, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Certification
        value = section.getVariable("movie.certification");
        if (StringTools.isValidString(value)) {
            movie.setCertification(value, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Country
        value = section.getVariable("movie.country");
        if (StringTools.isValidString(value)) {
            List<String> countries = new ArrayList<>();
            for (String country : Arrays.asList(value.split("\\|"))) {
                countries.add(country);
            }
            movie.setCountries(countries, SCRIPTABLESCRAPER_PLUGIN_ID);
        }

        // Quotes
        value = section.getVariable("movie.quotes");
        if (StringTools.isValidString(value)) {
            for (String quote : Arrays.asList(value.split("\\|"))) {
                movie.setQuote(cleanStringEnding(quote), SCRIPTABLESCRAPER_PLUGIN_ID);
                break;
            }
        }

        // Did you know
        value = section.getVariable("movie.didyouknow");
        if (StringTools.isValidString(value)) {
            for (String dyk : Arrays.asList(value.split("\\|"))) {
                movie.addDidYouKnow(dyk);
            }
        }

        return true;
    }

    return false;
}

From source file:com.uwsoft.editor.view.ui.properties.panels.UIBasicItemPropertiesMediator.java

@Override
protected void translateViewToItemData() {
    Entity entity = observableReference;

    transformComponent = ComponentCloner.get(ComponentRetriever.get(entity, TransformComponent.class));
    mainItemComponent = ComponentCloner.get(ComponentRetriever.get(entity, MainItemComponent.class));
    dimensionComponent = ComponentCloner.get(ComponentRetriever.get(entity, DimensionsComponent.class));
    tintComponent = ComponentCloner.get(ComponentRetriever.get(entity, TintComponent.class));

    mainItemComponent.itemIdentifier = viewComponent.getIdBoxValue();
    transformComponent.x = NumberUtils.toFloat(viewComponent.getXValue(), transformComponent.x);
    transformComponent.y = NumberUtils.toFloat(viewComponent.getYValue(), transformComponent.y);

    dimensionComponent.width = NumberUtils.toFloat(viewComponent.getWidthValue());
    dimensionComponent.height = NumberUtils.toFloat(viewComponent.getHeightValue());

    // TODO: manage width and height
    transformComponent.rotation = NumberUtils.toFloat(viewComponent.getRotationValue(),
            transformComponent.rotation);
    transformComponent.scaleX = (viewComponent.getFlipH() ? -1 : 1)
            * NumberUtils.toFloat(viewComponent.getScaleXValue(), transformComponent.scaleX);
    transformComponent.scaleY = (viewComponent.getFlipV() ? -1 : 1)
            * NumberUtils.toFloat(viewComponent.getScaleYValue(), transformComponent.scaleY);
    Color color = viewComponent.getTintColor();
    tintComponent.color.set(color);/*from w  ww .j a  va 2 s.  co m*/

    Array<Component> componentsToUpdate = new Array<>();
    componentsToUpdate.add(transformComponent);
    componentsToUpdate.add(mainItemComponent);
    componentsToUpdate.add(dimensionComponent);
    componentsToUpdate.add(tintComponent);
    Object[] payload = new Object[2];
    payload[0] = entity;
    payload[1] = componentsToUpdate;
    Overlap2DFacade.getInstance().sendNotification(MsgAPI.ACTION_UPDATE_ITEM_DATA, payload);
}

From source file:com.o2d.pkayjava.editor.view.ui.properties.panels.UIBasicItemPropertiesMediator.java

@Override
protected void translateViewToItemData() {
    Entity entity = observableReference;

    transformComponent = ComponentCloner.get(ComponentRetriever.get(entity, TransformComponent.class));
    mainItemComponent = ComponentCloner.get(ComponentRetriever.get(entity, MainItemComponent.class));
    dimensionComponent = ComponentCloner.get(ComponentRetriever.get(entity, DimensionsComponent.class));
    tintComponent = ComponentCloner.get(ComponentRetriever.get(entity, TintComponent.class));

    mainItemComponent.itemIdentifier = viewComponent.getIdBoxValue();
    transformComponent.setX(NumberUtils.toFloat(viewComponent.getXValue(), transformComponent.getX()));
    transformComponent.setY(NumberUtils.toFloat(viewComponent.getYValue(), transformComponent.getY()));

    dimensionComponent.width = NumberUtils.toFloat(viewComponent.getWidthValue());
    dimensionComponent.height = NumberUtils.toFloat(viewComponent.getHeightValue());

    // TODO: manage width and height
    transformComponent.setRotation(/*from   ww w.  j ava 2  s . c  om*/
            NumberUtils.toFloat(viewComponent.getRotationValue(), transformComponent.getRotation()));
    transformComponent.setScaleX((viewComponent.getFlipH() ? -1 : 1)
            * NumberUtils.toFloat(viewComponent.getScaleXValue(), transformComponent.getScaleX()));
    transformComponent.setScaleY((viewComponent.getFlipV() ? -1 : 1)
            * NumberUtils.toFloat(viewComponent.getScaleYValue(), transformComponent.getScaleY()));
    Color color = viewComponent.getTintColor();
    tintComponent.color.set(color);

    Array<Component> componentsToUpdate = new Array<>();
    componentsToUpdate.add(transformComponent);
    componentsToUpdate.add(mainItemComponent);
    componentsToUpdate.add(dimensionComponent);
    componentsToUpdate.add(tintComponent);
    Object[] payload = new Object[2];
    payload[0] = entity;
    payload[1] = componentsToUpdate;
    Overlap2DFacade.getInstance().sendNotification(Sandbox.ACTION_UPDATE_ITEM_DATA, payload);
}

From source file:com.jernejerin.traffic.helper.TripOperations.java

/**
 * Check if longitude is between -180 and 180 inclusive.
 *
 * @param longitudeStr string representation of longitude
 * @param defaultValue the default value to return if parse fails
 * @return converted longitude/*from   ww w  .j a v  a2  s . co  m*/
 */
public static float tryLongitude(String longitudeStr, float defaultValue) {
    if (longitudeStr == null)
        return defaultValue;
    float longitude = NumberUtils.toFloat(longitudeStr, defaultValue);
    if (longitude > 180 || longitude < -180)
        return defaultValue;
    else
        return longitude;
}

From source file:com.jernejerin.traffic.helper.TripOperations.java

/**
 * Check if latitude is between -90 and 90 inclusive.
 *
 * @param latitudeStr string representation of latitude
 * @param defaultValue the default value to return if parse fails
 * @return converted latitude//from  w  w  w . j  a  v  a 2  s.  c om
 */
public static float tryLatitude(String latitudeStr, float defaultValue) {
    if (latitudeStr == null)
        return defaultValue;
    float latitude = NumberUtils.toFloat(latitudeStr, defaultValue);
    if (latitude > 90 || latitude < -90)
        return defaultValue;
    else
        return latitude;
}

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public float countDistinct(KpiVO kpi) throws Exception {
    List<BbMeasureData> measureDatas = kpi.getMeasureDatas();
    List<Float> scores = new ArrayList<Float>();
    for (BbMeasureData measureData : measureDatas) {
        BscMeasureData data = new BscMeasureData();
        data.setActual(measureData.getActual());
        data.setTarget(measureData.getTarget());
        try {//  www .java  2 s. c o m
            Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
            if (value == null) {
                continue;
            }
            if (!NumberUtils.isNumber(String.valueOf(value))) {
                continue;
            }
            float nowScore = NumberUtils.toFloat(String.valueOf(value), 0.0f);
            if (!scores.contains(nowScore)) {
                scores.add(nowScore);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return Float.valueOf(scores.size());
}

From source file:com.moviejukebox.tools.StringTools.java

/**
 * Parse a string value and convert it into an integer rating
 *
 * The rating should be between 0 and 10 inclusive.<br/>
 * Invalid values or values less than 0 will return -1
 *
 * @param rating the converted rating or -1 if there was an error
 * @return//from  w w  w.  j  av  a 2 s . c o m
 */
public static int parseRating(String rating) {
    return parseRating(NumberUtils.toFloat(rating.replace(',', '.'), -1));
}

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public void countDistinctDateRange(KpiVO kpi, String frequency) throws Exception {
    BscReportSupportUtils.loadExpression();
    for (DateRangeScoreVO dateScore : kpi.getDateRangeScores()) {
        List<Float> scores = new ArrayList<Float>();
        float score = 0.0f;
        //int size = 0;
        for (BbMeasureData measureData : kpi.getMeasureDatas()) {
            String date = dateScore.getDate().replaceAll("/", "");
            if (!this.isDateRange(date, frequency, measureData)) {
                continue;
            }//from w w  w. j a  va2 s  .  c  o  m
            BscMeasureData data = new BscMeasureData();
            data.setActual(measureData.getActual());
            data.setTarget(measureData.getTarget());
            try {
                Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
                if (value == null) {
                    continue;
                }
                if (!NumberUtils.isNumber(String.valueOf(value))) {
                    continue;
                }
                float nowScore = NumberUtils.toFloat(String.valueOf(value), 0.0f);
                if (!scores.contains(nowScore)) {
                    scores.add(nowScore);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        score = Float.valueOf(scores.size());
        dateScore.setScore(score);
        dateScore.setFontColor(BscScoreColorUtils.getFontColor(score));
        dateScore.setBgColor(BscScoreColorUtils.getBackgroundColor(score));
        dateScore.setImgIcon(BscReportSupportUtils.getHtmlIcon(kpi, score));
    }
}

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public float max(KpiVO kpi) throws Exception {
    List<BbMeasureData> measureDatas = kpi.getMeasureDatas();
    float score = 0.0f; // init
    int size = 0;
    float nowScore = 0.0f;
    for (BbMeasureData measureData : measureDatas) {
        BscMeasureData data = new BscMeasureData();
        data.setActual(measureData.getActual());
        data.setTarget(measureData.getTarget());
        try {//from   w w w. j  a  va  2s  .co m
            Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
            if (value == null) {
                continue;
            }
            if (!NumberUtils.isNumber(String.valueOf(value))) {
                continue;
            }
            nowScore = NumberUtils.toFloat(String.valueOf(value), 0.0f);
            if (size < 1) {
                score = nowScore;
            } else { // Max
                if (score < nowScore) {
                    score = nowScore;
                }
            }
            size++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return score;
}