commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dmitri Blinov (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (JEXL-174) Overloadable property access operators
Date Sat, 29 Aug 2015 08:02:45 GMT

    [ https://issues.apache.org/jira/browse/JEXL-174?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14721005#comment-14721005
] 

Dmitri Blinov commented on JEXL-174:
------------------------------------

Wow, here comes magic, now it's possible to work with j.u.Date as simply as 

{{date.yyyy}} or may be {{date\["MM/yy/dd"\]}} or may be  {{date.yyyy = 2016}} or may be even
{{date.dd += 3}}

{code}
    protected Object getDateValue(Date date, String key) {

      try {
       
        if (key.equals("Time"))
          return date.getTime();

        Calendar cal = Calendar.getInstance(); 
        cal.setTime(date);

        if ("yyyy".equals(key)) {
          return cal.get(Calendar.YEAR);
        } else if ("MM".equals(key)) {
          return cal.get(Calendar.MONTH) + 1;
        } else if ("dd".equals(key)) {
          return cal.get(Calendar.DAY_OF_MONTH);
        } else if ("HH".equals(key)) {
          return cal.get(Calendar.HOUR_OF_DAY);
        } else if ("mm".equals(key)) {
          return cal.get(Calendar.MINUTE);
        } else if ("ss".equals(key)) {
          return cal.get(Calendar.SECOND);
        } else if ("SSS".equals(key)) {
          return cal.get(Calendar.MILLISECOND);
        } else if ("E".equals(key)) {
          return cal.get(Calendar.DAY_OF_WEEK);
        } else if ("G".equals(key)) {
          return cal.get(Calendar.ERA);
        } else if ("z".equals(key)) {
          return cal.getTimeZone().getID();
        }

        // Otherwise treat as format mask

        DateFormatSymbols dfs = new DateFormatSymbols(Locale.getDefault());
        SimpleDateFormat df = new SimpleDateFormat(key, dfs);

        return (df == null) ? null : df.format(date);

      } catch(Exception ex) {
        return null;
      }
    }

    protected Object setDateValue(Date date, String key, Object value) throws Exception {

      Calendar cal = Calendar.getInstance(); 

      cal.setTime(date);
      cal.setLenient(true);

      if ("yyyy".equals(key)) {
        cal.set(Calendar.YEAR, TypeCast.getInt(value));
      } else if ("MM".equals(key)) {
        cal.set(Calendar.MONTH, TypeCast.getInt(value) - 1);
      } else if ("dd".equals(key)) {
        cal.set(Calendar.DAY_OF_MONTH, TypeCast.getInt(value));
      } else if ("HH".equals(key)) {
        cal.set(Calendar.HOUR_OF_DAY, TypeCast.getInt(value));
      } else if ("mm".equals(key)) {
        cal.set(Calendar.MINUTE, TypeCast.getInt(value));
      } else if ("ss".equals(key)) {
        cal.set(Calendar.SECOND, TypeCast.getInt(value));
      } else if ("SSS".equals(key)) {
        cal.set(Calendar.MILLISECOND, TypeCast.getInt(value));
      } else if ("G".equals(key)) {
        cal.set(Calendar.ERA, TypeCast.getInt(value));
      } else if ("z".equals(key)) {
        cal.setTimeZone(TimeZone.getTimeZone(String.valueOf(value)));
      }

      date.setTime(cal.getTimeInMillis());

      return date;
    }

    public Object propertyGet(Date date, Object identifier) {
       return getDateValue(date, String.valueOf(identifier));
    }

    public Object propertySet(Date date, Object identifier, Object value) throws Exception
{
       return setDateValue(date, String.valueOf(identifier), value);
    }

    public Object arrayGet(Date date, Object identifier) {
       return getDateValue(date, String.valueOf(identifier));
    }
    
    public Object arraySet(Date date, Object identifier, Object value) throws Exception {
       return setDateValue(date, String.valueOf(identifier), value);
    }
{code}

Note: The following example does not contain TypeCast.getInt() method

> Overloadable property access operators
> --------------------------------------
>
>                 Key: JEXL-174
>                 URL: https://issues.apache.org/jira/browse/JEXL-174
>             Project: Commons JEXL
>          Issue Type: New Feature
>    Affects Versions: 3.0
>            Reporter: Dmitri Blinov
>            Assignee: Henri Biestro
>            Priority: Minor
>             Fix For: 3.0
>
>
> In analogy with overloading operators like empty(), size() etc, provide a way to overload
property "get" and "set" operators ([] and .), like 
> {code}
>    public Object getAt(Object obj, Object index) {...}
>    public Object putAt(Object obj, Object index, Object value) {...}
> {code}
> Overloaded operators should be tried before any standard access stategy, ie MAP, POJO
etc.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message