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 com.atlassian.vcache.SettingsUtils.ifPresent;
10  import static java.util.Objects.requireNonNull;
11  
12  /**
13   * An immutable representation of settings for an {@link JvmCache}.
14   *
15   * @since 1.0
16   */
17  @PublicApi
18  public class JvmCacheSettings
19  {
20      private final Optional<Integer> maxEntries;
21      private final Optional<Duration> defaultTtl;
22  
23      JvmCacheSettings(Optional<Integer> maxEntries, Optional<Duration> defaultTtl)
24      {
25          this.maxEntries = requireNonNull(maxEntries);
26          this.defaultTtl = requireNonNull(defaultTtl);
27      }
28  
29      /**
30       * Returns a new {@link JvmCacheSettings} instance where the current settings
31       * are overridden with settings specified in <tt>overrides</tt>. Only properties
32       * in <tt>overrides</tt> that are present will be applied.
33       * @param overrides contains the settings to override
34       * @return a new {@link JvmCacheSettings} instance with the <tt>overrides</tt> settings applied.
35       */
36      @Nonnull
37      public JvmCacheSettings override(JvmCacheSettings overrides)
38      {
39          return new JvmCacheSettings(
40                  ifPresent(overrides.getMaxEntries(), getMaxEntries()),
41                  ifPresent(overrides.getDefaultTtl(), getDefaultTtl()));
42      }
43  
44      /**
45       * Returns the maximum number of entries allowed.
46       * @return the maximum number of entries allowed.
47       */
48      @Nonnull
49      public Optional<Integer> getMaxEntries()
50      {
51          return maxEntries;
52      }
53  
54      /**
55       * Returns the default time-to-live for entries when added.
56       * @return the default time-to-live for entries when added.
57       */
58      @Nonnull
59      public Optional<Duration> getDefaultTtl()
60      {
61          return defaultTtl;
62      }
63  }