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

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

Introduction

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

Prototype

public static double toDouble(final String str) 

Source Link

Document

Convert a String to a double, returning 0.0d if the conversion fails.

If the string str is null, 0.0d is returned.

 NumberUtils.toDouble(null)   = 0.0d NumberUtils.toDouble("")     = 0.0d NumberUtils.toDouble("1.5")  = 1.5d 

Usage

From source file:org.ocsoft.rosetto.parsers.rosetto.RosettoElementParser.java

/**
 * ??Rosetto??????.<br>/*from  w  w w .  j av a2s  .c om*/
 * ??????????ActionCall??????????.
 */
@Override
public RosettoValue parseElement(String element) {
    //null?Values.NULL
    if (element == null)
        return Values.NULL;
    //??????
    element = ParseUtils.removeDoubleQuate(element);

    //??????????????
    if (element.startsWith("[") && element.endsWith("]")) {
        //????????
        return parseActionCall(element);
    } else if (element.startsWith("(") && element.endsWith(")")) {
        //??????
        return parseList(element);
    } else if (element.startsWith("{") && element.endsWith("}")) {
        //??????
        return parseMacro(element);
    }

    //????????????
    if (element.startsWith("@")) {
        //@?????
        return new ActionCall("getlocal", element.substring(1));
    } else if (element.startsWith("$")) {
        //$??????
        return new ActionCall("getglobal", element.substring(1));
    }

    //???????????
    if (NumberUtils.isNumber(element)) {
        if (element.contains(".")) {
            return new DoubleValue(NumberUtils.toDouble(element));
        } else {
            return new IntValue(NumberUtils.toLong(element));
        }
    }

    //bool???????????
    if (element.equals("true")) {
        return BoolValue.TRUE;
    } else if (element.equals("false")) {
        return BoolValue.FALSE;
    }

    //?????????
    return new StringValue(element);
}

From source file:org.onebusaway.presentation.impl.realtime.SiriSupport.java

private static OnwardCallStructure getOnwardCallStructure(StopBean stopBean,
        PresentationService presentationService, double distanceOfCallAlongTrip,
        double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction,
        boolean hasRealtimeData, long responseTimestamp, long scheduledArrivalTime) {

    OnwardCallStructure onwardCallStructure = new OnwardCallStructure();
    onwardCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber));

    StopPointRefStructure stopPointRef = new StopPointRefStructure();
    stopPointRef.setValue(stopBean.getId());
    onwardCallStructure.setStopPointRef(stopPointRef);

    if (stopBean.getCode() != null) {
        // Agency's prefer stop code display in UI, so override platform name for this use
        NaturalLanguageStringStructure platform = new NaturalLanguageStringStructure();
        platform.setValue(stopBean.getCode());
        onwardCallStructure.setArrivalPlatformName(platform);
    }//  w ww  .java2s.c  om

    NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure();
    stopPoint.setValue(stopBean.getName());
    onwardCallStructure.setStopPointName(stopPoint);

    if (prediction != null) {
        if (prediction.getTimepointPredictedArrivalTime() < responseTimestamp) {
            onwardCallStructure.setExpectedArrivalTime(new Date(responseTimestamp));
            onwardCallStructure.setExpectedDepartureTime(new Date(responseTimestamp));
        } else {
            onwardCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointPredictedArrivalTime()));
            onwardCallStructure
                    .setExpectedDepartureTime(new Date(prediction.getTimepointPredictedDepartureTime()));
        }
    } else if (!hasRealtimeData) {
        _log.debug("using arrival time of " + new Date(scheduledArrivalTime));
        onwardCallStructure.setExpectedArrivalTime(new Date(scheduledArrivalTime));
        onwardCallStructure.setExpectedDepartureTime(new Date(scheduledArrivalTime));
    }

    // siri extensions
    SiriExtensionWrapper wrapper = new SiriExtensionWrapper();
    ExtensionsStructure distancesExtensions = new ExtensionsStructure();
    SiriDistanceExtension distances = new SiriDistanceExtension();

    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setGroupingUsed(false);

    distances.setStopsFromCall(index);
    distances.setCallDistanceAlongRoute(NumberUtils.toDouble(df.format(distanceOfCallAlongTrip)));
    distances.setDistanceFromCall(NumberUtils.toDouble(df.format(distanceOfVehicleFromCall)));
    distances.setPresentableDistance(presentationService.getPresentableDistance(distances));

    wrapper.setDistances(distances);
    distancesExtensions.setAny(wrapper);
    onwardCallStructure.setExtensions(distancesExtensions);

    return onwardCallStructure;
}

From source file:org.onebusaway.presentation.impl.realtime.SiriSupport.java

