Skip to Content

Glossary

a JAVAPER – a person who knows ABAP and wants to inject some of Java flavour tohis/her coding. 

This blog is for people who want to go beyond ABAP thinking and try to feel like JAVAPER. Therefore this blog is for me too:)

This blog is probably not for people who know Java better then me, so for everyone who developed at least one robust application using this language.

 

What all Java Gurus know

In Java we

have very popular ArrayList class*.

This can be used to create a collection for generic object type. This can be

either basic object type (by means of “packing” classes like +Integer, Boolean, String, Character +etc.

) or simply other object of any type. Quick jump to 

Java API documentation

 under ArrayList let us analyze most useful methods

of the class.

*from Java 5.0 (so called Tiger) it has been enhanced with bunch of classes ArrayList<E>, but this we won’t be

discussing here.

image

This

generic class allows to add any kind of object programmer has created. This

brings real fun and facility to have such generic container, as all objects (no

matter the type) are treated uniformly. Let’s create similar class

in ABAP in order to have such utility class for our ABAP objects.

First let’s

create it as local class and later we will go for its global version with some

fancy stuff in there.

CLASS arrayList DEFINITION.

  PUBLIC SECTION.

    INTERFACES Iterable.

    ALIASES: Iterator FOR Iterable~iterator.

    METHODS: add        IMPORTING e TYPE REF TO object

                        RETURNING value(added) TYPE boole_d,

             indexof    IMPORTING e TYPE REF TO object

                        RETURNING value(index) TYPE i,

             get        IMPORTING index TYPE i

                        RETURNING VALUE(e) TYPE REF TO object,

             contains   IMPORTING e TYPE REF TO object

                        RETURNING value(contains) TYPE xflag,

             remove     IMPORTING e TYPE REF TO object,

             clear      ,

             isEmpty    RETURNING VALUE(is) TYPE boole_d,

             size       RETURNING value(size) TYPE i.

  PRIVATE SECTION.

  • our generic container is here

    DATA collection TYPE TABLE OF REF TO object.

ENDCLASS.                 

CLASS arrayList IMPLEMENTATION.

  METHOD add.

    READ TABLE collection WITH KEY table_line = e

               TRANSPORTING NO FIELDS.

    IF sy-subrc <> 0.

APPEND e TO collection.

added = ‘X’.

ENDIF.

ENDMETHOD.

METHOD indexof.

index = -1.

READ TABLE collection WITH KEY table_line = e

TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

index = sy-tabix. “in Java we should get sy-tabix – 1 as tables starts from 0 index

ENDIF.

ENDMETHOD.

METHOD get.

READ TABLE collection INTO e INDEX index.

ENDMETHOD.

METHOD contains.

READ TABLE collection WITH KEY table_line = e

TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

contains = ‘X’.

ENDIF.

ENDMETHOD.

METHOD remove.

DELETE collection WHERE table_line = e.

ENDMETHOD.

METHOD clear.

REFRESH collection.

ENDMETHOD.

METHOD isEmpty.

IF me->size( ) = 0.

is = ‘X’.

ENDIF.

ENDMETHOD.

METHOD size.

DESCRIBE TABLE collection LINES size.

ENDMETHOD.

ENDCLASS. 

No big

deal, isn’t it? For our generic container we simply used ABAP internal table

accepting objects of any type. You may think now why we ever need such class.

There is nothing sophisticated in it. Couple of READs and other basic

statements with table as operand.  Yeah,

sure, but there are several reasons why it is good to use such technique:

    • Imagine all these snippets you need to

write yourself over and over again just to make sure that your TABLE

has specific entry or has any data inside, here we do it only once and

forget how to read it or add new object to it

    • We switch b/w ABAP-TABLE thinking (so

procedural approach) to more abstract ABAP-CONTAINER thinking (so Object

Oriented approach)

    • This originates from Java, so hey, this

must be designed in good Object Oriented way. And as all Java Gurus use it

each day, we must do so too if we want to become a Javaper.

 

Anyhow let’s leave this and see what else we

can do with this class that might not be so obvious at first and how we can use

that in our programs.

 

Iterator

