CLASS {{p}}sample_batcher DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. TYPES: ty_test_type TYPE c LENGTH 4, ty_sample_id TYPE c LENGTH 10, ty_priority TYPE i. TYPES: BEGIN OF ty_batch, test_type TYPE ty_test_type, total TYPE i, urgent TYPE i, END OF ty_batch. TYPES tt_batches TYPE STANDARD TABLE OF ty_batch WITH EMPTY KEY. METHODS add_sample IMPORTING iv_sample_id TYPE ty_sample_id iv_test_type TYPE ty_test_type iv_priority TYPE ty_priority RETURNING VALUE(rv_added) TYPE abap_bool. METHODS get_batches RETURNING VALUE(rt_batches) TYPE tt_batches. METHODS get_batch IMPORTING iv_test_type TYPE ty_test_type RETURNING VALUE(rs_batch) TYPE ty_batch. PRIVATE SECTION. CONSTANTS: BEGIN OF c_priority, urgent TYPE ty_priority VALUE 1, normal TYPE ty_priority VALUE 2, routine TYPE ty_priority VALUE 3, END OF c_priority. TYPES: BEGIN OF ty_sample_entry, sample_id TYPE ty_sample_id, END OF ty_sample_entry. TYPES: tt_batch_index TYPE SORTED TABLE OF ty_batch WITH UNIQUE KEY test_type, tt_sample_index TYPE HASHED TABLE OF ty_sample_entry WITH UNIQUE KEY sample_id. DATA mt_batches TYPE tt_batch_index. DATA mt_samples TYPE tt_sample_index. METHODS normalize_id IMPORTING iv_id TYPE ty_sample_id RETURNING VALUE(rv_id) TYPE ty_sample_id. METHODS normalize_type IMPORTING iv_type TYPE ty_test_type RETURNING VALUE(rv_type) TYPE ty_test_type. METHODS is_valid_priority IMPORTING iv_priority TYPE ty_priority RETURNING VALUE(rv_valid) TYPE abap_bool. METHODS count_sample IMPORTING iv_test_type TYPE ty_test_type iv_priority TYPE ty_priority. ENDCLASS. CLASS {{p}}sample_batcher IMPLEMENTATION. METHOD add_sample. DATA(lv_sample_id) = normalize_id( iv_sample_id ). DATA(lv_test_type) = normalize_type( iv_test_type ). IF lv_sample_id IS INITIAL OR lv_test_type IS INITIAL OR is_valid_priority( iv_priority ) = abap_false OR line_exists( mt_samples[ sample_id = lv_sample_id ] ). RETURN. ENDIF. INSERT VALUE #( sample_id = lv_sample_id ) INTO TABLE mt_samples. count_sample( iv_test_type = lv_test_type iv_priority = iv_priority ). rv_added = abap_true. ENDMETHOD. METHOD get_batches. rt_batches = mt_batches. SORT rt_batches BY test_type. ENDMETHOD. METHOD get_batch. DATA(lv_test_type) = normalize_type( iv_test_type ). READ TABLE mt_batches INTO rs_batch WITH TABLE KEY test_type = lv_test_type. IF sy-subrc <> 0. rs_batch = VALUE #( test_type = lv_test_type ). ENDIF. ENDMETHOD. METHOD normalize_id. rv_id = to_upper( replace( val = iv_id sub = ` ` with = `` occ = 0 ) ). ENDMETHOD. METHOD normalize_type. rv_type = to_upper( replace( val = iv_type sub = ` ` with = `` occ = 0 ) ). ENDMETHOD. METHOD is_valid_priority. rv_valid = xsdbool( iv_priority = c_priority-urgent OR iv_priority = c_priority-normal OR iv_priority = c_priority-routine ). ENDMETHOD. METHOD count_sample. READ TABLE mt_batches ASSIGNING FIELD-SYMBOL() WITH TABLE KEY test_type = iv_test_type. IF sy-subrc <> 0. INSERT VALUE #( test_type = iv_test_type ) INTO TABLE mt_batches ASSIGNING . ENDIF. -total = -total + 1. IF iv_priority = c_priority-urgent. -urgent = -urgent + 1. ENDIF. ENDMETHOD. ENDCLASS.