Example usage for org.apache.commons.lang.math FloatRange getMaximumFloat

List of usage examples for org.apache.commons.lang.math FloatRange getMaximumFloat

Introduction

In this page you can find the example usage for org.apache.commons.lang.math FloatRange getMaximumFloat.

Prototype

public float getMaximumFloat() 

Source Link

Document

Gets the maximum number in this range as a float.

Usage

From source file:au.org.ala.delta.model.DiffUtils.java

private static boolean doCompareRealForTaxa(List<RealAttribute> attrs, boolean matchUnknowns,
        boolean matchInapplicables, MatchType matchType) {
    // sort attributes in ascending order according to range length
    Map<Attribute, Double> attrRangeLengths = new HashMap<Attribute, Double>();

    for (RealAttribute attr : attrs) {
        FloatRange range = attr.getPresentRange();
        attrRangeLengths.put(attr, (double) (range.getMaximumFloat() - range.getMinimumFloat()));
    }/*  w  w w  .  j  av a2 s.co  m*/

    Collections.sort(attrs, new AttributeValueSizeComparator(attrRangeLengths));

    if (matchType == MatchType.EXACT || matchType == MatchType.SUBSET) {
        boolean diff = false;

        for (int i = 0; i < attrs.size() - 1; i++) {

            RealAttribute a1 = attrs.get(i);
            RealAttribute a2 = attrs.get(i + 1);
            diff = !compareReal(a1, a2, matchUnknowns, matchInapplicables, matchType);

            if (diff) {
                break;
            }
        }

        return !diff;
    } else {
        // overlap - taxa must have at least one point in common
        // progressively calculate area of overlap
        int numAttrs = attrs.size();

        for (int i = 0; i < numAttrs; i++) {
            RealAttribute a1 = attrs.get(i);
            FloatRange a1Range = a1.getPresentRange();

            for (int j = i + 1; j < numAttrs; j++) {
                boolean overlap = false;

                RealAttribute a2 = attrs.get(j);
                FloatRange a2Range = a2.getPresentRange();

                overlap = a2Range.overlapsRange(a1Range);

                if (!overlap) {
                    return false;
                }
            }
        }
        return true;
    }
}

From source file:au.org.ala.delta.best.Best.java

/**
 * Helper method for orderBest. Takes a real character's value and converts
 * it into a multistate value using the real character's key state
 * boundaries// w ww. j  a va2  s . c  o  m
 * 
 * @param realChar
 *            The real character
 * @param realValue
 *            The value for the real character
 * @param statePresence
 *            This array will be filled by the method. A true value in this
 *            array indicates that the corresponding state value is present
 * @return the number of key states present for the supplied real value
 */
public static int generateKeyStatesForRealCharacter(RealCharacter realChar, FloatRange realValue,
        boolean[] statePresence) {
    int numStatesPresent = 0;

    List<Float> boundariesList = realChar.getKeyStateBoundaries();
    Float[] boundaries = new Float[boundariesList.size()];
    boundariesList.toArray(boundaries);

    float rangeMin = realValue.getMinimumFloat();
    float rangeMax = realValue.getMaximumFloat();

    int i = 0;
    for (; i < boundaries.length; i++) {
        if (rangeMin <= boundaries[i]) {
            statePresence[i] = true;
            numStatesPresent++;
            break;
        }
    }

    for (; i < boundaries.length - 1; i++) {
        if (rangeMax > boundaries[i]) {
            statePresence[i + 1] = true;
            numStatesPresent++;
        }
    }

    return numStatesPresent;
}

From source file:au.org.ala.delta.intkey.model.SortingUtils.java

/**
 * Helper method for orderBest. Takes a real character's value and converts
 * it into a multistate value using the real character's key state
 * boundaries/*w  ww  .  j  a va 2  s  .c om*/
 * 
 * @param realChar
 *            The real character
 * @param realValue
 *            The value for the real character
 * @param statePresence
 *            This array will be filled by the method. A true value in this
 *            array indicates that the corresponding state value is present
 * @return the number of key states present for the supplied real value
 */
private static int generateKeyStatesForRealCharacter(RealCharacter realChar, FloatRange realValue,
        boolean[] statePresence) {
    int numStatesPresent = 0;

    List<Float> boundariesList = realChar.getKeyStateBoundaries();
    Float[] boundaries = new Float[boundariesList.size()];
    boundariesList.toArray(boundaries);

    float rangeMin = realValue.getMinimumFloat();
    float rangeMax = realValue.getMaximumFloat();

    int i = 0;
    for (; i < boundaries.length; i++) {
        if (rangeMin <= boundaries[i]) {
            statePresence[i] = true;
            numStatesPresent++;
            break;
        }
    }

    for (; i < boundaries.length - 1; i++) {
        if (rangeMax > boundaries[i]) {
            statePresence[i + 1] = true;
            numStatesPresent++;
        }
    }

    return numStatesPresent;
}

