View Javadoc

1   package com.atlassian.vcache;
2   
3   import java.time.Duration;
4   import java.util.Optional;
5   import javax.annotation.Nonnull;
6   
7   import com.atlassian.annotations.PublicApi;
8   
9   import static java.util.Objects.requireNonNull;
10  
11  /**
12   * Builder for creating {@link ExternalCacheSettings} instances.
13   *
14   * @since 1.0
15   */
16  @PublicApi
17  public class ExternalCacheSettingsBuilder
18  {
19      private Optional<Duration> defaultTtl = Optional.empty();
20      private Optional<Integer> entryCountHint = Optional.empty();
21      private Optional<ChangeRate> dataChangeRateHint = Optional.empty();
22      private Optional<ChangeRate> entryGrowthRateHint = Optional.empty();
23  
24      /**
25       * Creates an instance with no settings initialised.
26       */
27      public ExternalCacheSettingsBuilder()
28      {
29      }
30  
31      /**
32       * Constructs an instance initialised with the supplied settings.
33       * @param settings the settings to configure the instance with.
34       */
35      public ExternalCacheSettingsBuilder(ExternalCacheSettings settings)
36      {
37          this.defaultTtl = requireNonNull(settings.getDefaultTtl());
38          this.entryCountHint = requireNonNull(settings.getEntryCountHint());
39          this.dataChangeRateHint = requireNonNull(settings.getDataChangeRateHint());
40          this.entryGrowthRateHint = requireNonNull(settings.getEntryGrowthRateHint());
41      }
42  
43      /**
44       * Returns a new {@link ExternalCacheSettings} instance configured using the supplied settings.
45       * @return a new {@link ExternalCacheSettings} instance configured using the supplied settings.
46       */
47      @Nonnull
48      public ExternalCacheSettings build()
49      {
50          return new ExternalCacheSettings(
51                  defaultTtl, entryCountHint, dataChangeRateHint, entryGrowthRateHint);
52      }
53  
54      /**
55       * Specifies the default time-to-live for entries to be held in the cache. Must be a positive number.
56       *
57       * @param ttl the default time-to-live for entries to be held in the cache
58       * @return the builder
59       * @throws IllegalArgumentException thrown if <tt>ttl</tt> is not positive
60       */
61      @Nonnull
62      public ExternalCacheSettingsBuilder defaultTtl(Duration ttl)
63      {
64          if (ttl.getNano() <= 0)
65          {
66              throw new IllegalArgumentException("ttl must be greater than zero, passed: " + ttl);
67          }
68  
69          defaultTtl = Optional.of(ttl);
70          return this;
71      }
72  
73      /**
74       * Provides a hint on the expected number of entries to be held in the cache. Must not be a negative number.
75       * The implementation is free to decide how to interpret <tt>0</tt>. For example, it may decide to still cache
76       * values externally, or it may decide to not store entries in the cache.
77       *
78       * @param hint the expected number of entries to be held in the cache
79       * @return the builder
80       * @throws IllegalArgumentException thrown if <tt>hint</tt> is negative
81       */
82      @Nonnull
83      public ExternalCacheSettingsBuilder entryCountHint(int hint)
84      {
85          if (hint < 0)
86          {
87              throw new IllegalArgumentException("maxEntries cannot be negative, passed: " + hint);
88          }
89  
90          entryCountHint = Optional.of(hint);
91          return this;
92      }
93  
94      /**
95       * Provides a hint on the expected change rate for data updates.
96       *
97       * @param hint the expected change rate for data updates.
98       * @return the builder
99       */
100     @Nonnull
101     public ExternalCacheSettingsBuilder dataChangeRateHint(ChangeRate hint)
102     {
103         dataChangeRateHint = Optional.of(hint);
104         return this;
105     }
106 
107     /**
108      * Provides a hint on the expected change rate for entry additions.
109      *
110      * @param hint the expected change rate for entry additions.
111      * @return the builder
112      */
113     @Nonnull
114     public ExternalCacheSettingsBuilder entryGrowthRateHint(ChangeRate hint)
115     {
116         entryGrowthRateHint = Optional.of(hint);
117         return this;
118     }
119 }