Example usage for java.math BigDecimal compareTo

List of usage examples for java.math BigDecimal compareTo

Introduction

In this page you can find the example usage for java.math BigDecimal compareTo.

Prototype

@Override
public int compareTo(BigDecimal val) 

Source Link

Document

Compares this BigDecimal with the specified BigDecimal .

Usage

From source file:org.eclipse.smarthome.ui.webapp.internal.render.SetpointRenderer.java

/**
 * {@inheritDoc}/*ww w.  j av a2 s. c  om*/
 */
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Setpoint sp = (Setpoint) w;

    State state = itemUIRegistry.getState(w);
    String newLowerState = state.toString();
    String newHigherState = state.toString();

    // set defaults for min, max and step
    BigDecimal step = sp.getStep();
    if (step == null) {
        step = BigDecimal.ONE;
    }
    BigDecimal minValue = sp.getMinValue();
    if (minValue == null) {
        minValue = BigDecimal.ZERO;
    }
    BigDecimal maxValue = sp.getMaxValue();
    if (maxValue == null) {
        maxValue = BigDecimal.valueOf(100);
    }

    // if the current state is a valid value, we calculate the up and down step values
    if (state instanceof DecimalType) {
        DecimalType actState = (DecimalType) state;
        BigDecimal newLower = actState.toBigDecimal().subtract(step);
        BigDecimal newHigher = actState.toBigDecimal().add(step);
        if (newLower.compareTo(minValue) < 0) {
            newLower = minValue;
        }
        if (newHigher.compareTo(maxValue) > 0) {
            newHigher = maxValue;
        }
        newLowerState = newLower.toString();
        newHigherState = newHigher.toString();
    }

    String snippetName = "setpoint";
    String snippet = getSnippet(snippetName);

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    snippet = StringUtils.replace(snippet, "%icon%", escapeURLPath(itemUIRegistry.getIcon(w)));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%state%", state.toString());
    snippet = StringUtils.replace(snippet, "%newlowerstate%", newLowerState);
    snippet = StringUtils.replace(snippet, "%newhigherstate%", newHigherState);
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    snippet = StringUtils.replace(snippet, "%minValue%", minValue.toString());
    snippet = StringUtils.replace(snippet, "%maxValue%", maxValue.toString());
    snippet = StringUtils.replace(snippet, "%step%", step.toString());

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
}

From source file:org.eclipse.smarthome.ui.classic.internal.render.SetpointRenderer.java

/**
 * {@inheritDoc}//from   w  ww  . j  ava 2 s .c om
 */
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Setpoint sp = (Setpoint) w;

    State state = itemUIRegistry.getState(w);
    String newLowerState = state.toString();
    String newHigherState = state.toString();

    // set defaults for min, max and step
    BigDecimal step = sp.getStep();
    if (step == null) {
        step = BigDecimal.ONE;
    }
    BigDecimal minValue = sp.getMinValue();
    if (minValue == null) {
        minValue = BigDecimal.ZERO;
    }
    BigDecimal maxValue = sp.getMaxValue();
    if (maxValue == null) {
        maxValue = BigDecimal.valueOf(100);
    }

    // if the current state is a valid value, we calculate the up and down step values
    if (state instanceof DecimalType) {
        DecimalType actState = (DecimalType) state;
        BigDecimal newLower = actState.toBigDecimal().subtract(step);
        BigDecimal newHigher = actState.toBigDecimal().add(step);
        if (newLower.compareTo(minValue) < 0) {
            newLower = minValue;
        }
        if (newHigher.compareTo(maxValue) > 0) {
            newHigher = maxValue;
        }
        newLowerState = newLower.toString();
        newHigherState = newHigher.toString();
    }

    String snippetName = "setpoint";
    String snippet = getSnippet(snippetName);

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    snippet = StringUtils.replace(snippet, "%category%", escapeURLPath(itemUIRegistry.getCategory(w)));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%state%", state.toString());
    snippet = StringUtils.replace(snippet, "%newlowerstate%", newLowerState);
    snippet = StringUtils.replace(snippet, "%newhigherstate%", newHigherState);
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    snippet = StringUtils.replace(snippet, "%minValue%", minValue.toString());
    snippet = StringUtils.replace(snippet, "%maxValue%", maxValue.toString());
    snippet = StringUtils.replace(snippet, "%step%", step.toString());

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
}

