Search in sources :

Example 1 with ReadablePartial

use of org.joda.time.ReadablePartial in project joda-time by JodaOrg.

the class TestConverterManager method testGetPartialConverterBadMultipleMatches.

public void testGetPartialConverterBadMultipleMatches() {
    PartialConverter c = new PartialConverter() {

        public int[] getPartialValues(ReadablePartial partial, Object object, Chronology chrono) {
            return null;
        }

        public int[] getPartialValues(ReadablePartial partial, Object object, Chronology chrono, DateTimeFormatter parser) {
            return null;
        }

        public Chronology getChronology(Object object, DateTimeZone zone) {
            return null;
        }

        public Chronology getChronology(Object object, Chronology chrono) {
            return null;
        }

        public Class getSupportedType() {
            return Serializable.class;
        }
    };
    try {
        ConverterManager.getInstance().addPartialConverter(c);
        try {
            ConverterManager.getInstance().getPartialConverter(new DateTime());
            fail();
        } catch (IllegalStateException ex) {
        // Serializable and ReadablePartial both match, so cannot pick
        }
    } finally {
        ConverterManager.getInstance().removePartialConverter(c);
    }
    assertEquals(PARTIAL_SIZE, ConverterManager.getInstance().getPartialConverters().length);
}
Also used : Serializable(java.io.Serializable) ReadablePartial(org.joda.time.ReadablePartial) Chronology(org.joda.time.Chronology) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime)

Example 2 with ReadablePartial

use of org.joda.time.ReadablePartial in project joda-time by JodaOrg.

the class TestConverterManager method testGetPartialConverterOKMultipleMatches.

public void testGetPartialConverterOKMultipleMatches() {
    PartialConverter c = new PartialConverter() {

        public int[] getPartialValues(ReadablePartial partial, Object object, Chronology chrono) {
            return null;
        }

        public int[] getPartialValues(ReadablePartial partial, Object object, Chronology chrono, DateTimeFormatter parser) {
            return null;
        }

        public Chronology getChronology(Object object, DateTimeZone zone) {
            return null;
        }

        public Chronology getChronology(Object object, Chronology chrono) {
            return null;
        }

        public Class getSupportedType() {
            return ReadableDateTime.class;
        }
    };
    try {
        ConverterManager.getInstance().addPartialConverter(c);
        PartialConverter ok = ConverterManager.getInstance().getPartialConverter(new DateTime());
        // ReadableDateTime and ReadablePartial both match, but RI discarded as less specific
        assertEquals(ReadableDateTime.class, ok.getSupportedType());
    } finally {
        ConverterManager.getInstance().removePartialConverter(c);
    }
    assertEquals(PARTIAL_SIZE, ConverterManager.getInstance().getPartialConverters().length);
}
Also used : ReadablePartial(org.joda.time.ReadablePartial) ReadableDateTime(org.joda.time.ReadableDateTime) Chronology(org.joda.time.Chronology) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime)

Example 3 with ReadablePartial

use of org.joda.time.ReadablePartial in project joda-time by JodaOrg.

the class ReadablePartialConverter method getPartialValues.

/**
     * Extracts the values of the partial from an object of this converter's type.
     * The chrono parameter is a hint to the converter, should it require a
     * chronology to aid in conversion.
     * 
     * @param fieldSource  a partial that provides access to the fields.
     *  This partial may be incomplete and only getFieldType(int) should be used
     * @param object  the object to convert
     * @param chrono  the chronology to use, which is the non-null result of getChronology()
     * @return the array of field values that match the fieldSource, must be non-null valid
     * @throws ClassCastException if the object is invalid
     */
public int[] getPartialValues(ReadablePartial fieldSource, Object object, Chronology chrono) {
    ReadablePartial input = (ReadablePartial) object;
    int size = fieldSource.size();
    int[] values = new int[size];
    for (int i = 0; i < size; i++) {
        values[i] = input.get(fieldSource.getFieldType(i));
    }
    chrono.validate(fieldSource, values);
    return values;
}
Also used : ReadablePartial(org.joda.time.ReadablePartial)

Aggregations

ReadablePartial (org.joda.time.ReadablePartial)3 Chronology (org.joda.time.Chronology)2 DateTime (org.joda.time.DateTime)2 DateTimeZone (org.joda.time.DateTimeZone)2 ReadableDateTime (org.joda.time.ReadableDateTime)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 Serializable (java.io.Serializable)1