View Javadoc

1   package com.atlassian.vcache;
2   
3   import com.atlassian.annotations.PublicApi;
4   
5   import java.time.Duration;
6   import java.util.Optional;
7   
8   import static com.atlassian.vcache.SettingsUtils.ifPresent;
9   import static java.util.Objects.requireNonNull;
10  
11  /**
12   * An immutable representation of settings for an {@link ExternalCache}.
13   *
14   * @since 1.0
15   */
16  @PublicApi
17  public class ExternalCacheSettings {
18      private final Optional<Duration> defaultTtl;
19      private final Optional<Integer> entryCountHint;
20      private final Optional<ChangeRate> dataChangeRateHint;
21      private final Optional<ChangeRate> entryGrowthRateHint;
22  
23      ExternalCacheSettings(
24              Optional<Duration> defaultTtl,
25              Optional<Integer> entryCountHint,
26              Optional<ChangeRate> dataChangeRateHint,
27              Optional<ChangeRate> entryGrowthRateHint) {
28          this.defaultTtl = requireNonNull(defaultTtl);
29          this.entryCountHint = requireNonNull(entryCountHint);
30          this.dataChangeRateHint = requireNonNull(dataChangeRateHint);
31          this.entryGrowthRateHint = requireNonNull(entryGrowthRateHint);
32      }
33  
34      /**
35       * Returns a new {@link ExternalCacheSettings} instance where the current settings
36       * are overridden with settings specified in <tt>overrides</tt>.
37       *
38       * @param overrides contains the settings to override
39       * @return a new {@link ExternalCacheSettings} instance with the <tt>overrides</tt> settings applied.
40       */
41      public ExternalCacheSettings override(ExternalCacheSettings overrides) {
42          return new ExternalCacheSettings(
43                  ifPresent(overrides.getDefaultTtl(), getDefaultTtl()),
44                  ifPresent(overrides.getEntryCountHint(), getEntryCountHint()),
45                  ifPresent(overrides.getDataChangeRateHint(), getDataChangeRateHint()),
46                  ifPresent(overrides.getEntryGrowthRateHint(), getEntryGrowthRateHint()));
47      }
48  
49      /**
50       * Returns the default time-to-live for entries when added. The time-to-live is calculated on the time the
51       * entry was added or updated.
52       *
53       * @return the default time-to-live for entries when added.
54       */
55      public Optional<Duration> getDefaultTtl() {
56          return defaultTtl;
57      }
58  
59      /**
60       * Returns the hint for the expected number of entries
61       *
62       * @return the hint for the expected number of entries
63       */
64      public Optional<Integer> getEntryCountHint() {
65          return entryCountHint;
66      }
67  
68      /**
69       * Returns the hint for the expected change rate for data updates.
70       *
71       * @return the hint for the expected change rate for data updates.
72       */
73      public Optional<ChangeRate> getDataChangeRateHint() {
74          return dataChangeRateHint;
75      }
76  
77      /**
78       * Returns the hint for the expected change rate for entry additions.
79       *
80       * @return the hint for the expected change rate for entry additions.
81       */
82      public Optional<ChangeRate> getEntryGrowthRateHint() {
83          return entryGrowthRateHint;
84      }
85  }