private static MonitoredCallStructure getMonitoredCallStructure(StopBean stopBean,
        PresentationService presentationService, double distanceOfCallAlongTrip,
        double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction,
        boolean hasRealtimeData, long responseTimestamp, long scheduledArrivalTime) {

    MonitoredCallStructure monitoredCallStructure = new MonitoredCallStructure();
    monitoredCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber));

    StopPointRefStructure stopPointRef = new StopPointRefStructure();
    stopPointRef.setValue(stopBean.getId());
    monitoredCallStructure.setStopPointRef(stopPointRef);

    NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure();
    stopPoint.setValue(stopBean.getName());
    monitoredCallStructure.setStopPointName(stopPoint);

    if (prediction != null) {
        if (!hasRealtimeData) {
            monitoredCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointScheduledTime()));
            monitoredCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointScheduledTime()));
        }/*from w w w  .  j av  a  2  s. c o  m*/
        // do not allow predicted times to be less than ResponseTimestamp
        else if (prediction.getTimepointPredictedArrivalTime() < responseTimestamp) {
            /*
             * monitoredCall has less precision than onwardCall (date vs. timestamp)
             * which results in a small amount of error when converting back to timestamp.
             * Add a second here to prevent negative values from showing up in the UI 
             * (actual precision of the value is 1 minute, so a second has little influence)
             */
            monitoredCallStructure.setExpectedArrivalTime(new Date(responseTimestamp + 1000));
            monitoredCallStructure.setExpectedDepartureTime(new Date(responseTimestamp + 1000));
        } else {
            monitoredCallStructure
                    .setExpectedArrivalTime(new Date(prediction.getTimepointPredictedArrivalTime()));
            monitoredCallStructure
                    .setExpectedDepartureTime(new Date(prediction.getTimepointPredictedArrivalTime()));
        }
    } else if (!hasRealtimeData) {
        monitoredCallStructure.setExpectedArrivalTime(new Date(scheduledArrivalTime));
        monitoredCallStructure.setExpectedDepartureTime(new Date(scheduledArrivalTime));
    }

    //setting the scheduled arrival time.
    if (monitoredCallStructure.getExpectedArrivalTime() != null) {
        monitoredCallStructure.setAimedArrivalTime(new Date(scheduledArrivalTime));
    }

    // siri extensions
    SiriExtensionWrapper wrapper = new SiriExtensionWrapper();
    ExtensionsStructure distancesExtensions = new ExtensionsStructure();
    SiriDistanceExtension distances = new SiriDistanceExtension();

    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setGroupingUsed(false);

    distances.setStopsFromCall(index);
    distances.setCallDistanceAlongRoute(NumberUtils.toDouble(df.format(distanceOfCallAlongTrip)));
    distances.setDistanceFromCall(NumberUtils.toDouble(df.format(distanceOfVehicleFromCall)));
    distances.setPresentableDistance(presentationService.getPresentableDistance(distances));

    wrapper.setDistances(distances);
    distancesExtensions.setAny(wrapper);
    monitoredCallStructure.setExtensions(distancesExtensions);

    return monitoredCallStructure;
}

From source file:org.openecomp.sdc.common.util.ValidationUtils.java

public static boolean hasBeenCertified(String version) {
    return NumberUtils.toDouble(version) >= 1;
}

From source file:org.sakaiproject.gradebookng.business.AssignmentGradeComparator.java

@Override
public int compare(final GbStudentGradeInfo g1, final GbStudentGradeInfo g2) {

    final GbGradeInfo info1 = g1.getGrades().get(this.assignmentId);
    final GbGradeInfo info2 = g2.getGrades().get(this.assignmentId);

    // for proper number ordering, these have to be numerical
    final Double grade1 = (info1 != null) ? NumberUtils.toDouble(info1.getGrade()) : null;
    final Double grade2 = (info2 != null) ? NumberUtils.toDouble(info2.getGrade()) : null;

    return new CompareToBuilder().append(grade1, grade2).toComparison();

}

From source file:se.trixon.toolbox.geotagger.GeotaggerTopComponent.java

private void openFileCsv(File file) throws IOException {
    List<String> lines = FileUtils.readLines(file);

    lines.stream().forEach((line) -> {
        String[] elements = StringUtils.splitPreserveAllTokens(line, ";");
        if (elements.length < 3 || elements[0].isEmpty() || elements[1].isEmpty() || elements[2].isEmpty()) {
            logAppend(String.format(" - %s: %s", mBundle.getString("invalidGeotag"), line));
        } else {//  ww w.j  ava2 s.c  o  m
            String name = elements[GeotagTableModel.COLUMN_NAME];
            double latitude = NumberUtils.toDouble(elements[GeotagTableModel.COLUMN_LATITUDE]);
            double longitude = NumberUtils.toDouble(elements[GeotagTableModel.COLUMN_LONGITUDE]);
            double altitude = 0;

            if (elements.length > GeotagTableModel.COLUMN_ALTITUDE) {
                altitude = NumberUtils.toDouble(elements[GeotagTableModel.COLUMN_ALTITUDE]);
            }

            Geotag geotag = new Geotag(name, latitude, longitude, altitude);
            mTableModel.addRow(geotag);
        }
    });
}

From source file:us.fatehi.pointlocation6709.parse.PointLocationParser.java

/**
 * Parses a string representation of the point location.
 *
 * @param representation//from   ww  w .j  a  v a 2  s .c  o m
 *        String representation of the point location
 * @return Point location
 * @throws ParserException
 *         On an exception
 */
public static PointLocation parsePointLocation(final String representation) throws ParserException {
    if (StringUtils.isBlank(representation)) {
        throw new ParserException("No point location value provided");
    }
    if (!StringUtils.endsWith(representation, "/")) {
        throw new ParserException("Point location value must be terminated with /");
    }

    final PointLocationParser parser = new PointLocationParser();
    final CoordinateParser coordinateParser = new CoordinateParser();
    // Split by group
    final List<String> tokens = parser.split(representation);
    if (tokens.size() != 4) {
        throw new ParserException("Cannot parse " + representation);
    }

    final Latitude latitude = coordinateParser.parseLatitude(tokens.get(0));
    final Longitude longitude = coordinateParser.parseLongitude(tokens.get(1));
    final double altitude = NumberUtils.toDouble(tokens.get(2));
    final String coordinateReferenceSystemIdentifier = StringUtils.trimToEmpty(tokens.get(3));

    final PointLocation pointLocation = new PointLocation(latitude, longitude, altitude,
            coordinateReferenceSystemIdentifier);
    return pointLocation;
}