Hi

Please keep in mind that "Object#equals" method is extremely important when it comes to Collections and Maps. "Object#equals" and "Object#hashcode" are both elementary, when searching a list or a map for a specific Paragraph.

If you override these methods with a "high level" definition, for example "paragraphs are same, if they contain the same text, regardless the format", then it becomes an extremely awful task to sort a bunch of paragraphs into a List with "contains" check, if the document contains three paragraphs that accidentally evaluate as equal according to your specific requirement, but actually are three different paragraphs on three different places in the document. Calling "List#indexOf" for one of those paragraphs would always return the index of the first paragraph, although the third instance should have been returned.

If you need a method, that compares two paragraphs, then you should define very precisely, what makes those two paragraphs "equal". Here some examples:
- They contain the same text.
- They contain the same text, associated with the same style-IDs.
- They contain the same text, associated with the same style-IDs, where the styles are equal, too. ( Question --> When are two styles equal?) - They contain the same subelements (runs, tables, bookmarks, etc.) and belong to the same package.

If coming to pictures or other media / embedded data (like an excel table object), the scenarion becomes a lot more complex.

Conclusion:
If you need a method, that evaluates two paragraphs as equal, then either implement it as a method on its own, leaving "Object#equals" alone. Or, if your "equals" method is interested only in a subset of all criterias available, explicitly ignoring other criterias (like "belong to the same package"), I propose to implement this method in your software. It is not very useful to have 12 different "equals"-alike methods, that ignore or evaluate different sets of criterias in the XWPFParagraph class. Another approach is to create Adapters or Wrapper-classes, that hide the XWPFParagraph and allow to work directly with all Collections and Maps.

Hope that helped,
Stefan Stern


Am 20.05.2011 13:05, schrieb Pavan Apache:
Hi,

I have a requirement to create a Document by replacing the bookmarks with
text. I am trying to acheive this by creating a new document and i am trying
to keep the same paragraphs in new document like this.

docDoc = new XWPFDocument(new FileInputStream(f));
             result = new XWPFDocument(new FileInputStream(n));
             List<XWPFParagraph>  paragraphs = docDoc.getParagraphs();
             Map bookmarkValues = new HashMap();
             List<XWPFParagraph>  targetParaGraph = new
ArrayList<XWPFParagraph>();
             for (int i = 0; i<  paragraphs.size(); i++) {
                 try {

                     XWPFParagraph tempPara = result.createParagraph();
                     int paraIndex  = result.getPosOfParagraph(tempPara);
                     result.setParagraph(paragraphs.get(i), paraIndex);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }


Then, i found that XWPFParagraph doesnt implement equals method.

Can we have any equals method or clone method to acheieve this. If i can
take this task then i will.

And i am doing it in a wrong way, please correct me.

And guide me if there is any alternate way.

I kept this post in Developer list because i want to contribute to both code
and documentation to poi DOC family.


--
Mind8 GmbH&  Co. KG · Gropiusplatz 10 · D-70563 Stuttgart
Phone: +49 711-89536-207 · Fax: +49 711-89536-199
mailto:[email protected]
www.mind8.com - Friedhelm Loh Group: www.friedhelm-loh-group.com

Sitz: Stuttgart, Amtsgericht Stuttgart, HRA 724150 - Persönlich haftende 
Gesellschafterin: Mind8 Verwaltungs-GmbH,
Sitz: Haiger, Amtsgericht Wetzlar, HRB 5847 - Geschäftsführung: Maximilian 
Brandl (Vorsitzender), Dr.-Ing. Jörg Lewek, Dr.-Ing. Marco Litto

Diese E-Mail ist vertraulich. Eine Weitergabe der darin verkörperten 
Informationen ist ohne Zustimmung des Versenders unzulässig.***
This email contains confidential information. You are not authorised to copy 
the contents without the consent of the sender.***



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to