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 JvmCache}.
13 *
14 * @since 1.0
15 */
16 @PublicApi
17 public class JvmCacheSettings {
18 private final Optional<Integer> maxEntries;
19 private final Optional<Duration> defaultTtl;
20
21 JvmCacheSettings(Optional<Integer> maxEntries, Optional<Duration> defaultTtl) {
22 this.maxEntries = requireNonNull(maxEntries);
23 this.defaultTtl = requireNonNull(defaultTtl);
24 }
25
26 /**
27 * Returns a new {@link JvmCacheSettings} instance where the current settings
28 * are overridden with settings specified in <tt>overrides</tt>. Only properties
29 * in <tt>overrides</tt> that are present will be applied.
30 *
31 * @param overrides contains the settings to override
32 * @return a new {@link JvmCacheSettings} instance with the <tt>overrides</tt> settings applied.
33 */
34 public JvmCacheSettings override(JvmCacheSettings overrides) {
35 return new JvmCacheSettings(
36 ifPresent(overrides.getMaxEntries(), getMaxEntries()),
37 ifPresent(overrides.getDefaultTtl(), getDefaultTtl()));
38 }
39
40 /**
41 * Returns the maximum number of entries allowed.
42 *
43 * @return the maximum number of entries allowed.
44 */
45 public Optional<Integer> getMaxEntries() {
46 return maxEntries;
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 }