|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 The original author or authors |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Apache License v2.0 which accompanies this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * |
| 11 | + * The Apache License v2.0 is available at |
| 12 | + * http://www.opensource.org/licenses/apache2.0.php |
| 13 | + * |
| 14 | + * You may elect to redistribute this code under either of these licenses. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.engagingspaces.vertx.dataloader; |
| 18 | + |
| 19 | +import io.vertx.core.json.JsonObject; |
| 20 | + |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +/** |
| 25 | + * Configuration options for {@link DataLoader} instances. |
| 26 | + * |
| 27 | + * @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a> |
| 28 | + */ |
| 29 | +public class DataLoaderOptions { |
| 30 | + |
| 31 | + private boolean batchingEnabled; |
| 32 | + private boolean cachingEnabled; |
| 33 | + private CacheKey cacheKeyFunction; |
| 34 | + private CacheMap cacheMap; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new data loader options with default settings. |
| 38 | + */ |
| 39 | + public DataLoaderOptions() { |
| 40 | + batchingEnabled = true; |
| 41 | + cachingEnabled = true; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Clones the provided data loader options. |
| 46 | + * |
| 47 | + * @param other the other options instance |
| 48 | + */ |
| 49 | + public DataLoaderOptions(DataLoaderOptions other) { |
| 50 | + Objects.requireNonNull(other, "Other data loader options cannot be null"); |
| 51 | + this.batchingEnabled = other.batchingEnabled; |
| 52 | + this.cachingEnabled = other.cachingEnabled; |
| 53 | + this.cacheKeyFunction = other.cacheKeyFunction; |
| 54 | + this.cacheMap = other.cacheMap; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new data loader options with values provided as JSON. |
| 59 | + * <p> |
| 60 | + * Note that only json-serializable options can be set with this constructor. Others, |
| 61 | + * like {@link DataLoaderOptions#cacheKeyFunction} must be set manually after creation. |
| 62 | + * <p> |
| 63 | + * Note also that this makes it incompatible with true Vert.x data objects, so beware if you use it that way. |
| 64 | + * |
| 65 | + * @param json the serialized data loader options to set |
| 66 | + */ |
| 67 | + public DataLoaderOptions(JsonObject json) { |
| 68 | + Objects.requireNonNull(json, "Json cannot be null"); |
| 69 | + this.batchingEnabled = json.getBoolean("batchingEnabled"); |
| 70 | + this.batchingEnabled = json.getBoolean("cachingEnabled"); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Option that determines whether to use batching (the default), or not. |
| 75 | + * |
| 76 | + * @return {@code true} when batching is enabled, {@code false} otherwise |
| 77 | + */ |
| 78 | + public boolean batchingEnabled() { |
| 79 | + return batchingEnabled; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sets the option that determines whether batch loading is enabled. |
| 84 | + * |
| 85 | + * @param batchingEnabled {@code true} to enable batch loading, {@code false} otherwise |
| 86 | + * @return the data loader options for fluent coding |
| 87 | + */ |
| 88 | + public DataLoaderOptions setBatchingEnabled(boolean batchingEnabled) { |
| 89 | + this.batchingEnabled = batchingEnabled; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Option that determines whether to use caching of futures (the default), or not. |
| 95 | + * |
| 96 | + * @return {@code true} when caching is enabled, {@code false} otherwise |
| 97 | + */ |
| 98 | + public boolean cachingEnabled() { |
| 99 | + return cachingEnabled; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Sets the option that determines whether caching is enabled. |
| 104 | + * |
| 105 | + * @param cachingEnabled {@code true} to enable caching, {@code false} otherwise |
| 106 | + * @return the data loader options for fluent coding |
| 107 | + */ |
| 108 | + public DataLoaderOptions setCachingEnabled(boolean cachingEnabled) { |
| 109 | + this.cachingEnabled = cachingEnabled; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Gets an (optional) function to invoke for creation of the cache key, if caching is enabled. |
| 115 | + * <p> |
| 116 | + * If missing the cache key defaults to the {@code key} type parameter of the data loader of type {@code K}. |
| 117 | + * |
| 118 | + * @return an optional with the function, or empty optional |
| 119 | + */ |
| 120 | + public Optional<CacheKey> cacheKeyFunction() { |
| 121 | + return Optional.ofNullable(cacheKeyFunction); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Sets the function to use for creating the cache key, if caching is enabled. |
| 126 | + * |
| 127 | + * @param cacheKeyFunction the cache key function to use |
| 128 | + * @return the data loader options for fluent coding |
| 129 | + */ |
| 130 | + public DataLoaderOptions setCacheKeyFunction(CacheKey cacheKeyFunction) { |
| 131 | + this.cacheKeyFunction = cacheKeyFunction; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Gets the (optional) cache map implementation that is used for caching, if caching is enabled. |
| 137 | + * <p> |
| 138 | + * If missing a standard {@link java.util.LinkedHashMap} will be used as the cache implementation. |
| 139 | + * |
| 140 | + * @return an optional with the cache map instance, or empty |
| 141 | + */ |
| 142 | + public Optional<CacheMap> cacheMap() { |
| 143 | + return Optional.ofNullable(cacheMap); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Sets the cache map implementation to use for caching, if caching is enabled. |
| 148 | + * |
| 149 | + * @param cacheMap the cache map instance |
| 150 | + * @return the data loader options for fluent coding |
| 151 | + */ |
| 152 | + public DataLoaderOptions setCacheMap(CacheMap cacheMap) { |
| 153 | + this.cacheMap = cacheMap; |
| 154 | + return this; |
| 155 | + } |
| 156 | +} |
0 commit comments