Multiple streams defined in Class using annotations

588 views
Skip to first unread message

typwr...@gmail.com

unread,
Aug 31, 2014, 10:38:10 AM8/31/14
to bea...@googlegroups.com
From what I understand it is possible to use annotations in a java clas to define the mapping, but for only one stream.

Is it possible to combine the different formats in a single class? Does BeanIO skip portions that are not relevant for a specific format (e.g. XML namespace when exporting to csv)?

To be able to set multiple options per stream a Streambuilder would be needed, just as the XML mapping files.

Example:
@StreamBuilder(name="contactQuotedCSV", format="csv")
@Parser(delimiter= "\t", alwaysquote="true")
@Record(minOccurs=0, maxOccurs=-1)

@StreamBuilder(name="contactNotQuotedCSV", format="csv")
@Parser(delimiter= "\t", alwaysquote="false")
@Record(minOccurs=0, maxOccurs=-1)

@StreamBuilder(name="contactXML", format="xml")
@Record(minOccurs=0, maxOccurs=-1)

public class Contact {
@StreamBuilderName(name="contactQuotedCSV")
@Field(at=1, maxLength=20)

@StreamBuilderName(name="contactNotQuotedCSV")
@Field(at=1, maxLength=20)

@StreamBuilderName(name="contactXML")
@Field(at=1, maxLength=20)

 
String firstName;
 
 
@Field(at=2, required=true, maxLength=30)
 
String lastName;
}

Using the same constructions as Fields({Field@(at=1,....)}) could also be an option.

What are your thoughts on this? I would appreciate such a solution a lot, but I have no idea how time consuming it will be..

Kevin

unread,
Sep 5, 2014, 10:31:26 PM9/5/14
to bea...@googlegroups.com
Yes, BeanIO will ignore configuration settings not relevant to the format, but this is often not flexible enough.

Unfortunately, Java does not allow multiple annotations of the same type on a single class/method declaration.  And in my opinion creating sets of annotations would get rather cumbersome.  So for now, I recommend using a mapping file instead of annotations if you need to use the same beans in multiple formats.

Thanks,
Kevin

typwr...@gmail.com

unread,
Sep 6, 2014, 4:42:48 PM9/6/14
to bea...@googlegroups.com
Hi Kevin,

Thanks for the reply.

I agree that it could become cumbersome. I still think that this is counterbalanced by the inline validation, that is not possible using an XML mapping file. Any typos or errors in the XML will only show when using that particular portion of the stream. Quite a few frameworks use annotations. Taking JPA as an example, I define the @Column of JPA and the @Field of BeanIO directly above each other, so I can immediately see if there are differences in length for example. When combining this with multiple streams, I have everything together. Any changes are done together.

Based on your remarks I did some research on using multiple annotations. The option was introduced in Java 8:
http://docs.oracle.com/javase/tutorial/java/annotations/repeating.html

There seems to be another option, which is discussed here:
http://stackoverflow.com/questions/1554112/multiple-annotations-of-the-same-type-on-one-element
Copy-paste of the relevant section:
public @interface Foos {
   
Foo[] value();
}

@Foos({@Foo(bar="one"), @Foo(bar="two")})
public void haha() {}

Can you tell if this would work for BeanIO? It seems to match the code in Fields.class and Field.class.
I will see how far I can get and post back if something interesting comes up. I have no idea how to implement the rest, but it will be somewhere on Google :)

Thanks for the framework and the time you invest in it, I learn a lot this way.

Kevin

unread,
Sep 7, 2014, 10:27:31 AM9/7/14
to bea...@googlegroups.com
I did not know Java 8 allows repeated annotations- thats cool.  When I change the dependency to Java 8, I will consider it then.

The other approach/workaround, I am not a fan of.  Its already being used for @Fields, but I don't plan to extend it to support multiple stream definitions.

Thanks,
Kevin
Reply all
Reply to author
Forward
0 new messages