View Javadoc
1   package com.guinetik.hexafun.hexa;
2   
3   import java.util.Objects;
4   
5   /**
6    * Type-safe key for use case registration and invocation.
7    * Provides compile-time type checking instead of string-based dispatch.
8    *
9    * <p>Usage:
10   * <pre class="language-java">{@code
11   * // Define keys as constants
12   * public interface MyUseCases {
13   *     UseCaseKey<CreateInput, Result<Entity>> CREATE = UseCaseKey.of("create");
14   *     UseCaseKey<String, Result<Entity>> FIND = UseCaseKey.of("find");
15   * }
16   *
17   * // Use in DSL
18   * HexaFun.dsl()
19   *     .useCase(MyUseCases.CREATE)
20   *         .validate(...)
21   *         .handle(...)
22   *     .build();
23   *
24   * // Type-safe invocation
25   * Result<Entity> result = app.invoke(MyUseCases.CREATE, input);
26   * }</pre>
27   *
28   * @param <I> The input type of the use case
29   * @param <O> The output type of the use case
30   */
31  public final class UseCaseKey<I, O> {
32  
33      private final String name;
34  
35      private UseCaseKey(String name) {
36          this.name = Objects.requireNonNull(
37              name,
38              "Use case name cannot be null"
39          );
40      }
41  
42      /**
43       * Create a new type-safe use case key.
44       *
45       * @param name The unique name for this use case
46       * @param <I> The input type
47       * @param <O> The output type
48       * @return A new UseCaseKey instance
49       */
50      public static <I, O> UseCaseKey<I, O> of(String name) {
51          return new UseCaseKey<>(name);
52      }
53  
54      /**
55       * Get the string name of this use case key.
56       * @return The use case name
57       */
58      public String name() {
59          return name;
60      }
61  
62      @Override
63      public boolean equals(Object o) {
64          if (this == o) return true;
65          if (o == null || getClass() != o.getClass()) return false;
66          UseCaseKey<?, ?> that = (UseCaseKey<?, ?>) o;
67          return name.equals(that.name);
68      }
69  
70      @Override
71      public int hashCode() {
72          return name.hashCode();
73      }
74  
75      @Override
76      public String toString() {
77          return "UseCaseKey(" + name + ")";
78      }
79  }