|
| 1 | +// SPDX-FileCopyrightText: 2022-present Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package io.atomix.client; |
| 6 | + |
| 7 | + |
| 8 | +import io.atomix.client.utils.ThreadContext; |
| 9 | +import io.grpc.Channel; |
| 10 | + |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.CompletionException; |
| 13 | + |
| 14 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 15 | + |
| 16 | +/** |
| 17 | + * Abstract builder for distributed primitives. |
| 18 | + * |
| 19 | + * @param <B> builder type |
| 20 | + * @param <P> primitive type |
| 21 | + */ |
| 22 | +public abstract class PrimitiveBuilder<B extends PrimitiveBuilder<B, P>, P extends SyncPrimitive> { |
| 23 | + protected final String primitiveName; |
| 24 | + protected boolean readOnly; |
| 25 | + protected final Channel serviceChannel; |
| 26 | + protected final ThreadContext threadContext; |
| 27 | + |
| 28 | + protected PrimitiveBuilder(String primitiveName, Channel serviceChannel, ThreadContext threadContext) { |
| 29 | + this.primitiveName = checkNotNull(primitiveName, |
| 30 | + "primitive name cannot be null"); |
| 31 | + this.serviceChannel = checkNotNull(serviceChannel, |
| 32 | + "primitive channel cannot be null"); |
| 33 | + this.threadContext = checkNotNull(threadContext, |
| 34 | + "thread context cannot be null"); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the primitive name. |
| 39 | + * |
| 40 | + * @return the primitive name |
| 41 | + */ |
| 42 | + protected String getPrimitiveName() { |
| 43 | + return primitiveName; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the service channel. |
| 48 | + * |
| 49 | + * @return the service channel |
| 50 | + */ |
| 51 | + protected Channel getServiceChannel() { |
| 52 | + return serviceChannel; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the thread context. |
| 57 | + * |
| 58 | + * @return the thread context |
| 59 | + */ |
| 60 | + protected ThreadContext getThreadContext() { |
| 61 | + return threadContext; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the primitive to read-only. |
| 66 | + * |
| 67 | + * @return the primitive builder |
| 68 | + */ |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + public B withReadOnly() { |
| 71 | + return withReadOnly(true); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sets whether the primitive is read-only. |
| 76 | + * |
| 77 | + * @param readOnly whether the primitive is read-only |
| 78 | + * @return the primitive builder |
| 79 | + */ |
| 80 | + @SuppressWarnings("unchecked") |
| 81 | + public B withReadOnly(boolean readOnly) { |
| 82 | + this.readOnly = readOnly; |
| 83 | + return (B) this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Builds a new instance of the primitive. |
| 88 | + * <p> |
| 89 | + * The returned instance will be distinct from all other instances of the same primitive on this node, with a |
| 90 | + * distinct session, ordering guarantees, memory, etc. |
| 91 | + * |
| 92 | + * @return a new instance of the primitive |
| 93 | + */ |
| 94 | + public P build() { |
| 95 | + try { |
| 96 | + return buildAsync().join(); |
| 97 | + } catch (Exception e) { |
| 98 | + if (e instanceof CompletionException && e.getCause() instanceof RuntimeException) { |
| 99 | + throw (RuntimeException) e.getCause(); |
| 100 | + } else { |
| 101 | + throw e; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Builds a new instance of the primitive asynchronously. |
| 108 | + * <p> |
| 109 | + * The returned instance will be distinct from all other instances of the same primitive on this node, with a |
| 110 | + * distinct session, ordering guarantees, memory, etc. |
| 111 | + * |
| 112 | + * @return asynchronous distributed primitive |
| 113 | + */ |
| 114 | + public abstract CompletableFuture<P> buildAsync(); |
| 115 | + |
| 116 | + /** |
| 117 | + * Gets or builds a singleton instance of the primitive. |
| 118 | + * <p> |
| 119 | + * The returned primitive will be shared by all {@code get()} calls for the named primitive. If no instance has yet |
| 120 | + * been constructed, the instance will be built from this builder's configuration. |
| 121 | + * |
| 122 | + * @return a singleton instance of the primitive |
| 123 | + */ |
| 124 | + public P get() { |
| 125 | + try { |
| 126 | + return getAsync().join(); |
| 127 | + } catch (Exception e) { |
| 128 | + if (e instanceof CompletionException && e.getCause() instanceof RuntimeException) { |
| 129 | + throw (RuntimeException) e.getCause(); |
| 130 | + } else { |
| 131 | + throw e; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Gets or builds a singleton instance of the primitive asynchronously. |
| 138 | + * <p> |
| 139 | + * The returned primitive will be shared by all {@code get()} calls for the named primitive. If no instance has yet |
| 140 | + * been constructed, the instance will be built from this builder's configuration. |
| 141 | + * |
| 142 | + * @return a singleton instance of the primitive |
| 143 | + */ |
| 144 | + public CompletableFuture<P> getAsync() { |
| 145 | + return null; |
| 146 | + } |
| 147 | +} |
0 commit comments