1   /*
2    * Copyright 2025 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.metadata;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import javax.annotation.Nullable;
22  
23  import com.google.common.base.MoreObjects;
24  import com.google.common.base.MoreObjects.ToStringHelper;
25  import com.google.common.base.Objects;
26  
27  import com.linecorp.centraldogma.internal.Util;
28  
29  /**
30   * An abstract base class for {@link AppIdentity} implementations.
31   */
32  abstract class AbstractAppIdentity implements AppIdentity {
33  
34      private final String appId;
35      private final AppIdentityType type;
36      private final boolean isSystemAdmin;
37      private final boolean allowGuestAccess;
38      private final UserAndTimestamp creation;
39      @Nullable
40      private final UserAndTimestamp deactivation;
41      @Nullable
42      private final UserAndTimestamp deletion;
43  
44      AbstractAppIdentity(String appId, AppIdentityType type, boolean isSystemAdmin,
45                          boolean allowGuestAccess, UserAndTimestamp creation,
46                          @Nullable UserAndTimestamp deactivation, @Nullable UserAndTimestamp deletion) {
47          this.appId = Util.validateFileName(appId, "appId");
48          this.type = requireNonNull(type, "type");
49          this.isSystemAdmin = isSystemAdmin;
50          this.allowGuestAccess = allowGuestAccess;
51          this.creation = requireNonNull(creation, "creation");
52          this.deactivation = deactivation;
53          this.deletion = deletion;
54      }
55  
56      @Override
57      public String id() {
58          return appId;
59      }
60  
61      @Override
62      public String appId() {
63          return appId;
64      }
65  
66      @Override
67      public AppIdentityType type() {
68          return type;
69      }
70  
71      @Override
72      public boolean isSystemAdmin() {
73          return isSystemAdmin;
74      }
75  
76      @Override
77      public boolean allowGuestAccess() {
78          return allowGuestAccess;
79      }
80  
81      @Override
82      public UserAndTimestamp creation() {
83          return creation;
84      }
85  
86      @Nullable
87      @Override
88      public UserAndTimestamp deactivation() {
89          return deactivation;
90      }
91  
92      @Nullable
93      @Override
94      public UserAndTimestamp deletion() {
95          return deletion;
96      }
97  
98      @Override
99      public boolean equals(Object o) {
100         if (this == o) {
101             return true;
102         }
103 
104         if (o == null || getClass() != o.getClass()) {
105             return false;
106         }
107 
108         final AbstractAppIdentity that = (AbstractAppIdentity) o;
109         return appId.equals(that.appId) &&
110                type == that.type &&
111                isSystemAdmin == that.isSystemAdmin &&
112                allowGuestAccess == that.allowGuestAccess &&
113                creation.equals(that.creation) &&
114                Objects.equal(deactivation, that.deactivation) &&
115                Objects.equal(deletion, that.deletion);
116     }
117 
118     @Override
119     public int hashCode() {
120         return Objects.hashCode(appId, type, isSystemAdmin, allowGuestAccess, creation, deactivation, deletion);
121     }
122 
123     @Override
124     public final String toString() {
125         final ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues()
126                                                  .add("appId", id())
127                                                  .add("type", type())
128                                                  .add("isSystemAdmin", isSystemAdmin())
129                                                  .add("allowGuestAccess", allowGuestAccess())
130                                                  .add("creation", creation())
131                                                  .add("deactivation", deactivation())
132                                                  .add("deletion", deletion());
133         addProperties(helper);
134         return helper.toString();
135     }
136 
137     abstract void addProperties(ToStringHelper helper);
138 }