Example usage for java.lang Float isInfinite

List of usage examples for java.lang Float isInfinite

Introduction

In this page you can find the example usage for java.lang Float isInfinite.

Prototype

public boolean isInfinite() 

Source Link

Document

Returns true if this Float value is infinitely large in magnitude, false otherwise.

Usage

From source file:eu.uqasar.util.UQasarUtil.java

/**
 * Traverse the tree in postorder and update tree values
 * @param node// w w  w . j av  a  2s  .  c om
 */
private static void postorder(TreeNode node) {

    if (node == null) {
        return;
    }

    logger.debug("------------postorder: " + node.getName() + "---------------");

    // Iterate the node children
    for (Object o : node.getChildren()) {
        TreeNode nodeChild = (TreeNode) o;
        UQasarUtil.postorder(nodeChild);
    }
    logger.debug("Traversing project tree in postorder..." + node.toString());
    // Update the value
    try {
        InitialContext ic = new InitialContext();
        AdapterDataService adapterDataService = new AdapterDataService();
        TreeNodeService treeNodeService = (TreeNodeService) ic.lookup("java:module/TreeNodeService");

        if (node instanceof Metric) {
            Metric metric = (Metric) node;
            logger.debug("Recomputing the value of the Metric " + node);
            Float value = null;
            if (metric.getMetricSource() == MetricSource.Manual) {
                metric.updateQualityStatus();
            } else {
                value = adapterDataService.getMetricValue(metric.getMetricSource(), metric.getMetricType(),
                        metric.getProject());
                metric.setValue(value);
            }
            metric.setLastUpdated(getLatestTreeUpdateDate());
            metric.addHistoricValue();
            // End Metric node treatment 
        } else if (node instanceof QualityIndicator) {
            logger.info("Recomputing the value of the Quality Indicator " + node);
            QualityIndicator qi = (QualityIndicator) node;
            if (qi.getUseFormula()) {
                String formulaToEval = Formula.parseFormula(qi.getViewFormula());
                if (formulaToEval != null && !formulaToEval.isEmpty()) {

                    Float computedValue = Formula.evalFormula(formulaToEval);
                    if (computedValue != null && !computedValue.isNaN()) {
                        qi.setValue(computedValue);
                        qi.setLastUpdated(getLatestTreeUpdateDate());
                        treeNodeService.update(qi);
                    }

                }
            } else {
                float achieved = 0;
                float denominator = 0;
                for (final TreeNode me : qi.getChildren()) {
                    float weight = ((Metric) me).getWeight();
                    if (me.getQualityStatus() == QualityStatus.Green) {
                        achieved += weight;
                    }
                    denominator += weight;
                }
                if (denominator == 0)
                    qi.getChildren().size();
                qi.setValue(achieved * 100 / denominator);
            }
            qi.setLastUpdated(getLatestTreeUpdateDate());
            qi.addHistoricValue();
            // End Q.Indicator node treatment 
        } else if (node instanceof QualityObjective) {
            logger.info("Recomputing the value of the Quality Objective " + node);
            QualityObjective qo = (QualityObjective) node;
            if (qo.getUseFormula()) {
                String formulaToEval = Formula.parseFormula(qo.getViewFormula());
                if (formulaToEval != null && !formulaToEval.isEmpty()) {

                    Float computedValue = Formula.evalFormula(formulaToEval);
                    if (computedValue != null && !computedValue.isNaN()) {
                        qo.setValue(computedValue);
                        qo.setLastUpdated(getLatestTreeUpdateDate());
                    }

                }
            } else {
                float denominator = 0;
                float achieved = 0;
                for (final TreeNode qi : qo.getChildren()) {
                    float weight = ((QualityIndicator) qi).getWeight();
                    if (qi.getQualityStatus() == QualityStatus.Green) {
                        achieved += weight;
                    }
                    denominator += weight;
                }
                qo.setValue(achieved * 100 / denominator);
            }
            qo.setLastUpdated(getLatestTreeUpdateDate());
            qo.addHistoricValue();
            // End Quality Objective node treatment 
        } else if (node instanceof Project) {
            logger.info("Recomputing the value of the Project " + node);
            Project prj = (Project) node;
            double qoValueSum = 0;
            double denominator = 0;
            for (Object o : node.getChildren()) {
                QualityObjective qo = (QualityObjective) o;
                if (qo.getWeight() == 0) {
                    continue;
                }
                qoValueSum += qo.getValue() * (prj.isFormulaAverage() ? qo.getWeight() : 1);
                denominator += prj.isFormulaAverage() ? qo.getWeight() : 1;
            }

            // bad idea to divide something under 0
            if (denominator == 0) {
                denominator = 1;
            }

            Double computedValue = qoValueSum / denominator;

            if (computedValue != null && !computedValue.isNaN() && !computedValue.isInfinite()) {
                prj.setValue(computedValue);
            }

            prj.setLastUpdated(getLatestTreeUpdateDate());
            prj.addHistoricValue();
            logger.debug(" [" + qoValueSum + "] denominator [" + denominator + "] " + computedValue);
            // End Project node treatment 
        }

        // Get a (possible) suggestion for the tree node
        Multimap<?, ?> suggestions = getSuggestionForNode(node);
        //TODO: take all the suggestions into account
        Object[] types = suggestions.keys().toArray();
        Object[] suggestionsValues = suggestions.values().toArray();
        if (types.length > 0) {
            // for now use the first item as suggestion
            SuggestionType stype = (SuggestionType) types[0];
            node.setSuggestionType(stype);
            if (suggestionsValues[0] != null && !suggestionsValues[0].equals("")) {
                node.setSuggestionValue((String) suggestionsValues[0]);
            }
        }

        treeNodeService.update(node);

    } catch (NamingException e) {
        e.printStackTrace();
    }

    return;
}

