commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dimitri Papaioannou" <dimitri.papaioan...@gmail.com>
Subject [jexl] PROPOSAL: Extend Jexl to Support comparison operations between used-defined types
Date Sat, 17 Mar 2007 15:45:05 GMT
Please let me know if you aagree with the following proposal:

I would like to extend Jexl to support comparison operations between
used-defined types.
This will enable Jexl to do comparison operations between user-define data
types that are not a-priori comparable. For example, you will be able to
compare dates to strings representing dates, numbers to strings formatted as
percentages et cetera.



This will be accomplished by implementing custom type converters:



public interface TypeConverter {



            public Object convert(JexlContext context, Object object) throws
TypeConversionException ;

}



And registering them with the context:



JexlContext context = JexlHelper.createContext();

context.registerConverter(Date.class, converter);



These are some sample usages in unit-test form:



            public void testDateComparison() throws Exception {

                        SimpleDateTypeConverter converter = new
SimpleDateTypeConverter();

                        context.registerConverter(Date.class, converter);

                        Calendar c1 = new GregorianCalendar(2006,
Calendar.MARCH, 4);

                        context.getVars().put("date", c1.getTime());

                        assertTrue("date < '2007-01-01'");

                        assertTrue("'2006-01-01' < date");



                        converter.registerFormat("MM/dd/yy");

                        assertTrue("date == '3/4/06'");


            }



            public void testUserDefinedType() throws Exception {

                        context.registerConverter(Temperature.class, new
TypeConverter() {



                                    public Object convert(JexlContext
context, Object object) throws TypeConversionException {

                                                if (object == null) {

                                                            return null;

                                                }

                                                if (object instanceof
Temperature) {

                                                            return
(Temperature) object;

                                                }

                                                String str = object.toString
();

                                                return Temperature.valueOf
(str);

                                    }



                        });



                        context.getVars().put("temp", Temperature.COLD);

                        assertTrue("temp == 'COLD'");

                        assertTrue("temp < 'HOT'");

                        assertTrue("'MEDIUM' > temp");

            }


    public void testExtendTypeConverter() throws Exception {
        context.registerConverter(Float.class, new PercentConverter());
        context.registerConverter(Double.class, new PercentConverter());
        context.registerConverter(BigDecimal.class, new PercentConverter());
        assertTrue("0.14 == '14%'");
        context.getVars().put("a", new BigDecimal(0.65));
        assertTrue("a < '70%'");
    }

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message