Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ lo_map->set(
iv_val = '2' ). " raises cx_no_check
```

- you may switch the map into "list" mode - this way there is no guarantee for the record uniquiness, but can be usedful for some applications.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uniqueness, useful

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tnx! fixed


```abap
lo = zcl_abap_string_map=>create( iv_list_mode = abap_false ).
```

For more examples see [unit tests code](https://github.com/sbcgua/abap-string-map/blob/master/src/zcl_abap_string_map.clas.testclasses.abap)

## Blog posts
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Legend
+ : added
- : removed

v1.0.5, 2024-??-??
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024-11-27

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually don't bump version for minor changes. But on the other hand there are so few changes to the string map, that why not. Done.

------------------
+ list mode (mode with no uniquness guarantee)

v1.0.4, 2024-06-24
------------------
+ to_entries - saves entries to a standard table with 2 char-like components (#11)
Expand Down
23 changes: 18 additions & 5 deletions src/zcl_abap_string_map.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ class zcl_abap_string_map definition
types:
tty_entries type standard table of ty_entry with key k.
types:
tts_entries type sorted table of ty_entry with unique key k.
tts_entries type sorted table of ty_entry with non-unique key k.

data mt_entries type tts_entries read-only.

class-methods create
importing
!iv_case_insensitive type abap_bool default abap_false
!iv_list_mode type abap_bool default abap_false " removes uniqueness requirement,
" use with care: it is not the primary scenario
!iv_from type any optional
preferred parameter iv_from
returning
value(ro_instance) type ref to zcl_abap_string_map.
methods constructor
importing
!iv_case_insensitive type abap_bool default abap_false
!iv_list_mode type abap_bool default abap_false " removes uniqueness requirement,
" use with care: it is not the primary scenario
!iv_from type any optional.

methods get
Expand Down Expand Up @@ -119,6 +123,7 @@ class zcl_abap_string_map definition
data mv_is_strict type abap_bool.
data mv_read_only type abap_bool.
data mv_case_insensitive type abap_bool.
data mv_list_mode type abap_bool.
ENDCLASS.


Expand All @@ -140,6 +145,7 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
method constructor.
mv_is_strict = abap_true.
mv_case_insensitive = iv_case_insensitive.
mv_list_mode = iv_list_mode.

if iv_from is not initial.
data lo_type type ref to cl_abap_typedescr.
Expand Down Expand Up @@ -180,6 +186,7 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
method create.
create object ro_instance
exporting
iv_list_mode = iv_list_mode
iv_case_insensitive = iv_case_insensitive
iv_from = iv_from.
endmethod.
Expand Down Expand Up @@ -374,13 +381,19 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
lv_key = iv_key.
endif.

read table mt_entries assigning <entry> with key k = lv_key.
if sy-subrc = 0.
<entry>-v = iv_val.
else.
if mv_list_mode = abap_true.
ls_entry-k = lv_key.
ls_entry-v = iv_val.
insert ls_entry into table mt_entries.
else.
read table mt_entries assigning <entry> with key k = lv_key.
if sy-subrc = 0.
<entry>-v = iv_val.
else.
ls_entry-k = lv_key.
ls_entry-v = iv_val.
insert ls_entry into table mt_entries.
endif.
endif.

ro_map = me.
Expand Down
47 changes: 47 additions & 0 deletions src/zcl_abap_string_map.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ltcl_string_map definition

methods create_from for testing.
methods case_insensitive_create for testing.
methods list_mode for testing.

endclass.

Expand Down Expand Up @@ -951,4 +952,50 @@ class ltcl_string_map implementation.

endmethod.

method list_mode.

data lo_cut type ref to zcl_abap_string_map.

lo_cut = zcl_abap_string_map=>create( ).
lo_cut->setx( 'a:1' ).
lo_cut->setx( 'a:2' ).

cl_abap_unit_assert=>assert_equals(
exp = 1
act = lo_cut->size( ) ).
cl_abap_unit_assert=>assert_equals(
exp = lo_cut->get( 'a' )
act = '2' ).

lo_cut = zcl_abap_string_map=>create( iv_list_mode = abap_true ).
lo_cut->setx( 'a:1' ).
lo_cut->setx( 'a:2' ).

cl_abap_unit_assert=>assert_equals(
exp = 2
act = lo_cut->size( ) ).

data lt_entries type zcl_abap_string_map=>tty_entries.
data ls_item like line of lt_entries.
lt_entries = lo_cut->mt_entries.
sort lt_entries by k v.

read table lt_entries index 1 into ls_item.
cl_abap_unit_assert=>assert_equals(
exp = '1'
act = ls_item-v ).

read table lt_entries index 2 into ls_item.
cl_abap_unit_assert=>assert_equals(
exp = '2'
act = ls_item-v ).

lo_cut->delete( 'a' ).
cl_abap_unit_assert=>assert_equals(
exp = 0
act = lo_cut->size( ) ).

endmethod.


endclass.