Example usage for java.lang Float NEGATIVE_INFINITY

List of usage examples for java.lang Float NEGATIVE_INFINITY

Introduction

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

Prototype

float NEGATIVE_INFINITY

To view the source code for java.lang Float NEGATIVE_INFINITY.

Click Source Link

Document

A constant holding the negative infinity of type float .

Usage

From source file:jp.furplag.util.commons.NumberUtilsTest.java

/**
 * {@link jp.furplag.util.commons.NumberUtils#isNaN(java.lang.Object)}.
 *///from  ww  w.  j a  v a 2  s.co  m
@Test
public void testIsNaNObject() {
    assertEquals("null", false, isNaN(null));
    assertEquals("empty", false, isNaN(""));
    assertEquals("invalidString", false, isNaN("not a number"));
    assertEquals("notParsable", false, isNaN(new int[] { 1, 2, 3 }));
    assertEquals("notParsable", false, isNaN(new float[] { Float.NaN }));
    assertEquals("Infinity", false, isNaN(Double.POSITIVE_INFINITY));
    assertEquals("-Infinity", false, isNaN(Float.NEGATIVE_INFINITY));
}

From source file:ml.shifu.shifu.core.dtrain.dt.DTWorker.java

/**
 * 'binBoundary' is ArrayList in fact, so we can use get method. ["-Infinity", 1d, 4d, ....]
 * //from  w  w  w  .  j  a  v  a 2 s  .c o m
 * @param value
 *            the value to be checked
 * @param binBoundary
 *            the bin boundary list
 * @return the index in which bin
 */
