Why does DataForm.getFields() return an Iterator instead of a List? Or better yet, I think it should return a Map indexed by the field variable names, since those are unique. That would make it a lot easier to get a form field value by name rather than having to use the iterator to search.
In general Smack returns Iterators instead of the actual Collections. I think that the reasons for this are 1) hide the type of collection internally used and 2) make clear that the answer is something to iterate over and not to change (even though you can try to remove an element using an Iterator).
I’'m interested in the reason why you need to directly use a DataForm and not a Form. DataForms are intended to be private classes or classes from a lower layer. Instead, the idea of the class Form is to provide a higher level of abstraction. If you can use the class Form, you will see that the Form class already provides a method that returns the field whose variable matches the one you are looking for.
- That’'s what the List and Map interfaces are for. 2) You can use Collections.unmodifiableList/Map to enforce this (in fact you already do, but then return the iterator instead of the collection itself).
Umm, to be honest, the only reason I don’‘t use Form is that I didn’‘t notice it! I only knew that I had to extract a PacketExtension from the DiscoverInfo packet, and DataForm implements PacketExtension, so I used that. I see now that I should instead use Form.getFormFrom on the DiscoverInfo packet. (Correct me if I’'m wrong…)
Yep. You should use Form.getFormFrom(Packet) to get the form imbibed in a packet. I forgot to add to my other post regarding JEP-128 that you should also use Form.getFormFrom(Packet) to get the form included in the disco response.
OK, thanks. It might be convenient to also have a getForm() method on DiscoverInfo, but that’'s a matter of taste.
By the way, my other point about returning a collection instead of an iterator is backed up by this from the Java Collections design FAQ:
We don’‘t believe in using Enumerations (or Iterators) as "poor man’'s collections." This was occasionally done in prior releases, but now that we have the Collection interface, it is the preferred way to pass around abstract collections of objects.