03-03-2015 5:08 PM
In the comments of the conversation a few of us were discussing the use of an iterators and iterator classes. Below I will add what has already been presented:
I have read about it but I had never used. What I usually do is just encapsulating an internal table inside a class and not modelling table rows as objects (I hope this was clear). In this case I'm not sure if the iterator class can be used and useful.
I have used iterators for particular complex data structures. Underneath was an internal table of references, and I used indexes to keep track. I could of course have looped through the internal table at the higher level instead of using an iterator, but due to the data complexity, I really didn't want to expose the internal table at that level.
I do in complex application (in my latest one I don't use loops except to deal with BAPIRET), because it's the way you code in C# and Java, and that way I can easily translate the code to C#. Basically for large modules I program as if I was coding in C#.
If you use Collections and HashMaps it's the only way to "loop", although I do miss the typed List<T> and HashMap<T,X>.
PS: Actually it's not the only way, there's the get_values_table or something .
I use CL_OBJECT_MAP and CL_OBJECT_COLLECTION which are the equivalent to HashMap and List the problem is that these are not typed lists and so I have to rely on casts that the compiler doesn't check for correctness.
I usually write the type in the description of the return parameter of the method.
- I used Iterator classes heavily in java and C#, not in abap yet. I want to use them again. I relied heavily on array list, hash maps, lists, etc when programming in other languages.
- Restrictions...hmm, in java i can't critique, they are awesome. In abap, the primary hurdle would probably be generics. Abap does generics differently than java but I assume adding a constructor to initially set the type of object of the list would solve that. Adding a check to make sure objects of that type can only be added would be next. Iterator classes can be implemented from anywhere an object can be implement. If I run into any other hurdles i'll let you know.
- Too many, If I ever needed a place to hold multiple objects of the same type I'd use an iterator class. They provide a wide range of functionality for accessing a list of objects, and follows the object oriented design model. From what I was taught at University and experienced in the field, data structures such as link lists are essential.
What is everyone's opinion on iterator classes and the comments above?
Here are some useful links to similar postings:
03-05-2015 1:40 AM
03-03-2015 11:58 PM
Hello Jeffrey,
I routinely implement iterators for internal tables using local classes like
CLASS lcl_iterator DEFINITION FRIENDS lif_unit_test.
PUBLIC SECTION.
METHODS constructor IMPORTING it_messages TYPE tt_trace
iv_start TYPE sytabix DEFAULT 1
iv_stop TYPE sytabix OPTIONAL.
METHODS next RETURNING VALUE(rs_data) TYPE ts_message
RAISING cx_sy_itab_error.
METHODS has_next RETURNING VALUE(rv_flag) TYPE xsdboolean.
METHODS skip IMPORTING iv_count TYPE i DEFAULT 1.
PROTECTED SECTION.
DATA mv_idx TYPE sytabix.
DATA mv_size TYPE sytabix.
DATA mt_list TYPE tt_trace.
ENDCLASS.
A use case is the event handler of ALV Grid: I pass an iterator and the handler processes selected entries without having to know about the selection or additional validation logic. It is just a case of separation of concern.
CLASS lcl_iterator IMPLEMENTATION.
METHOD constructor.
CLEAR mv_idx.
mt_list = it_messages. " shared, never changed
mv_size = COND #( WHEN iv_stop IS INITIAL THEN lines( mt_list )
ELSE iv_stop ).
skip( iv_start - 1 ).
ENDMETHOD.
METHOD next.
skip( ).
rs_data = mt_list[ mv_idx ].
ENDMETHOD.
METHOD has_next.
rv_flag = boolc( mv_idx < mv_size ).
ENDMETHOD.
METHOD skip.
ADD iv_count TO mv_idx.
ENDMETHOD.
ENDCLASS.
Reminder: a new object must be created for each iteration. Access to the table index MV_IDX should be avoided. But if worst comes to the worst...
this ABAP criminal will avoid ABAP police infested blogs
regards,
JNN
03-04-2015 9:52 PM
Nice implementation. Couple questions:
Sections like:
COND #( WHEN iv_stop IS INITIAL THEN lines( mt_list )
ELSE iv_stop ).
mt_list[ mv_idx ]
Don't look like abap pieces of code. Inline conditionals aren't supported in abap to my knowledge and your table indexing looks like accessing the index of an array. Are these pieces of abap code that are supported in different version of SAP?
Also have you thought about adding an initialize method to reset the count? It's through a method to a protected variable so it's a controlled touch to the index counter.
03-04-2015 11:02 PM
Hello Jeffrey,
you must read the ABAP news.
No reset is needed if a new iterator is used for each transversal. I use the clone( ) method
METHODS clone IMPORTING iv_start TYPE sytabix DEFAULT 1
iv_stop TYPE sytabix OPTIONAL
PREFERRED PARAMETER iv_start
RETURNING VALUE(ro_iter) TYPE REF TO lcl_iterator.
METHOD clone.
ro_iter = NEW #(
it_messages = mt_list
iv_start = iv_start
iv_stop = COND #( WHEN iv_stop IS SUPPLIED THEN iv_stop
ELSE mv_size ) ).
ENDMETHOD.
regards,
JNN
03-05-2015 12:04 AM
Took a quick glance at blog and i'm excited. Right now I'm on 7.31 so i didn't know that 7.4 existed. I've only ventured into the SCN world over the last weekend and it's already helping quite a bit.
My apologises for questioning your code, it looked like a java hybrid with the inline conditional and table indexing.
I like your implementation of the iterator class, I find iterators extremely useful.
Do you happen to use the iterator with more advanced data structures such as hashmaps, arraylists, lists, etc?
03-05-2015 1:40 AM
03-05-2015 5:35 PM
Hey Custodio,
That's a solid article. I just glanced at it quickly and from the looks of it, will be quite helpful.
Love the term JAVAPER, was never a fan of the ABAPER name, but i can do JAVAPER.
I'm currently implementing the java.util.logger package in abap so their interpretation of java code and data structure implementation should be useful.
The info is much appreciated bro,
Jeff