From source file:au.org.ala.delta.intkey.directives.invocation.OutputDescribeDirectiveInvocation.java

private String getRealAttributeAsString(RealAttribute attr) {
    StringBuilder builder = new StringBuilder();

    FloatRange presentRange = attr.getPresentRange();

    if (presentRange != null) {
        builder.append(String.format("%.2f", presentRange.getMinimumFloat()));

        if (presentRange.getMinimumFloat() != presentRange.getMaximumFloat()) {
            builder.append(String.format("-%.2f", presentRange.getMaximumFloat()));
        }// w  w  w. java2s  .c  om

        if (attr.isInapplicable()) {
            builder.append("/-");
        }

    } else {
        if (attr.isInapplicable()) {
            builder.append("-");
        }
    }
    return builder.toString();
}

From source file:au.org.ala.delta.util.Utils.java

public static String formatFloatRangeAsString(FloatRange range) {
    StringBuilder builder = new StringBuilder();
    float minimumValue = range.getMinimumFloat();
    float maximumValue = range.getMaximumFloat();
    if (minimumValue == maximumValue) {
        if (minimumValue == Math.round(minimumValue)) {
            builder.append((int) minimumValue);
        } else {/*from  w  w w.  j av  a2 s .  com*/
            builder.append(minimumValue);
        }
    } else {
        if (minimumValue == Math.round(minimumValue)) {
            builder.append((int) minimumValue);
        } else {
            builder.append(minimumValue);
        }
        builder.append("-");
        if (maximumValue == Math.round(maximumValue)) {
            builder.append((int) maximumValue);
        } else {
            builder.append(maximumValue);
        }
    }

    return builder.toString();
}

From source file:au.org.ala.delta.intkey.WriteOnceIntkeyItemsFile.java

public void writeAttributeFloats(int charNumber, BitSet inapplicableBits, List<FloatRange> values,
        List<Float> keyStateBoundaries) {
    int record = updateCharacterIndex(charNumber);
    List<Integer> inapplicable = bitSetToInts(inapplicableBits, _header.getNItem());
    record += writeToRecord(record, inapplicable);

    List<Float> floats = new ArrayList<Float>();
    for (FloatRange range : values) {
        // Special cases, Float.MAX_VALUE indicates coded unknown.
        //               -Float.MIN_VALUE indicates uncoded unknown
        if (range.getMinimumFloat() == Float.MAX_VALUE) {
            // These somewhat strange values are for CONFOR compatibility
            floats.add((float) CONFOR_INT_MAX);
            floats.add(-(float) CONFOR_INT_MAX);
        } else if (range.getMaximumFloat() == -Float.MAX_VALUE) {
            floats.add(1f);//from w  w w  . ja v a2 s  .  c om
            floats.add(0f);
        } else {
            floats.add(range.getMinimumFloat());
            floats.add(range.getMaximumFloat());
        }
    }
    writeFloatsToRecord(record, floats);

    int recordNum = nextAvailableRecord();
    writeToRecord(recordNum, keyStateBoundaries.size());
    writeFloatsToRecord(recordNum + 1, keyStateBoundaries);

    if (_keyStateBoundariesIndex == null) {
        _keyStateBoundariesIndex = new int[_header.getNChar()];
        Arrays.fill(_keyStateBoundariesIndex, 0);
    }
    _keyStateBoundariesIndex[charNumber - 1] = recordNum;
    _header.setLSbnd(_header.getLSbnd() + keyStateBoundaries.size());
    _header.setLkstat(Math.max(_header.getLkstat(), keyStateBoundaries.size()));

}

From source file:au.org.ala.delta.intkey.directives.invocation.UseDirectiveInvocation.java