From source file:com.xumpy.timesheets.services.implementations.JobsGraphics.java

public List<WorkingDay> calculateWorkingDates(List<Jobs> jobs) {
    List<WorkingDay> workingDays = new ArrayList<WorkingDay>();

    List<BigDecimal> lstPayedHours = new ArrayList<BigDecimal>();

    for (Jobs job : jobs) {
        if (getWorkingDay(workingDays, job.getJobDate()) == null) {
            WorkingDay workingDay = new WorkingDay();
            workingDay.setDate(job.getJobDate());
            workingDay.setJobs(new ArrayList<Jobs>());
            workingDay.setActualWorkHours(new BigDecimal(0));
            workingDay.setOvertimeHours(new BigDecimal(0));
            workingDay.setOvertimeHoursPayed(new BigDecimal(0));

            workingDays.add(workingDay);
        }/* w w  w. j av a2s .c om*/

        if (job.getJobsGroup().getCompany() != null) {
            if (!lstPayedHours.contains(job.getJobsGroup().getCompany().getDailyPayedHours())) {
                lstPayedHours.add(job.getJobsGroup().getCompany().getDailyPayedHours());
            }
        }
    }

    BigDecimal payedHours = new BigDecimal(0);

    for (BigDecimal payedHoursFromList : lstPayedHours) {
        if (payedHoursFromList.compareTo(payedHours) > 0) {
            payedHours = payedHoursFromList;
        }
    }

    for (WorkingDay workingDay : workingDays) {
        for (Jobs job : jobs) {
            if (job.getJobDate().equals(workingDay.getDate())) {
                workingDay.getJobs().add(job);
                if (job.getPercentage() == null || job.getPercentage().equals(new BigDecimal(0))) {
                    workingDay.setActualWorkHours(workingDay.getActualWorkHours().add(job.getWorkedHours()));
                } else {
                    workingDay.setActualWorkHours(workingDay.getActualWorkHours().add(
                            job.getWorkedHours().multiply(job.getPercentage().divide(new BigDecimal(100)))));
                }
                workingDay.setOvertimeHours(workingDay.getActualWorkHours().subtract(payedHours));
            }
        }
    }

    return workingDays;
}

From source file:org.openvpms.archetype.test.ArchetypeServiceTest.java

/**
 * Verifies two <tt>BigDecimals</tt> are equal.
 *
 * @param expected the expected value. May be {@code null}
 * @param actual   the actual value. May be {@code null}
 *//*from   w  ww.  ja v  a 2  s . co m*/
protected void checkEquals(BigDecimal expected, BigDecimal actual) {
    if (expected == null) {
        assertNull(actual);
    } else if (actual == null || expected.compareTo(actual) != 0) {
        fail("Expected " + expected + ", but got " + actual);
    }
}

From source file:org.talend.dataprofiler.chart.preview.DQRuleItemLabelGenerator.java

/**
 * DOC yyin Comment method "stringformat".
 * /*from   w ww .j  a va  2s .c  o m*/
 * @param percent
 * @param i
 * @return
 */
private Object stringformat(Object percent, int i) {
    // ADD msjian TDQ-10793: when there is no data, the percent value is NaN
    if (Double.isNaN((double) percent)) {
        return String.valueOf(Double.NaN);
    }
    // TDQ-10793~

    BigDecimal zero = new BigDecimal(0);
    BigDecimal temp = new BigDecimal(percent.toString());
    BigDecimal min = new BigDecimal(10E-5);
    BigDecimal max = new BigDecimal(9999 * 10E-5);
    boolean isUseScientific = false;
    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
        isUseScientific = true;
    } else if (temp.compareTo(max) == 1 && temp.compareTo(new BigDecimal(1)) == -1) {
        percent = max.toString();
    }
    DecimalFormat format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH);
    format.applyPattern("0.00%"); //$NON-NLS-1$

    if (isUseScientific) {
        format.applyPattern("0.###E0%"); //$NON-NLS-1$
    }
    return format.format(new Double(percent.toString()));
}

From source file:org.apache.hadoop.hbase.client.coprocessor.BigDecimalColumnInterpreter.java

@Override
public int compare(BigDecimal val1, BigDecimal val2) {
    if ((((val1 == null) ? 1 : 0) ^ ((val2 == null) ? 1 : 0)) != 0) {
        return ((val1 == null) ? -1 : 1);
    }//from   ww  w .  j a  v  a 2 s  . co m
    if (val1 == null)
        return 0;
    return val1.compareTo(val2);
}

