Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Abap Iterating and Iterator Classes

Former Member
0 Kudos

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:

Fabio Pagoti


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.

Matthew Billingham


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.

Joao Sousa


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.

Jeffrey Vander Graaf

  1. 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.
  2. 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.
  3. 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:

1 ACCEPTED SOLUTION

custodio_deoliveira
Active Contributor
0 Kudos

Hi Jeffrey,

I added a comment on that blog post, but will duplicate it here. This is a nice blog post on this subject:

Cheers,

Custodio

6 REPLIES 6

nomssi
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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.

nomssi
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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?

custodio_deoliveira
Active Contributor
0 Kudos

Hi Jeffrey,

I added a comment on that blog post, but will duplicate it here. This is a nice blog post on this subject:

Cheers,

Custodio

0 Kudos

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