private String formatAttributeValueForLog(Attribute attr) {
    if (attr instanceof TextAttribute) {
        return ((TextAttribute) attr).getText();
    } else if (attr instanceof MultiStateAttribute) {
        return Utils.formatIntegersAsListOfRanges(((MultiStateAttribute) attr).getPresentStatesAsList(), "/",
                "-");
    } else if (attr instanceof IntegerAttribute) {
        Set<Integer> presentValues = ((IntegerAttribute) attr).getPresentValues();
        List<Integer> presentValuesList = new ArrayList<Integer>(presentValues);
        Collections.sort(presentValuesList);
        return Utils.formatIntegersAsListOfRanges(presentValuesList, "/", "-");
    } else if (attr instanceof RealAttribute) {
        FloatRange range = ((RealAttribute) attr).getPresentRange();
        StringBuilder builder = new StringBuilder();
        float min = range.getMinimumFloat();
        float max = range.getMaximumFloat();

        if (min == max) {
            builder.append(min);//from   w ww  .  j av  a  2  s  .  c om
        } else {
            builder.append(min);
            builder.append("-");
            builder.append(max);
        }
        return builder.toString();
    } else {
        throw new IllegalArgumentException("Unrecognised attribute type!");
    }
}

From source file:au.org.ala.delta.intkey.directives.invocation.UseDirectiveInvocation.java

private Attribute promptForCharacterValue(au.org.ala.delta.model.Character ch,
        au.org.ala.delta.model.Character dependentChar, Set<Integer> applicableStates,
        DirectivePopulator populator, Specimen specimen, IntkeyContext context) {
    SimpleAttributeData impl = new SimpleAttributeData(false, false);

    if (ch instanceof MultiStateCharacter) {
        Set<Integer> stateValues;
        if (dependentChar != null) {
            // We are setting a value for a controlling character. Ensure
            // that the states that make the dependent character applicable
            // are pre-selected.
            stateValues = populator.promptForMultiStateValue((MultiStateCharacter) ch, applicableStates,
                    dependentChar);//from  ww w.  ja  v  a  2s  .  c  o m
        } else {
            // Get the current value of the character as set in the
            // specimen. If no
            // value is set for the character, an attribute with
            // unknown = true is returned.
            MultiStateAttribute currentAttribute = (MultiStateAttribute) specimen.getAttributeForCharacter(ch);
            stateValues = populator.promptForMultiStateValue((MultiStateCharacter) ch,
                    currentAttribute.isUnknown() ? null : currentAttribute.getPresentStates(), null);
        }
        if (stateValues != null && stateValues.isEmpty() && dependentChar == null) {
            // User hit OK but did not select any states. Delete any value
            // set
            // for the character.
            if (specimen.hasValueFor(ch)) {
                deleteCharacter(ch, context);
            }
            return null;
        } else if (stateValues != null && stateValues.size() > 0) {
            impl.setPresentStateOrIntegerValues(stateValues);
        } else {
            return null;
        }
    } else if (ch instanceof IntegerCharacter) {
        // Get the current value of the character as set in the specimen. If
        // no
        // value is set for the character, an attribute with
        // unknown = true is returned.
        IntegerAttribute currentAttribute = (IntegerAttribute) specimen.getAttributeForCharacter(ch);

        Set<Integer> intValue = populator.promptForIntegerValue((IntegerCharacter) ch,
                currentAttribute.isUnknown() ? null : currentAttribute.getPresentValues());

        if (intValue != null && intValue.isEmpty()) {
            // User hit OK but did not input any values. Delete any value
            // set
            // for the character
            if (specimen.hasValueFor(ch)) {
                deleteCharacter(ch, context);
            }
            return null;
        } else if (intValue != null && intValue.size() > 0) {
            impl.setPresentStateOrIntegerValues(intValue);
        } else {
            return null;
        }
    } else if (ch instanceof RealCharacter) {
        // Get the current value of the character as set in the specimen. If
        // no
        // value is set for the character, an attribute with
        // unknown = true is returned.
        RealAttribute currentAttribute = (RealAttribute) specimen.getAttributeForCharacter(ch);

        FloatRange floatRange = populator.promptForRealValue((RealCharacter) ch,
                currentAttribute.isUnknown() ? null : currentAttribute.getPresentRange());

        if (floatRange != null) {
            // Float range with minimum and maximum value as negative
            // infinity indicates that the user hit OK without inputting
            // values.
            if (floatRange.getMaximumFloat() == Float.NEGATIVE_INFINITY
                    && floatRange.getMinimumFloat() == Float.NEGATIVE_INFINITY) {
                // User hit OK but did not input any values. Delete any
                // value set
                // for the character
                if (specimen.hasValueFor(ch)) {
                    deleteCharacter(ch, context);
                }
                return null;
            }
            impl.setRealRange(floatRange);
        } else {
            return null;
        }
    } else if (ch instanceof TextCharacter) {
        // Get the current value of the character as set in the specimen. If
        // no
        // value is set for the character, an attribute with
        // unknown = true is returned.
        TextAttribute currentAttribute = (TextAttribute) specimen.getAttributeForCharacter(ch);

        List<String> stringList = populator.promptForTextValue((TextCharacter) ch,
                currentAttribute.isUnknown() ? null
                        : Arrays.asList(currentAttribute.getValueAsString().split("/")));

        if (stringList != null && stringList.isEmpty()) {
            // User hit OK but did not input any values. Delete any
            // value set
            // for the character
            if (specimen.hasValueFor(ch)) {
                deleteCharacter(ch, context);
            }
            return null;
        } else if (stringList != null && stringList.size() > 0) {
            impl.setValueFromString(StringUtils.join(stringList, '/'));
        } else {
            return null;
        }
    } else {
        throw new IllegalArgumentException("Unrecognized character type");
    }

    Attribute attr = AttributeFactory.newAttribute(ch, impl);
    attr.setSpecimenAttribute(true);

    return attr;
}

