1   /*
2    * Copyright 2020 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  package com.linecorp.centraldogma.server.internal.replication;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.concurrent.Executor;
21  
22  import org.apache.curator.framework.recipes.shared.SharedCountListener;
23  import org.apache.curator.framework.recipes.shared.SharedCountReader;
24  import org.apache.curator.framework.recipes.shared.VersionedValue;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  final class SettableSharedCount implements SharedCountReader {
29  
30      private static final Logger logger = LoggerFactory.getLogger(SettableSharedCount.class);
31  
32      private final List<SharedCountListener> listeners;
33      private int count;
34  
35      SettableSharedCount(int count) {
36          this.count = count;
37          listeners = new ArrayList<>();
38      }
39  
40      void setCount(int count) {
41          this.count = count;
42          for (SharedCountListener listener : listeners) {
43              try {
44                  listener.countHasChanged(this, count);
45              } catch (Exception e) {
46                  logger.warn("Unexpected exception caught while notifying {}", listener, e);
47              }
48          }
49      }
50  
51      @Override
52      public int getCount() {
53          return count;
54      }
55  
56      @Override
57      public VersionedValue<Integer> getVersionedValue() {
58          throw new UnsupportedOperationException();
59      }
60  
61      @Override
62      public void addListener(SharedCountListener listener) {
63          listeners.add(listener);
64      }
65  
66      @Override
67      public void addListener(SharedCountListener listener, Executor executor) {
68          throw new UnsupportedOperationException();
69      }
70  
71      @Override
72      public void removeListener(SharedCountListener listener) {
73          throw new UnsupportedOperationException();
74      }
75  }