1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.command;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.concurrent.CompletableFuture;
22  
23  import com.linecorp.centraldogma.internal.Util;
24  import com.linecorp.centraldogma.server.QuotaConfig;
25  
26  /**
27   * A {@link CommandExecutor} which forwards all its method calls to another {@link CommandExecutor}.
28   */
29  public class ForwardingCommandExecutor implements CommandExecutor {
30  
31      private final CommandExecutor delegate;
32  
33      protected ForwardingCommandExecutor(CommandExecutor delegate) {
34          this.delegate = requireNonNull(delegate, "delegate");
35      }
36  
37      protected final <T extends CommandExecutor> T delegate() {
38          return Util.unsafeCast(delegate);
39      }
40  
41      @Override
42      public int replicaId() {
43          return delegate().replicaId();
44      }
45  
46      @Override
47      public boolean isStarted() {
48          return delegate().isStarted();
49      }
50  
51      @Override
52      public CompletableFuture<Void> start() {
53          return delegate().start();
54      }
55  
56      @Override
57      public CompletableFuture<Void> stop() {
58          return delegate().stop();
59      }
60  
61      @Override
62      public boolean isWritable() {
63          return delegate().isWritable();
64      }
65  
66      @Override
67      public void setWritable(boolean writable) {
68          delegate().setWritable(writable);
69      }
70  
71      @Override
72      public void setWriteQuota(String projectName, String repoName, QuotaConfig writeQuota) {
73          delegate().setWriteQuota(projectName, repoName, writeQuota);
74      }
75  
76      @Override
77      public <T> CompletableFuture<T> execute(Command<T> command) {
78          return delegate().execute(command);
79      }
80  
81      @Override
82      public CommandExecutorStatusManager statusManager() {
83          return delegate().statusManager();
84      }
85  
86      @Override
87      public String toString() {
88          return getClass().getSimpleName() + '(' + delegate() + ')';
89      }
90  }