From source file:au.org.ala.delta.translation.intkey.IntkeyItemsFileWriter.java

private Set<Float> writeRealAttributes(int filteredCharNumber, Character realChar, boolean wasInteger) {
    int unfilteredCharNumber = realChar.getCharacterId();
    boolean useNormalValues = _context.getUseNormalValues(unfilteredCharNumber);

    List<FloatRange> values = new ArrayList<FloatRange>();
    BitSet inapplicableBits = new BitSet();

    Iterator<FilteredItem> items = _dataSet.filteredItems();
    while (items.hasNext()) {
        FilteredItem item = items.next();
        NumericAttribute attribute = (NumericAttribute) _dataSet.getAttribute(item.getItem().getItemNumber(),
                unfilteredCharNumber);//from ww w .  j a  va  2  s. c o m

        if (attribute == null || attribute.isCodedUnknown() || attribute.isInapplicable()
                || attribute.isVariable()) {
            FloatRange range = new FloatRange(Float.MAX_VALUE);
            values.add(range);
            if (isInapplicable(attribute)) {
                inapplicableBits.set(item.getItemNumber() - 1);
            }
            continue;
        }
        List<NumericRange> ranges = attribute.getNumericValue();
        // This can happen if the attribute has a comment but no value.
        if (ranges.isEmpty()) {
            FloatRange range = new FloatRange(-Float.MAX_VALUE);
            values.add(range);
            if (isInapplicable(attribute)) {
                inapplicableBits.set(item.getItemNumber() - 1);
            }
            continue;
        }
        Range useRange;
        float min = Float.MAX_VALUE;
        float max = -Float.MAX_VALUE;
        for (NumericRange range : ranges) {
            if (_context.hasAbsoluteError(unfilteredCharNumber)) {
                range.setAbsoluteError(_context.getAbsoluteError(unfilteredCharNumber));
            } else if (_context.hasPercentageError(unfilteredCharNumber)) {
                range.setPercentageError(_context.getPercentageError(unfilteredCharNumber));
            }
            if (useNormalValues) {
                useRange = range.getNormalRange();
            } else {
                useRange = range.getFullRange();
            }
            min = Math.min(min, useRange.getMinimumFloat());
            max = Math.max(max, useRange.getMaximumFloat());

        }

        FloatRange floatRange = new FloatRange(min, max);
        values.add(floatRange);
    }

    Set<Float> floats = new HashSet<Float>();
    for (FloatRange range : values) {
        if (range.getMinimumFloat() != Float.MAX_VALUE && range.getMinimumFloat() != -Float.MAX_VALUE) {
            floats.add(range.getMinimumFloat());
        } else {
            if (range.getMinimumFloat() == -Float.MAX_VALUE && !wasInteger) {
                floats.add(0f); // For CONFOR compatibility, seems wrong.
            }
        }
        if (range.getMaximumFloat() != Float.MAX_VALUE && range.getMinimumFloat() != -Float.MAX_VALUE) {
            floats.add(range.getMaximumFloat());
        } else {
            if (range.getMinimumFloat() == -Float.MAX_VALUE && !wasInteger) {
                floats.add(1.0f); // For CONFOR compatibility, seems wrong.
            }
        }
    }
    List<Float> boundaries = new ArrayList<Float>(floats);
    Collections.sort(boundaries);
    _itemsFile.writeAttributeFloats(filteredCharNumber, inapplicableBits, values, boundaries);

    return floats;
}