You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apex.apache.org by Bhupesh Chawda <bh...@datatorrent.com> on 2015/11/18 14:02:19 UTC

Expression for multiple fields extraction from a POJO

Hi All,

Is there a way I can extract multiple "String" fields from a POJO using
only a single getter?
In other words, can we define a single expression which can get 2 String
fields concatenated together from a POJO ?

Example:
class POJO {
 String a;
 String b;
}

Result expected: "ab"

Also, is there a performance penalty if we use multiple getters for
multiple fields, as compared to a single getter with a complex expression ?

Thanks.
-Bhupesh

Re: Expression for multiple fields extraction from a POJO

Posted by Bhupesh Chawda <bh...@datatorrent.com>.
Hi All,

Have another question on same topic (fields extraction from POJO). This is
more from a usability and convention perspective.

Is it okay to accept an "expression" from the user instead of just the
field names?
Meaning, can I ask for "{$}.getName() + {$}.getAddress()" as a key
expression from the user, instead of just "name,address"?

Also, is there an easy way to construct getter expressions from just the
field names? There might be issues if the getters in the POJO do not follow
java conventions. In that case, a "name => getName()" conversion might not
be applicable.

Comments?

Thanks.
Bhupesh

On Wed, Nov 18, 2015 at 7:45 PM, Bhupesh Chawda <bh...@datatorrent.com>
wrote:

> Thanks Tushar, that helps. Did not know about the object place holders.
>
> -Bhupesh
>
> On Wed, Nov 18, 2015 at 7:38 PM, Tushar Gosavi <tu...@datatorrent.com>
> wrote:
>
>> Hi Bhupesh,
>>
>> You can use PojoUtils to creates a getter with expression to concat two
>> strings, for example
>>
>> public  class TestC {
>>   public String a;
>>   public String b;
>>
>>   public TestC(String a, String b)
>>   {
>>     this.a = a;
>>     this.b = b;
>>   }
>> }
>>
>>
>> public void test1() {
>>   Getter sumg = PojoUtils.createGetter(TestC.class, "{$}.a + {$}.b",
>> String.class);
>>   TestC o = new TestP("a", "b");
>>   String s = (String)sumg.get(o);
>>   System.out.println(s);
>> }
>>
>> The expression here is "{$}.a + {$}.b" where {$} is object placeholder.
>>
>> PojoUtils compiles the expression internally so it will be faster than
>> manually getting individual fields using reflection and
>> computing required value.
>>
>> Regards,
>> -Tushar.
>>
>>
>>
>>
>> On Wed, Nov 18, 2015 at 6:32 PM, Bhupesh Chawda <bh...@datatorrent.com>
>> wrote:
>>
>> > Hi All,
>> >
>> > Is there a way I can extract multiple "String" fields from a POJO using
>> > only a single getter?
>> > In other words, can we define a single expression which can get 2 String
>> > fields concatenated together from a POJO ?
>> >
>> > Example:
>> > class POJO {
>> >  String a;
>> >  String b;
>> > }
>> >
>> > Result expected: "ab"
>> >
>> > Also, is there a performance penalty if we use multiple getters for
>> > multiple fields, as compared to a single getter with a complex
>> expression ?
>> >
>> > Thanks.
>> > -Bhupesh
>> >
>>
>
>

Re: Expression for multiple fields extraction from a POJO

Posted by Bhupesh Chawda <bh...@datatorrent.com>.
Thanks Tushar, that helps. Did not know about the object place holders.

-Bhupesh

On Wed, Nov 18, 2015 at 7:38 PM, Tushar Gosavi <tu...@datatorrent.com>
wrote:

> Hi Bhupesh,
>
> You can use PojoUtils to creates a getter with expression to concat two
> strings, for example
>
> public  class TestC {
>   public String a;
>   public String b;
>
>   public TestC(String a, String b)
>   {
>     this.a = a;
>     this.b = b;
>   }
> }
>
>
> public void test1() {
>   Getter sumg = PojoUtils.createGetter(TestC.class, "{$}.a + {$}.b",
> String.class);
>   TestC o = new TestP("a", "b");
>   String s = (String)sumg.get(o);
>   System.out.println(s);
> }
>
> The expression here is "{$}.a + {$}.b" where {$} is object placeholder.
>
> PojoUtils compiles the expression internally so it will be faster than
> manually getting individual fields using reflection and
> computing required value.
>
> Regards,
> -Tushar.
>
>
>
>
> On Wed, Nov 18, 2015 at 6:32 PM, Bhupesh Chawda <bh...@datatorrent.com>
> wrote:
>
> > Hi All,
> >
> > Is there a way I can extract multiple "String" fields from a POJO using
> > only a single getter?
> > In other words, can we define a single expression which can get 2 String
> > fields concatenated together from a POJO ?
> >
> > Example:
> > class POJO {
> >  String a;
> >  String b;
> > }
> >
> > Result expected: "ab"
> >
> > Also, is there a performance penalty if we use multiple getters for
> > multiple fields, as compared to a single getter with a complex
> expression ?
> >
> > Thanks.
> > -Bhupesh
> >
>

Re: Expression for multiple fields extraction from a POJO

Posted by Tushar Gosavi <tu...@datatorrent.com>.
Hi Bhupesh,

You can use PojoUtils to creates a getter with expression to concat two
strings, for example

public  class TestC {
  public String a;
  public String b;

  public TestC(String a, String b)
  {
    this.a = a;
    this.b = b;
  }
}


public void test1() {
  Getter sumg = PojoUtils.createGetter(TestC.class, "{$}.a + {$}.b",
String.class);
  TestC o = new TestP("a", "b");
  String s = (String)sumg.get(o);
  System.out.println(s);
}

The expression here is "{$}.a + {$}.b" where {$} is object placeholder.

PojoUtils compiles the expression internally so it will be faster than
manually getting individual fields using reflection and
computing required value.

Regards,
-Tushar.




On Wed, Nov 18, 2015 at 6:32 PM, Bhupesh Chawda <bh...@datatorrent.com>
wrote:

> Hi All,
>
> Is there a way I can extract multiple "String" fields from a POJO using
> only a single getter?
> In other words, can we define a single expression which can get 2 String
> fields concatenated together from a POJO ?
>
> Example:
> class POJO {
>  String a;
>  String b;
> }
>
> Result expected: "ab"
>
> Also, is there a performance penalty if we use multiple getters for
> multiple fields, as compared to a single getter with a complex expression ?
>
> Thanks.
> -Bhupesh
>