使用RTTS从内部表为字段创建范围

时间:2019-05-24 08:23:42

标签: oop abap rtts

我想创建一个包含两个参数的函数/自定义类方法:

1)IM_ITAB类型ANY TABLE

2)IM_COMPONENT类型STRING

并返回1个参数:

1)EX_RANGE类型PIQ_SELOPT_T

所以,算法是这样的:

  • 首先,我们检查组件名称是否全部存在
  • 然后,我们检查内部表是否为空。
  • 然后,我们遍历内部表分配组件和填充范围表。代码在下面。
Mockito.when()

但是我想进一步改进该方法。如果导入的内部表有255列,则遍历该表将花费更长的时间。但是我只需要一列即可组成范围。

所以我想获取内部表的组件,然后仅选择一个组件,创建仅包含该组件的新线型,然后使用该线型创建内部表并复制。

这是与我要实现的目标相对应的伪代码:

Set<String> ordersBuy = mock(Set.class);
Set<String> ordersSell = mock(Set.class);

ArgumentCaptor<Function<CurrencyPairDTO, String >> getBase = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<CurrencyPairDTO, String>> getCounter = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<MyOrdersSmartDTO, Set<String>>> getSell = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<MyOrdersSmartDTO, Set<String>>> getBuy = ArgumentCaptor.forClass(Function.class);

when(this.recalculateInMemoryBoardUtils.fillSetByMarginOrdersUsingFunctions(eq(instancesByUsername), eq(currencyBase), getBase.capture(), getSell.capture())).thenReturn(ordersSell);
when(this.recalculateInMemoryBoardUtils.fillSetByMarginOrdersUsingFunctions(eq(instancesByUsername), eq(currencyBase), getCounter.capture(), getBuy.capture())).thenReturn(ordersBuy);

如何使用RTTS“剪切”一个组件并创建新的线型?

3 个答案:

答案 0 :(得分:0)

您使所有事情都变得过于复杂,不需要RTTS。

DEFINE make_range.
  ex_range = VALUE #( BASE ex_range ( sign = 'I' option = 'EQ' low = &1 ) ).
END-OF-DEFINITION.

LOOP AT im_itab ASSIGNING FIELD-SYMBOL(<fs_line>).
  ASSIGN COMPONENT im_component OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_field>).
  CHECK sy-subrc = 0 AND <fs_field> IS NOT INITIAL.
  make_range <fs_field>.
ENDLOOP.

是的,正如Sandra所说,使用RTTS不会获得任何性能,恰恰相反。

答案 1 :(得分:0)

Surprisingly, this variant turned out to be faster:

CLASS-METHODS make_range_variant_2
  IMPORTING
    sample        TYPE table_type
    column        TYPE string
  RETURNING
    VALUE(result) TYPE range_type.

METHOD make_range_variant_2.

  TYPES:
    BEGIN OF narrow_structure_type,
      content TYPE char32,
    END OF narrow_structure_type.

  TYPES narrow_table_type TYPE STANDARD TABLE OF narrow_structure_type WITH EMPTY KEY.

  DATA narrow_table TYPE narrow_table_type.

  DATA(mapping) =
    VALUE cl_abap_corresponding=>mapping_table_value(
      ( kind = cl_abap_corresponding=>mapping_component srcname = column dstname = 'CONTENT' ) ).

  DATA(mover) =
    cl_abap_corresponding=>create_with_value(
      source      = sample
      destination = narrow_table
      mapping     = mapping ).

  mover->execute(
    EXPORTING
      source      = sample
    CHANGING
      destination = narrow_table ).

  LOOP AT narrow_table ASSIGNING FIELD-SYMBOL(<row>).

    INSERT VALUE #(
        sign   = 'I'
        option = 'EQ'
        low    = <row>-content )
      INTO TABLE result.

  ENDLOOP.

ENDMETHOD.

CL_ABAP_CORRESPONDING delegates to a kernel function for the structure-to-structure move, which apparently is faster than the ABAP-native ASSIGN COMPONENT [...] OF STRUCTURE [...] TO FIELD-SYMBOL [...]. The actual loop then seems to be faster because it uses fixed-name assignments.

Maybe somebody could verify.

答案 2 :(得分:0)

我不会去买Macro

Data:
      lr_data type ref to data.

FIELD-SYMBOLS:
      <lv_component> TYPE any,
      <ls_data> TYPE any.

CREATE DATA lr_data LIKE LINE OF im_itab.
ASSIGN lr_data->* TO <ls_data>.

"Check whether im_component exists
ASSIGN COMPONENT im_component OF STRUCTURE <ls_data> TO <lv_component>.

CHECK sy-subrc EQ 0.

LOOP AT im_itab INTO <ls_data>.
  APPEND VALUE #( sign = 'I' option = 'EQ' low = <lv_component> ) TO ex_range.
ENDLOOP.