View Javadoc
1   package lv.id.jc.piglatin.actuator;
2   
3   import java.util.Map;
4   import java.util.concurrent.atomic.AtomicInteger;
5   
6   import org.springframework.beans.factory.annotation.Qualifier;
7   import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
8   import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
9   import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
10  import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
11  import org.springframework.stereotype.Component;
12  
13  /**
14   * This class represents an endpoint for counting translations.
15   * It provides methods for reading, setting, and resetting the translation count.
16   */
17  @Component
18  @Endpoint(id = "counter")
19  public class TranslationCountEndpoint {
20  
21      private final AtomicInteger counterService;
22  
23      public TranslationCountEndpoint(@Qualifier("translationCounter") AtomicInteger counterService) {
24          this.counterService = counterService;
25      }
26  
27      /**
28       * Retrieves the current count of translations.
29       *
30       * @return A Map containing the count of translations.
31       *         The key "count" maps to the current count value.
32       */
33      @ReadOperation
34      public Map<String, Integer> count() {
35          return Map.of("count", counterService.get());
36      }
37  
38      /**
39       * Sets the translation counts to the specified value.
40       *
41       * @param count The new count value.
42       * @return A Map containing the updated count of translations.
43       *         The key "count" maps to the new count value.
44       */
45      @WriteOperation
46      public Map<String, Integer> set(int count) {
47          counterService.set(count);
48          return count();
49      }
50  
51      /**
52       * Resets the translation counts to zero.
53       */
54      @DeleteOperation
55      public void reset() {
56          counterService.set(0);
57      }
58  }