From source file:eu.uqasar.util.UQasarUtil.java

/**
 * Traverse the tree in postorder and update tree values
 * @param node/*w  w w. j a v  a  2s . c om*/
 */
private static void postorderWithParticularNode(TreeNode node, TreeNode projectTreeNode) {

    if (node == null) {
        return;
    }

    if (projectTreeNode == null) {
        return;
    }

    logger.debug("------------postorder: " + projectTreeNode.getName() + "---------------");

    logger.debug("Traversing project tree in postorder..." + projectTreeNode.toString());
    // Update the value
    try {
        InitialContext ic = new InitialContext();
        AdapterDataService adapterDataService = new AdapterDataService();
        TreeNodeService treeNodeService = (TreeNodeService) ic.lookup("java:module/TreeNodeService");

        if (projectTreeNode instanceof Metric) {
            Metric metric = (Metric) projectTreeNode;
            logger.debug("Recomputing the value of the Metric " + projectTreeNode);
            Float value = null;
            if (metric.getMetricSource() == MetricSource.Manual) {
                metric.updateQualityStatus();
            } else {
                value = adapterDataService.getMetricValue(metric.getMetricSource(), metric.getMetricType(),
                        metric.getProject());
                metric.setValue(value);
            }
            metric.setLastUpdated(getLatestTreeUpdateDate());
            metric.addHistoricValue();
            // End Metric node treatment 
        } else if (projectTreeNode instanceof QualityIndicator) {
            logger.info("Recomputing the value of the Quality Indicator " + projectTreeNode);
            QualityIndicator qi = (QualityIndicator) projectTreeNode;
            if (qi.getUseFormula()) {
                String formulaToEval = Formula.parseFormula(qi.getViewFormula());
                if (formulaToEval != null && !formulaToEval.isEmpty()) {

                    Float computedValue = Formula.evalFormula(formulaToEval);
                    if (computedValue != null && !computedValue.isNaN()) {
                        qi.setValue(computedValue);
                        qi.setLastUpdated(getLatestTreeUpdateDate());
                        treeNodeService.update(qi);
                    }

                }
            } else {
                float achieved = 0;
                float denominator = 0;
                for (final TreeNode me : qi.getChildren()) {
                    float weight = ((Metric) me).getWeight();
                    if (me.getQualityStatus() == QualityStatus.Green) {
                        achieved += weight;
                    }
                    denominator += weight;
                }
                if (denominator == 0)
                    qi.getChildren().size();
                qi.setValue(achieved * 100 / denominator);
            }
            qi.setLastUpdated(getLatestTreeUpdateDate());
            qi.addHistoricValue();
            // End Q.Indicator node treatment 
        } else if (projectTreeNode instanceof QualityObjective) {
            logger.info("Recomputing the value of the Quality Objective " + projectTreeNode);
            QualityObjective qo = (QualityObjective) projectTreeNode;
            if (qo.getUseFormula()) {
                String formulaToEval = Formula.parseFormula(qo.getViewFormula());
                if (formulaToEval != null && !formulaToEval.isEmpty()) {

                    Float computedValue = Formula.evalFormula(formulaToEval);
                    if (computedValue != null && !computedValue.isNaN()) {
                        qo.setValue(computedValue);
                        qo.setLastUpdated(getLatestTreeUpdateDate());
                    }

                }
            } else {
                float denominator = 0;
                float achieved = 0;
                for (final TreeNode qi : qo.getChildren()) {
                    float weight = ((QualityIndicator) qi).getWeight();
                    if (qi.getQualityStatus() == QualityStatus.Green) {
                        achieved += weight;
                    }
                    denominator += weight;
                }
                qo.setValue(achieved * 100 / denominator);
            }
            qo.setLastUpdated(getLatestTreeUpdateDate());
            qo.addHistoricValue();
            // End Quality Objective node treatment 
        } else if (projectTreeNode instanceof Project) {
            logger.info("Recomputing the value of the Project " + projectTreeNode);
            Project prj = (Project) projectTreeNode;
            double qoValueSum = 0;
            double denominator = 0;
            for (Object o : projectTreeNode.getChildren()) {
                QualityObjective qo = (QualityObjective) o;
                if (qo.getWeight() == 0) {
                    continue;
                }
                qoValueSum += qo.getValue() * (prj.isFormulaAverage() ? qo.getWeight() : 1);
                denominator += prj.isFormulaAverage() ? qo.getWeight() : 1;
            }

            // bad idea to divide something under 0
            if (denominator == 0) {
                denominator = 1;
            }

            Double computedValue = qoValueSum / denominator;

            if (computedValue != null && !computedValue.isNaN() && !computedValue.isInfinite()) {
                prj.setValue(computedValue);
            }

            prj.setLastUpdated(getLatestTreeUpdateDate());
            prj.addHistoricValue();
            logger.debug(" [" + qoValueSum + "] denominator [" + denominator + "] " + computedValue);
            // End Project node treatment 
        }

        // Get a (possible) suggestion for the tree node
        Multimap<?, ?> suggestions = getSuggestionForNode(projectTreeNode);
        //TODO: take all the suggestions into account
        Object[] types = suggestions.keys().toArray();
        Object[] suggestionsValues = suggestions.values().toArray();
        if (types.length > 0) {
            // for now use the first item as suggestion
            SuggestionType stype = (SuggestionType) types[0];
            projectTreeNode.setSuggestionType(stype);
            if (suggestionsValues[0] != null && !suggestionsValues[0].equals("")) {
                projectTreeNode.setSuggestionValue((String) suggestionsValues[0]);
            }
        }

        treeNodeService.update(projectTreeNode);

    } catch (NamingException e) {
        e.printStackTrace();
    }

    // Iterate the node children
    TreeNode nodeChild = projectTreeNode.getParent();
    UQasarUtil.postorderWithParticularNode(projectTreeNode, nodeChild);

    return;
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  w ww  .  j av  a 2  s. c o m
 * Turns a string value into a java.lang.Number.
 * </p>
 * 
 * <p>
 * First, the value is examined for a type qualifier on the end (
 * <code>'f','F','d','D','l','L'</code>). If it is found, it starts trying
 * to create successively larger types from the type specified until one is
 * found that can represent the value.
 * </p>
 * 
 * <p>
 * If a type specifier is not found, it will check for a decimal point and
 * then try successively larger types from <code>Integer</code> to
 * <code>BigInteger</code> and from <code>Float</code> to
 * <code>BigDecimal</code>.
 * </p>
 * 
 * <p>
 * If the string starts with <code>0x</code> or <code>-0x</code>, it will be
 * interpreted as a hexadecimal integer. Values with leading <code>0</code>
 * 's will not be interpreted as octal.
 * </p>
 * 
 * <p>
 * Returns <code>null</code> if the string is <code>null</code>.
 * </p>
 * 
 * <p>
 * This method does not trim the input string, i.e., strings with leading or
 * trailing spaces will generate NumberFormatExceptions.
 * </p>
 * 
 * @param str
 *            String containing a number, may be null
 * @return Number created from the string
 * @throws NumberFormatException
 *             if the value cannot be converted
 */
public static Number createNumber(String str) throws NumberFormatException {
    if (str == null) {
        return null;
    }
    if (isBlank(str)) {
        throw new NumberFormatException("A blank string is not a valid number");
    }
    if (str.startsWith("--")) {
        // this is protection for poorness in java.lang.BigDecimal.
        // it accepts this as a legal value, but it does not appear
        // to be in specification of class. OS X Java parses it to
        // a wrong value.
        return null;
    }
    if (str.startsWith("0x") || str.startsWith("-0x")) {
        return createInteger(str);
    }
    char lastChar = str.charAt(str.length() - 1);
    String mant;
    String dec;
    String exp;
    int decPos = str.indexOf('.');
    int expPos = str.indexOf('e') + str.indexOf('E') + 1;

    if (decPos > -1) {

        if (expPos > -1) {
            if (expPos < decPos) {
                throw new NumberFormatException(str + " is not a valid number.");
            }
            dec = str.substring(decPos + 1, expPos);
        } else {
            dec = str.substring(decPos + 1);
        }
        mant = str.substring(0, decPos);
    } else {
        if (expPos > -1) {
            mant = str.substring(0, expPos);
        } else {
            mant = str;
        }
        dec = null;
    }
    if (!Character.isDigit(lastChar) && lastChar != '.') {
        if (expPos > -1 && expPos < str.length() - 1) {
            exp = str.substring(expPos + 1, str.length() - 1);
        } else {
            exp = null;
        }
        // Requesting a specific type..
        String numeric = str.substring(0, str.length() - 1);
        boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
        switch (lastChar) {
        case 'l':
        case 'L':
            if (dec == null && exp == null
                    && (numeric.charAt(0) == '-' && isDigits(numeric.substring(1)) || isDigits(numeric))) {
                try {
                    return createLong(numeric);
                } catch (NumberFormatException nfe) {
                    // Too big for a long
                }
                return createBigInteger(numeric);

            }
            throw new NumberFormatException(str + " is not a valid number.");
        case 'f':
        case 'F':
            try {
                Float f = createFloat(numeric);
                if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                    // If it's too big for a float or the float value =
                    // 0
                    // and the string
                    // has non-zeros in it, then float does not have the
                    // precision we want
                    return f;
                }

            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            //$FALL-THROUGH$
        case 'd':
        case 'D':
            try {
                Double d = createDouble(numeric);
                if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
                    return d;
                }
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            try {
                return createBigDecimal(numeric);
            } catch (NumberFormatException e) {
                // ignore the bad number
            }
            //$FALL-THROUGH$
        default:
            throw new NumberFormatException(str + " is not a valid number.");

        }
    } else {
        // User doesn't have a preference on the return type, so let's start
        // small and go from there...
        if (expPos > -1 && expPos < str.length() - 1) {
            exp = str.substring(expPos + 1, str.length());
        } else {
            exp = null;
        }
        if (dec == null && exp == null) {
            // Must be an int,long,bigint
            try {
                return createInteger(str);
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            try {
                return createLong(str);
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            return createBigInteger(str);

        } else {
            // Must be a float,double,BigDec
            boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
            try {
                Float f = createFloat(str);
                if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                    return f;
                }
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            try {
                Double d = createDouble(str);
                if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                    return d;
                }
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }

            return createBigDecimal(str);

        }
    }
}