Search in sources :

Example 1 with Field

use of org.apache.sis.measure.AngleFormat.Field in project sis by apache.

the class FormattedCharacterIteratorTest method testAttributes.

/**
 * Tests an iteration with attributes, optionally having {@code INTEGER} attributes
 * overlapping the {@code DEGREES}/{@code MINUTES}/{@code SECONDS} ones. The given
 * iterator shall iterates over the {@code "45°30′15.0″N"} characters.
 *
 * <p>This test is leveraged by {@link AngleFormatTest#testFormatToCharacterIterator()}.</p>
 */
@SuppressWarnings("fallthrough")
static void testAttributes(final AttributedCharacterIterator it, final boolean withNumberFields) {
    assertEquals(getAllAttributeKeys(withNumberFields), it.getAllAttributeKeys());
    assertEquals(0, it.getIndex());
    assertEquals(3, it.getRunLimit(MINUTES));
    assertEquals(6, it.getRunLimit(SECONDS));
    for (char c = it.first(); c != DONE; c = it.next()) {
        final int index = it.getIndex();
        final AngleFormat.Field key;
        final Comparable<?> value;
        final int start, limit;
        if (index < 3) {
            key = DEGREES;
            value = 45;
            start = 0;
            limit = 3;
        } else if (index < 6) {
            key = MINUTES;
            value = 30;
            start = 3;
            limit = 6;
        } else if (index < 11) {
            key = SECONDS;
            value = 15f;
            start = 6;
            limit = 11;
        } else {
            key = HEMISPHERE;
            value = 'N';
            start = 11;
            limit = 12;
        }
        /*
             * Expected values when asking for a NumberFormat field.
             * Initialized to the values when no such field exists,
             * then updated if overlapping fields are allowed.
             */
        boolean isInteger = false;
        boolean isSeparator = false;
        boolean isFraction = false;
        int startInteger = 0;
        int limitInteger = 12;
        int startSeparator = 0;
        int limitSeparator = 12;
        int startFraction = 0;
        int limitFraction = 12;
        int numAttributes = 1;
        if (withNumberFields) {
            /*
                 * Update the above expected values when the current position
                 * is inside a NumberFormat field.
                 */
            switch(index) {
                // Degrees
                case 0:
                // Degrees
                case 1:
                // Minutes
                case 3:
                // Minutes
                case 4:
                case 6:
                case 7:
                    isInteger = true;
                    numAttributes = 2;
                    startInteger = start;
                    limitInteger = 2 + start;
                    break;
                case 8:
                    isSeparator = true;
                    numAttributes = 2;
                    startSeparator = 8;
                    limitSeparator = 9;
                    break;
                case 9:
                    isFraction = true;
                    numAttributes = 2;
                    startFraction = 9;
                    limitFraction = 10;
                    break;
            }
            /*
                 * Update the expected values for fields which are not at the current position.
                 * A search for a field should give the position where the next field start, or
                 * where the previous field ended.
                 */
            if (!isInteger) {
                if (index < 7) {
                    // End of previous integer field.
                    startInteger = index;
                    // Start of next integer field.
                    limitInteger = index + 1;
                } else {
                    // End of last integer field.
                    startInteger = 8;
                }
            }
            if (!isSeparator) {
                if (// Start of next separator field.
                index < 8)
                    // Start of next separator field.
                    limitSeparator = 8;
                else
                    // End of previous separator field.
                    startSeparator = 9;
            }
            if (!isFraction) {
                if (// Start of next fraction field.
                index < 9)
                    // Start of next fraction field.
                    limitFraction = 9;
                else
                    // End of previous fraction field.
                    startFraction = 10;
            }
        }
        final Map<Attribute, Object> attributes = it.getAttributes();
        assertEquals("attributes.size", numAttributes, attributes.size());
        assertEquals("attributes.get", value, attributes.get(key));
        assertEquals("attributes.get", isInteger, attributes.get(INTEGER) != null);
        assertEquals("attributes.get", isFraction, attributes.get(FRACTION) != null);
        assertEquals("attributes.get", isSeparator, attributes.get(DECIMAL_SEPARATOR) != null);
        assertEquals("getRunStart", start, it.getRunStart(key));
        assertEquals("getRunLimit", limit, it.getRunLimit(key));
        assertEquals("getRunStart", startInteger, it.getRunStart(INTEGER));
        assertEquals("getRunLimit", limitInteger, it.getRunLimit(INTEGER));
        assertEquals("getRunStart", startFraction, it.getRunStart(FRACTION));
        assertEquals("getRunLimit", limitFraction, it.getRunLimit(FRACTION));
        assertEquals("getRunStart", startSeparator, it.getRunStart(DECIMAL_SEPARATOR));
        assertEquals("getRunLimit", limitSeparator, it.getRunLimit(DECIMAL_SEPARATOR));
        assertEquals("getRunStart", max(max(max(startInteger, startSeparator), startFraction), start), it.getRunStart());
        assertEquals("getRunLimit", min(min(min(limitInteger, limitSeparator), limitFraction), limit), it.getRunLimit());
    }
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) Field(org.apache.sis.measure.AngleFormat.Field)

Aggregations

Attribute (java.text.AttributedCharacterIterator.Attribute)1 Field (org.apache.sis.measure.AngleFormat.Field)1