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.client;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Objects;
22  
23  import javax.annotation.Nullable;
24  
25  import com.google.common.base.MoreObjects;
26  
27  import com.linecorp.centraldogma.common.Revision;
28  
29  /**
30   * An immutable holder of the latest known value and its {@link Revision} retrieved by a {@link Watcher}.
31   *
32   * @param <U> the value type
33   */
34  public final class Latest<U> {
35  
36      private final Revision revision;
37      @Nullable
38      private final U value;
39  
40      /**
41       * Creates a new instance with the specified {@link Revision} and value.
42       */
43      public Latest(Revision revision, @Nullable U value) {
44          this.revision = requireNonNull(revision, "revision");
45          this.value = value;
46      }
47  
48      /**
49       * Returns the {@link Revision} of the latest known value.
50       */
51      public Revision revision() {
52          return revision;
53      }
54  
55      /**
56       * Returns the latest known value.
57       */
58      @Nullable
59      public U value() {
60          return value;
61      }
62  
63      @Override
64      public boolean equals(Object o) {
65          if (this == o) {
66              return true;
67          }
68          if (!(o instanceof Latest<?>)) {
69              return false;
70          }
71          final Latest<?> that = (Latest<?>) o;
72          return revision.equals(that.revision) && Objects.equals(value, that.value);
73      }
74  
75      @Override
76      public int hashCode() {
77          return revision.hashCode() * 31 + Objects.hashCode(value);
78      }
79  
80      @Override
81      public String toString() {
82          return MoreObjects.toStringHelper(this)
83                            .add("revision", revision)
84                            .add("value", value)
85                            .toString();
86      }
87  }