public static int getBinIndex(float value, List<Double> binBoundary) {
    if (binBoundary.size() <= 1) {
        // feature with binBoundary.size() <= 1 will not be send to worker, while such feature is still loading into
        // memory, just return the first bin index to avoid exception, while actually such feature isn't used in
        // GBT/RF.
        return 0;
    }

    // the last bin if positive infinity
    if (value == Float.POSITIVE_INFINITY) {
        return binBoundary.size() - 1;
    }

    // the first bin if negative infinity
    if (value == Float.NEGATIVE_INFINITY) {
        return 0;
    }

    int low = 0, high = binBoundary.size() - 1;
    while (low <= high) {
        int mid = (low + high) >>> 1;
        double lowThreshold = binBoundary.get(mid);
        double highThreshold = mid == binBoundary.size() - 1 ? Double.MAX_VALUE : binBoundary.get(mid + 1);
        if (value >= lowThreshold && value < highThreshold) {
            return mid;
        }
        if (value >= highThreshold) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return -1;
}

From source file:gdsc.core.filters.FilteredNonMaximumSuppression.java

/**
 * Expand the image to the new dimensions with a 1-pixel border
 *///from   w ww  . j a  v  a 2 s  .c o  m
private float[] expand(float[] data, int maxx, int maxy, int newx, int newy) {
    int size = newx * newy;

    if (!dataBuffer || newDataFloat == null || newDataFloat.length < size)
        newDataFloat = new float[size];

    // Zero first row
    for (int x = 0; x < newx; x++) {
        newDataFloat[x] = Float.NEGATIVE_INFINITY;
    }
    // Zero last rows
    for (int y = maxy + 1; y < newy; y++) {
        int newIndex = y * newx;
        for (int x = 0; x < newx; x++) {
            newDataFloat[newIndex++] = Float.NEGATIVE_INFINITY;
        }
    }

    int index = 0;
    for (int y = 0; y < maxy; y++) {
        int newIndex = (y + 1) * newx;

        // Zero first column
        newDataFloat[newIndex++] = Float.NEGATIVE_INFINITY;

        // Copy data
        for (int x = 0; x < maxx; x++) {
            newDataFloat[newIndex++] = data[index++];
        }

        // Zero remaining columns
        for (int x = maxx + 1; x < newx; x++)
            newDataFloat[newIndex++] = Float.NEGATIVE_INFINITY;
    }

    return newDataFloat;
}

From source file:net.pms.util.Rational.java

/**
 * Converts this {@link Rational} to a {@code float}. This conversion is
 * similar to the <i>narrowing primitive conversion</i> from {@code double}
 * to {@code float} as defined in section 5.1.3 of <cite>The Java&trade;
 * Language Specification</cite>: if this {@link Rational} has too great a
 * magnitude to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link Float#POSITIVE_INFINITY} as
 * appropriate.//from w  ww  .  j a v a 2 s. c  om
 * <p>
 * Note that even when the return value is finite, this conversion can lose
 * information about the precision of the {@link Rational} value.
 *
 * @return This {@link Rational} converted to a {@code float}.
 */
@Override
public float floatValue() {
    if (isNaN()) {
        return Float.NaN;
    }
    if (isInfinitePositive()) {
        return Float.POSITIVE_INFINITY;
    }
    if (isInfiniteNegative()) {
        return Float.NEGATIVE_INFINITY;
    }
    return new BigDecimal(reducedNumerator).divide(new BigDecimal(reducedDenominator), MathContext.DECIMAL32)
            .floatValue();
}

From source file:com.actelion.research.table.view.JVisualization.java

/**
 * Based on axis column assignments and on hvIndices of VisualizationPoints
 * this method assigns all visible VisualizationPoints to bars/pies and to color categories
 * within these bars/pies. It also calculates relative bar/pie sizes.
 * @param hvCount/*  w  w w.jav a  2  s . c o  m*/
 */
protected void calculateBarsOrPies() {
    calculateCategoryCounts(-1);

    Color[] colorList = mMarkerColor.getColorList();
    int focusFlagNo = getFocusFlag();
    int basicColorCount = colorList.length + 1;
    int colorCount = basicColorCount * ((focusFlagNo == -1) ? 1 : 2);

    int catCount = mCaseSeparationCategoryCount;
    for (int count : mCategoryVisibleCount)
        catCount *= count;

    mChartInfo = new CategoryViewInfo(mHVCount, catCount, colorCount);

    mChartInfo.barAxis = 0;
    for (int axis = 1; axis < mDimensions; axis++)
        if (mAxisIndex[axis] == cColumnUnassigned)
            mChartInfo.barAxis = axis;
        else if (mAxisIndex[mChartInfo.barAxis] != cColumnUnassigned
                && mCategoryVisibleCount[axis] <= mCategoryVisibleCount[mChartInfo.barAxis])
            mChartInfo.barAxis = axis;

    for (int i = 0; i < colorList.length; i++)
        mChartInfo.color[i] = colorList[i];
    mChartInfo.color[colorList.length] = VisualizationColor.cSelectedColor;
    if (focusFlagNo != -1) {
        for (int i = 0; i < colorList.length; i++)
            mChartInfo.color[i + basicColorCount] = VisualizationColor.grayOutColor(colorList[i]);
        mChartInfo.color[colorList.length + basicColorCount] = VisualizationColor
                .grayOutColor(VisualizationColor.cSelectedColor);
    }

    int visibleCount = 0;
    for (int i = 0; i < mDataPoints; i++) {
        if (isVisibleExcludeNaN(mPoint[i])) {
            int colorIndex = ((mPoint[i].record.getFlags() & CompoundRecord.cFlagMaskSelected) != 0
                    && mFocusHitlist != cFocusOnSelection) ? colorList.length : mPoint[i].colorIndex;
            if (focusFlagNo != -1 && !mPoint[i].record.isFlagSet(focusFlagNo))
                colorIndex += basicColorCount;

            int cat = getChartCategoryIndex(mPoint[i]);
            mChartInfo.pointsInCategory[mPoint[i].hvIndex][cat]++;
            mChartInfo.pointsInColorCategory[mPoint[i].hvIndex][cat][colorIndex]++;
            visibleCount++;
            switch (mChartMode) {
            case cChartModeCount:
            case cChartModePercent:
                mChartInfo.barValue[mPoint[i].hvIndex][cat]++;
                break;
            case cChartModeMean:
            case cChartModeSum:
                mChartInfo.barValue[mPoint[i].hvIndex][cat] += mPoint[i].record.getDouble(mChartColumn);
                break;
            case cChartModeMin:
            case cChartModeMax:
                float value = mPoint[i].record.getDouble(mChartColumn);
                if (mChartInfo.pointsInCategory[mPoint[i].hvIndex][cat] == 1)
                    mChartInfo.barValue[mPoint[i].hvIndex][cat] = value;
                else if (mChartMode == cChartModeMin)
                    mChartInfo.barValue[mPoint[i].hvIndex][cat] = Math
                            .min(mChartInfo.barValue[mPoint[i].hvIndex][cat], value);
                else
                    mChartInfo.barValue[mPoint[i].hvIndex][cat] = Math
                            .max(mChartInfo.barValue[mPoint[i].hvIndex][cat], value);
                break;
            }
        }
    }
    if (mChartMode == cChartModePercent)
        for (int i = 0; i < mHVCount; i++)
            for (int j = 0; j < catCount; j++)
                mChartInfo.barValue[i][j] *= 100f / visibleCount;
    if (mChartMode == cChartModeMean)
        for (int i = 0; i < mHVCount; i++)
            for (int j = 0; j < catCount; j++)
                if (mChartInfo.pointsInCategory[i][j] != 0)
                    mChartInfo.barValue[i][j] /= mChartInfo.pointsInCategory[i][j];

    int[][][] count = new int[mHVCount][catCount][colorCount];
    for (int hv = 0; hv < mHVCount; hv++)
        for (int cat = 0; cat < catCount; cat++)
            for (int color = 1; color < colorCount; color++)
                count[hv][cat][color] = count[hv][cat][color - 1]
                        + mChartInfo.pointsInColorCategory[hv][cat][color - 1];
    for (int i = 0; i < mDataPoints; i++) {
        if (isVisibleExcludeNaN(mPoint[i])) {
            int colorIndex = (mPoint[i].record.isSelected() && mFocusHitlist != cFocusOnSelection)
                    ? colorList.length
                    : mPoint[i].colorIndex;
            if (focusFlagNo != -1 && !mPoint[i].record.isFlagSet(focusFlagNo))
                colorIndex += basicColorCount;

            int hv = mPoint[i].hvIndex;
            int cat = getChartCategoryIndex(mPoint[i]);
            mPoint[i].chartGroupIndex = count[hv][cat][colorIndex];
            count[hv][cat][colorIndex]++;
        }
    }

    float dataMin = Float.POSITIVE_INFINITY;
    float dataMax = Float.NEGATIVE_INFINITY;
    for (int i = 0; i < mHVCount; i++) {
        for (int j = 0; j < catCount; j++) {
            if (mChartInfo.pointsInCategory[i][j] != 0) {
                if (dataMin > mChartInfo.barValue[i][j])
                    dataMin = mChartInfo.barValue[i][j];
                if (dataMax < mChartInfo.barValue[i][j])
                    dataMax = mChartInfo.barValue[i][j];
            }
        }
    }
    mChartInfo.barOrPieDataAvailable = (dataMin != Float.POSITIVE_INFINITY);

    if (mChartInfo.barOrPieDataAvailable) {
        switch (mChartMode) {
        case cChartModeCount:
        case cChartModePercent:
            mChartInfo.axisMin = 0.0f;
            mChartInfo.axisMax = dataMax * cBarSpacingFactor;
            mChartInfo.barBase = 0.0f;
            break;
        default:
            if (mTableModel.isLogarithmicViewMode(mChartColumn)) {
                float dataRange = dataMax - dataMin;
                mChartInfo.axisMin = dataMin - dataRange * (cBarSpacingFactor - 1.0f);
                mChartInfo.axisMax = dataMax + dataRange * (cBarSpacingFactor - 1.0f);
                mChartInfo.barBase = mChartInfo.axisMin;
            } else {
                if (dataMin >= 0.0) {
                    mChartInfo.axisMin = 0.0f;
                    mChartInfo.axisMax = dataMax * cBarSpacingFactor;
                } else if (dataMax <= 0.0) {
                    mChartInfo.axisMin = dataMin * cBarSpacingFactor;
                    mChartInfo.axisMax = 0.0f;
                } else {
                    float dataRange = dataMax - dataMin;
                    float spacing = (cBarSpacingFactor - 1.0f) * dataRange / 2.0f;
                    mChartInfo.axisMax = dataMax + spacing;
                    mChartInfo.axisMin = dataMin - spacing;
                }
                mChartInfo.barBase = 0.0f;
            }
            break;
        }
    } else {
        mChartInfo.axisMin = 0.0f;
        mChartInfo.axisMax = cBarSpacingFactor;
        mChartInfo.barBase = 0.0f;
    }
    //System.out.println("calculateBarsOrPies() dataMin:"+mChartInfo.dataMin+" dataMax:"+mChartInfo.dataMax+" axisMin:"+mChartInfo.axisMin+" axisMax:"+mChartInfo.axisMax+" barBase:"+mChartInfo.barBase);
}

From source file:jp.furplag.util.commons.NumberUtilsTest.java

/**
 * {@link jp.furplag.util.commons.NumberUtils.NumberObject}
 *///  w  w  w  .ja  v a2  s. c o m
@SuppressWarnings("unchecked")
@Test
public void NumberObjectTest() {
    try {
        Class<?> numberObject = ClassLoader.getSystemClassLoader()
                .loadClass(NumberUtils.class.getName() + "$NumberObject");

        Constructor<?> c = numberObject.getDeclaredConstructor(Class.class);
        c.setAccessible(true);

        Method ofType = numberObject.getMethod("of", Class.class);
        ofType.setAccessible(true);

        Method ofN = numberObject.getMethod("of", Number.class);
        ofN.setAccessible(true);

        Method parsable = numberObject.getDeclaredMethod("parsable", Number.class);
        parsable.setAccessible(true);

        Method contains = numberObject.getDeclaredMethod("contains", Number.class);
        contains.setAccessible(true);

        Method valueOf = numberObject.getDeclaredMethod("valueOf", Number.class);
        valueOf.setAccessible(true);

        for (Class<?> type : NUMBERS) {
            Object o = c.newInstance(type);
            Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type);
            Object numob = ofType.invoke(null, type);
            assertEquals("ofType: " + type.getSimpleName(), o, numob);
            Number n = null;
            if (!type.isPrimitive()) {
                if (ClassUtils.isPrimitiveWrapper(type)) {
                    n = (Number) ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class)
                            .invoke(null, "1");
                } else {
                    n = (Number) type.getField("ONE").get(null);
                }
                if (type.equals(byte.class))
                    assertEquals("ofN: 1: " + type.getSimpleName(), o, ofN.invoke(null, n));
            }
            assertEquals("parsable: -1: " + type.getSimpleName(), true, parsable.invoke(numob, -1));
            assertEquals("parsable: 0: " + type.getSimpleName(), true, parsable.invoke(numob, 0));
            assertEquals("parsable: 1: " + type.getSimpleName(), true, parsable.invoke(numob, 1));

            assertEquals("parsable: null: " + type.getSimpleName(), !type.isPrimitive(),
                    parsable.invoke(numob, (Number) null));

            Object expected = ObjectUtils.isAny(wrapper, Float.class, Double.class, BigDecimal.class,
                    BigInteger.class);
            assertEquals("parsable: Infinity: Double: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, Double.POSITIVE_INFINITY));
            assertEquals("parsable: Infinity: Double: BigDecimal: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_DOUBLE));
            assertEquals("parsable: Infinity: Double: BigInteger: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_DOUBLE.toBigInteger()));
            assertEquals("parsable: Infinity: Float: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, Float.POSITIVE_INFINITY));
            assertEquals("parsable: Infinity: Float: BigDecimal: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_FLOAT));
            assertEquals("parsable: Infinity: Float: BigInteger: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_FLOAT.toBigInteger()));
            assertEquals("parsable: -Infinity: Double: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, Double.NEGATIVE_INFINITY));
            assertEquals("parsable: -Infinity: Double: BigDecimal: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_DOUBLE.negate()));
            assertEquals("parsable: -Infinity: Double: BigInteger: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_DOUBLE.negate().toBigInteger()));
            assertEquals("parsable: -Infinity: Float: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, Float.NEGATIVE_INFINITY));
            assertEquals("parsable: -Infinity: Float: BigDecimal: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_FLOAT.negate()));
            assertEquals("parsable: -Infinity: Float: BigInteger: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, INFINITY_FLOAT.negate().toBigInteger()));

            expected = ObjectUtils.isAny(wrapper, Float.class, Double.class);
            assertEquals("parsable: NaN: Float: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, Float.NaN));
            assertEquals("parsable: NaN: Double: " + type.getSimpleName(), expected,
                    parsable.invoke(numob, Double.NaN));

            if (Byte.class.equals(wrapper)) {
                assertEquals("parsable: contains: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null)));
                assertEquals("parsable: contains: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null)));
                assertEquals("parsable: overflow: min: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Short.MIN_VALUE));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Short.MAX_VALUE));
                assertEquals("parsable: fraction: " + type.getSimpleName(), false,
                        parsable.invoke(numob, 123.456f));

                assertEquals("contains: min: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null)));
                assertEquals("contains: max: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null)));
                assertEquals("contains: overflow: min: " + type.getSimpleName(), false,
                        contains.invoke(numob, Short.MIN_VALUE));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, Short.MAX_VALUE));
                assertEquals("contains: fraction: " + type.getSimpleName(), true,
                        contains.invoke(numob, 123.456f));
                assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false,
                        contains.invoke(numob, 1234.56f));
            }
            if (Short.class.equals(wrapper)) {
                assertEquals("parsable: contains: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null)));
                assertEquals("parsable: contains: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null)));
                assertEquals("parsable: overflow: min: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Integer.MIN_VALUE));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Integer.MAX_VALUE));
                assertEquals("parsable: fraction: " + type.getSimpleName(), false,
                        parsable.invoke(numob, 123.456f));

                assertEquals("contains: min: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null)));
                assertEquals("contains: max: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null)));
                assertEquals("contains: overflow: min: " + type.getSimpleName(), false,
                        contains.invoke(numob, Integer.MIN_VALUE));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, Integer.MAX_VALUE));
                assertEquals("contains: fraction: " + type.getSimpleName(), true,
                        contains.invoke(numob, 12345.6f));
                assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false,
                        contains.invoke(numob, 123456.789f));
            }
            if (Integer.class.equals(wrapper)) {
                assertEquals("parsable: contains: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null)));
                assertEquals("parsable: contains: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null)));
                assertEquals("parsable: overflow: min: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Long.MIN_VALUE));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Long.MAX_VALUE));
                assertEquals("parsable: fraction: " + type.getSimpleName(), false,
                        parsable.invoke(numob, 123456.789f));

                assertEquals("contains: min: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null)));
                assertEquals("contains: max: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null)));
                assertEquals("contains: overflow: min: " + type.getSimpleName(), false,
                        contains.invoke(numob, Long.MIN_VALUE));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, Long.MAX_VALUE));
                assertEquals("contains: fraction: " + type.getSimpleName(), true,
                        contains.invoke(numob, 123456.789f));
                assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false,
                        contains.invoke(numob, 12345678912345678912.3456d));
            }
            if (Long.class.equals(wrapper)) {
                assertEquals("parsable: contains: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null)));
                assertEquals("parsable: contains: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null)));
                assertEquals("parsable: overflow: min: " + type.getSimpleName(), false,
                        parsable.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2)));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), false,
                        parsable.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2)));
                assertEquals("parsable: fraction: " + type.getSimpleName(), false,
                        parsable.invoke(numob, 123.456f));

                assertEquals("contains: min: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null)));
                assertEquals("contains: max: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null)));
                assertEquals("contains: overflow: min: " + type.getSimpleName(), false,
                        contains.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2)));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2)));
                assertEquals("contains: fraction: " + type.getSimpleName(), true,
                        contains.invoke(numob, 123456.789f));
                assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false,
                        contains.invoke(numob, 12345678912345678912.3456f));
            }
            if (Float.class.equals(wrapper)) {
                assertEquals("parsable: contains: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null)));
                assertEquals("parsable: contains: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null)));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), false,
                        parsable.invoke(numob, -Double.MAX_VALUE));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), false,
                        parsable.invoke(numob, Double.MAX_VALUE));

                assertEquals("contains: min: " + type.getSimpleName(), true,
                        contains.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null)));
                assertEquals("contains: max: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null)));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, -Double.MAX_VALUE));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, Double.MAX_VALUE));
            }
            if (Double.class.equals(wrapper)) {
                assertEquals("parsable: contains: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null)));
                assertEquals("parsable: contains: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null)));
                assertEquals("parsable: overflow: min: " + type.getSimpleName(), true,
                        parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate()));
                assertEquals("parsable: overflow: max: " + type.getSimpleName(), true,
                        parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN)));

                assertEquals("contains: min: " + type.getSimpleName(), true,
                        contains.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null)));
                assertEquals("contains: max: " + type.getSimpleName(), true,
                        contains.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null)));
                assertEquals("contains: overflow: min: " + type.getSimpleName(), false,
                        contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate()));
                assertEquals("contains: overflow: max: " + type.getSimpleName(), false,
                        contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN)));
            }
            if (!ClassUtils.isPrimitiveWrapper(wrapper)) {
                assertEquals("parsable: fraction: " + type.getSimpleName(), BigDecimal.class.equals(type),
                        parsable.invoke(numob, 123.456f));
                assertEquals("contains: fraction: " + type.getSimpleName(), true,
                        contains.invoke(numob, 123.456f));
            }

            if (ClassUtils.isPrimitiveWrapper(wrapper)) {
                expected = wrapper.getMethod("valueOf", String.class).invoke(null, "123");
            } else {
                expected = new BigDecimal("123");
                if (BigInteger.class.equals(wrapper))
                    expected = ((BigDecimal) expected).toBigInteger();
            }
            for (Class<?> valueType : OBJECTS) {
                if (ClassUtils.isPrimitiveWrapper(valueType)) {
                    n = (Number) valueType.getMethod("valueOf", String.class).invoke(null, "123");
                } else {
                    n = new BigDecimal("123");
                    if (BigInteger.class.equals(valueType))
                        n = ((BigDecimal) n).toBigInteger();
                }
                assertEquals(
                        "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(),
                        expected, valueOf.invoke(numob, n));
                assertEquals(
                        "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: "
                                + type.getSimpleName(),
                        expected.getClass(), valueOf.invoke(numob, n).getClass());
            }

            n = 123.456f;
            if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) {
                expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString());
            } else if (ClassUtils.isPrimitiveWrapper(wrapper)) {
                expected = wrapper.getMethod("valueOf", String.class).invoke(null,
                        Integer.toString(((Float) n).intValue()));
            } else {
                expected = new BigDecimal(n.toString());
                if (BigInteger.class.equals(wrapper))
                    expected = ((BigDecimal) expected).toBigInteger();
            }
            assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(),
                    expected, valueOf.invoke(numob, n));
            assertEquals(
                    "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(),
                    expected.getClass(), valueOf.invoke(numob, n).getClass());

            n = 1.23456789E-6d;
            if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) {
                expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString());
            } else if (ClassUtils.isPrimitiveWrapper(wrapper)) {
                expected = wrapper.getMethod("valueOf", String.class).invoke(null,
                        Integer.toString(((Double) n).intValue()));
            } else {
                expected = new BigDecimal(n.toString());
                if (BigInteger.class.equals(wrapper))
                    expected = ((BigDecimal) expected).toBigInteger();
            }
            assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(),
                    expected, valueOf.invoke(numob, n));
            assertEquals(
                    "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(),
                    expected.getClass(), valueOf.invoke(numob, n).getClass());

            n = INFINITY_DOUBLE.pow(2);
            if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) {
                expected = wrapper.getField("POSITIVE_INFINITY").get(null);
            } else if (ClassUtils.isPrimitiveWrapper(wrapper)) {
                expected = wrapper.getField("MAX_VALUE").get(null);
            } else {
                expected = new BigDecimal(n.toString());
                if (BigInteger.class.equals(wrapper))
                    expected = ((BigDecimal) expected).toBigInteger();
            }
            assertEquals("valueOf: Huge: " + type.getSimpleName(), expected, valueOf.invoke(numob, n));
            assertEquals("valueOf: Huge: class: " + type.getSimpleName(), expected.getClass(),
                    valueOf.invoke(numob, n).getClass());

            n = INFINITY_DOUBLE.pow(2).negate();
            if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) {
                expected = wrapper.getField("NEGATIVE_INFINITY").get(null);
            } else if (ClassUtils.isPrimitiveWrapper(wrapper)) {
                expected = wrapper.getField("MIN_VALUE").get(null);
            } else {
                expected = new BigDecimal(n.toString());
                if (BigInteger.class.equals(wrapper))
                    expected = ((BigDecimal) expected).toBigInteger();
            }
            assertEquals("valueOf: Huge: negative: " + type.getSimpleName(), expected,
                    valueOf.invoke(numob, n));
            assertEquals("valueOf: Huge: negative: class: " + type.getSimpleName(), expected.getClass(),
                    valueOf.invoke(numob, n).getClass());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace()));
    }
}