You must

have already heard of iterator. This is a special type of entity which allows

to loop over any kind of collection we could imagine. Although the true power

of iterator comes in Java, you can also use it in many cases in ABAP. You could

i.e. iterate over a table of

objects, rows of BSP UI control table, even components of structure.

What it is?

The Iterator is simply an interface

with very limited functionality. In Java it has barely 3 methods.

image

 

You can use

it in any class and implement it in any way you want to achieve different form

of iteration. This kind of iterator is also used here in our ArrayList class.

There is however one catch.

 

What we are used to

We are used

to having such iterator implemented directly in class which objects we would

like to iterate through. So we would have 3 iterator’s methods

“embedded” in class body which we would provide implementation for.

As is in ArrayList

If we look

at this

class API

once again we would see that in fact there are no such (iterator’s) methods there. Instead we have

method

!https://weblogs.sdn.sap.com/weblogs/images/251824489/JAVAPER_AI_get_iterator.jpg|height=39|alt=image|width=494|src=https://weblogs.sdn.sap.com/weblogs/images/251824489/JAVAPER_AI_get_iterator.jpg|border=0!</body>

To report this post you need to login first.

20 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha
    Hi Marcin,
    You’re blog is a hit(as always!).
    Actually i have seen this iterator logic being used in XML processing interfaces e.g., IF_IXML_NODE_ITERATOR. So a JAVAPer does(did??) exist inside SAP AG 😉
    Expecting more JAVAP programming blogs coming from your stable.
    Cheers,
    Suhas

    PS: Did you coin this the term “JAVAPer”? Sounds cool 🙂

    (0) 
    1. Marcin Pciak Post author
      Thanks Suhas for kind words as usual. Yes, the iterator isn’t anything new to ABAP but I just wanted to encourage people to start using it or not be scared once they face it.

      I will think of authorizing this term and take some profit from it;)

      I will try to continue the serries as I think there are many things we can learn from JAVA and adapt to our ABAP programs. And more importantly, this is just a fun:)

      (0) 
  2. Kiran Kumar Valluru
    Hi Marcin,

    Very Nice blog. Since I am from JAVA base, I know ArrayList and the way You explained here is very nice.

    Expecting some more blogs.. please continue this series.

    Thanks,

    (0) 
  3. Eitan Rosenberg
    I might be wrong but it seems to me that the add method implementation is not the same as in Java.

    According to the Java document the “Add” method:
    “Appends the specified element to the end of this list.”

    In this case I think that “METHOD add” should be more like this:

    METHOD add.
           APPEND e TO collection.
      ENDMETHOD. 

    (0) 
    1. Marcin Pciak Post author
      Actually it is like you mention

      METHOD add.
      READ TABLE collection WITH KEY table_line = e
      TRANSPORTING NO FIELDS.
      IF sy-subrc <> 0.
      APPEND e TO collection.
      added = ‘X’.
      ENDIF.
      ENDMETHOD.

      It simply does additional check in order not to add same object to list twice. Also flag is set informing about success of fail.

      (0) 
        1. Marcin Pciak Post author
          Although this isn’t of higest matter here, yes you are right. I thought Java won’t let us add same object to ArrayList. Here obviously someone would have to remove this additional check.

          Regards
          Marcin

          (0) 
  4. Michelle Crapo

    This is very useful.  I hope for more blogs on JAVAPER.  It will help ABAPers as well as JAVA programmers.  I know hence the name JAVAPER.   Michelle

    (0) 
  5. R. Vleeschhouwer
    Hi Marcin,

    Nice blog about the ArrayList and the Iterator.
    In SAP the ArrayList is called an ObjectMap.

    Please take a look at class: CL_OBJECT_MAP

    You get the iterator with method: GET_VALUES_ITERATOR

    This method will return an interface: IF_OBJECT_COLLECTION_ITERATOR

    Regards,

    Robin Vleeschhouwer

    (0) 
    1. Marcin Pciak Post author
      Thanks Robin, nice to know that.

      I don’t know SAP Java, all I want to achieve writting this blog is sharing my reflections of how ABAP can take some of what Java is expert in for long time. I am still just learning basics of the latter and trying to map this new knowledge to SAP ABAP world which is more close to me.

      Regards
      Marcin

      (0) 
      1. R. Vleeschhouwer
        Hi Marcin,

        Sorry that I gave you the wrong impression.
        What I am talking about is ABAP.
        Please look at the following ABAP code:

        DATA lo_object_map TYPE REF TO cl_object_map.
        DATA lo_iterator TYPE REF TO if_object_collection_iterator.
        DATA lo_test TYPE REF TO zcl_test. “Any object

        CREATE OBJECT lo_object_map.
        CREATE OBJECT lo_test.

        lo_object_map->put( EXPORTING
                              key   = ‘UNIQUE_KEY’
                              value = lo_test ).

        lo_iterator = so_object_map->get_values_iterator( ).

        WHILE lo_iterator->has_next( ) = abap_true.
          lo_test ?= lo_iterator->get_next( ).
          [… abap code]
        ENDWHILE.

        This is the SAP standard implementation of the ArrayList. For example this is used in Web Dynpro ABAP for getting the Windows and Views from a Component.

        Regards,

        Robin Vleeschhouwer

        (0) 
        1. Marcin Pciak Post author
          Hi Robin,

          So now we have two possible ways of achieving the same. Standard one via Object Map and our custom one discussed in the blog. And we are aware how both are more or less implemented:)

          Thanks for sharing that
          Marcin

          (0) 
          1. Michelle Crapo
            Marcin / Robin,

            A BIG thank you to both of you. The best way to learn is usually from the comments. But I’ve learned how to build a “JAVAish” class as well.

            So I received double the knowledge from one blog.

            Excellent – Marcin I’m glad you asked the question. I was wondering the same thing myself.

            I also wonder how to find the SAP Classes – Ah – that’s probably another blog.

            (0) 
    2. Mauricio Cruz
      Thanks for this tip, and also congrats to the blog author, very interesting to see an JAVAPER in action!

      Robin’s comment got me thinking if there are more global ABAP classes that implements java concepts… I’ll share if I find anything.

      BR
      Mauricio Roberto Cruz

      (0) 
  6. Michelle Crapo
    Marcin a big thank you for putting your code out here.  There are always 10,000 plus ways of doing everything.  Me – sometimes I cross my fingers when I put my code out.  But as I always say – if I can learn from the comments then WOW – it’s a great day.

    So to you – WOW – it’s a great day!

    Michelle

    (0) 
    1. Marcin Pciak Post author
      Thank you Michelle for your remarks too. It’s very nice to hear that someone appreciates your effort. That encourages to keep working and sharing what you’ve learnt.

      Cheers
      Marcin

      (0) 
  7. Itay Assraf
    Thank you Marcin, great blog.

    I think the Method “remove” in the Class “arrayListIterator”
    Should look like:

    METHOD remove.
    DATA size TYPE i.
    DATA e TYPE REF TO object.

    size = object->size( ).
    IF currindex <= size.
    e = object->get( currindex ).
    object->remove( e ).
    SUBTRACT 1 FROM currindex.
    ENDIF.
    ENDMETHOD.

    (0) 
    1. Marcin Pciak Post author
      Yes, you are right. Initially I used object as parameter of REMOVE method, then I changed it to be index and forgot to adap the coding. Thanks for the insight. Corrected.

      Regards
      Marcin

      (0) 
  8. Tapio Reisinger

    Hi Marcin,

    great Blog! But could you check the content. For me it looks somehow corrupted and the global part is missing. Maybe it’s lost due to the change to new SDN?!?

    It would be really nice to se the full picture of this Blog.

    Best reagrds,

    Tapio

    (0) 
    1. Marcin Pciak Post author

      Hello Tapio,

      I am sorry but this had been formatted well until SDN changed their look and feel. Seems like this is a common issue with all the blogs which have been created before this critical date. I can see right now that most of the content got lost and chaning it would mean writing the post from the scratch. By the way, it’s a pitty SDN doens’t share this in the original form or make it available i.e. by means of an archive.

      Regards

      Marcin

      (0) 

Leave a Reply