From source file:org.eel.kitchen.jsonschema.keyword.DivisibleByKeywordValidator.java

@Override
protected void validateDecimal(final ValidationReport report, final JsonNode instance) {
    final BigDecimal instanceValue = instance.decimalValue();
    final BigDecimal decimalValue = number.decimalValue();

    final BigDecimal remainder = instanceValue.remainder(decimalValue);

    /*// w  ww  .  j av  a  2s .co  m
     * We cannot use equality! As far as BigDecimal goes,
     * "0" and "0.0" are NOT equal. But .compareTo() returns the correct
     * result.
     */
    if (remainder.compareTo(BigDecimal.ZERO) == 0)
        return;

    final Message.Builder msg = newMsg().setMessage("number is not a multiple of divisibleBy")
            .addInfo("value", instance).addInfo("divisor", number);
    report.addMessage(msg.build());
}

From source file:org.apache.sling.reqanalyzer.impl.RequestAnalyzerWebConsole.java

private String formatByteSize(final long value) {
    final String suffix;
    final String suffixedValue;

    if (value >= 0) {
        final BigDecimal KB = new BigDecimal(1000L);
        final BigDecimal MB = new BigDecimal(1000L * 1000);
        final BigDecimal GB = new BigDecimal(1000L * 1000 * 1000);

        BigDecimal bd = new BigDecimal(value);
        if (bd.compareTo(GB) > 0) {
            bd = bd.divide(GB);//from  w w w  .ja  v a 2  s  . com
            suffix = "GB";
        } else if (bd.compareTo(MB) > 0) {
            bd = bd.divide(MB);
            suffix = "MB";
        } else if (bd.compareTo(KB) > 0) {
            bd = bd.divide(KB);
            suffix = "kB";
        } else {
            suffix = "B";
        }
        suffixedValue = bd.setScale(2, RoundingMode.UP).toString();
    } else {
        suffixedValue = "n/a";
        suffix = "";
    }

    return suffixedValue + suffix;
}

From source file:com.github.fge.jsonschema.keyword.validator.helpers.DivisorValidator.java

@Override
protected final void validateDecimal(final ProcessingReport report, final MessageBundle bundle,
        final FullData data) throws ProcessingException {
    final JsonNode node = data.getInstance().getNode();
    final BigDecimal instanceValue = node.decimalValue();
    final BigDecimal decimalValue = number.decimalValue();

    final BigDecimal remainder = instanceValue.remainder(decimalValue);

    /*/*  w w w  .  java 2s.  co  m*/
     * We cannot use equality! As far as BigDecimal goes,
     * "0" and "0.0" are NOT equal. But .compareTo() returns the correct
     * result.
     */
    if (remainder.compareTo(BigDecimal.ZERO) == 0)
        return;

    report.error(newMsg(data, bundle, "err.common.divisor.nonZeroRemainder").putArgument("value", node)
            .putArgument("divisor", number));
}

From source file:org.fineract.module.stellar.controller.BridgeController.java

@RequestMapping(value = "/vault/{assetCode}", method = RequestMethod.PUT, consumes = {
        "application/json" }, produces = { "application/json" })
public ResponseEntity<BigDecimal> adjustVaultIssuedAssets(
        @RequestHeader(API_KEY_HEADER_LABEL) final String apiKey,
        @RequestHeader(TENANT_ID_HEADER_LABEL) final String mifosTenantId,
        @PathVariable("assetCode") final String assetCode,
        @RequestBody final AmountConfiguration amountConfiguration) {
    this.securityService.verifyApiKey(apiKey, mifosTenantId);
    //TODO: add security for currency issuing.
    final BigDecimal amount = amountConfiguration.getAmount();
    if (amount.compareTo(BigDecimal.ZERO) < 0) {
        return new ResponseEntity<>(amount, HttpStatus.BAD_REQUEST);
    }//from  www  .jav  a2  s  .c  o  m

    final BigDecimal amountAdjustedTo = bridgeService.adjustVaultIssuedAssets(mifosTenantId, assetCode, amount);

    if (amountAdjustedTo.compareTo(amount) != 0)
        return new ResponseEntity<>(amountAdjustedTo, HttpStatus.CONFLICT);
    else
        return new ResponseEntity<>(amountAdjustedTo, HttpStatus.OK);
}