View Javadoc
1   package fr.tiogars.inception.common.services;
2   
3   import java.util.ArrayList;
4   import java.util.LinkedHashSet;
5   import java.util.List;
6   import java.util.function.Predicate;
7   
8   import fr.tiogars.inception.common.models.DeleteSelectedResponse;
9   
10  /**
11   * Applies a single-record deletion to each selected identifier.
12   *
13   * @since 0.1.0
14   */
15  public final class DeleteSelectedSupport {
16  
17      private DeleteSelectedSupport() {
18      }
19  
20      /**
21       * Deletes each distinct identifier, preserving the input order.
22       *
23       * @param ids    selected identifiers
24       * @param delete deletion returning {@code true} when the record was deleted
25       * @return deleted and skipped identifiers
26       */
27      public static DeleteSelectedResponse deleteEach(List<Long> ids, Predicate<Long> delete) {
28          List<Long> deleted = new ArrayList<>();
29          List<Long> skipped = new ArrayList<>();
30          for (Long id : new LinkedHashSet<>(ids)) {
31              if (delete.test(id)) {
32                  deleted.add(id);
33              } else {
34                  skipped.add(id);
35              }
36          }
37          return new DeleteSelectedResponse(List.copyOf(deleted), List.copyOf(skipped));
38      }
39  }