From source file:org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.java

public float getAverageRating(int apiId) throws APIManagementException {
    Connection conn = null;//from w w w. ja v  a 2s . com
    float avrRating = 0;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = APIMgtDBUtil.getConnection();
        conn.setAutoCommit(false);

        if (apiId == -1) {
            String msg = "Invalid APIId : " + apiId;
            log.error(msg);
            return Float.NEGATIVE_INFINITY;
        }
        //This query to update the AM_API_RATINGS table
        String sqlQuery = SQLConstants.GET_AVERAGE_RATING_SQL;

        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, apiId);
        rs = ps.executeQuery();

        while (rs.next()) {
            avrRating = rs.getFloat("RATING");
        }
    } catch (SQLException e) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                log.error("Failed to rollback getting user ratings ", e1);
            }
        }
        handleException("Failed to get user ratings", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return avrRating;
}

From source file:org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.java

/**
 * @param apiIdentifier API Identifier//from  w w w. j  a  v a 2  s  .c om
 * @throws APIManagementException if failed to add Application
 */
public float getAverageRating(APIIdentifier apiIdentifier, Connection conn)
        throws APIManagementException, SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    float avrRating = 0;
    try {
        //Get API Id
        int apiId;
        apiId = getAPIID(apiIdentifier, conn);
        if (apiId == -1) {
            String msg = "Could not load API record for: " + apiIdentifier.getApiName();
            log.error(msg);
            return Float.NEGATIVE_INFINITY;
        }
        //This query to update the AM_API_RATINGS table
        String sqlQuery = SQLConstants.GET_AVERAGE_RATING_SQL;

        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, apiId);
        rs = ps.executeQuery();

        while (rs.next()) {
            avrRating = rs.getFloat("RATING");
        }

    } catch (SQLException e) {
        handleException("Failed to add Application", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, null, rs);
    }

    BigDecimal decimal = new BigDecimal(avrRating);
    return Float.parseFloat(decimal.setScale(1, BigDecimal.ROUND_UP).toString());
}