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.common;
18  
19  import static com.linecorp.centraldogma.internal.Util.emailToUsername;
20  import static java.util.Objects.requireNonNull;
21  
22  import com.fasterxml.jackson.annotation.JsonCreator;
23  import com.fasterxml.jackson.annotation.JsonProperty;
24  
25  import com.linecorp.centraldogma.internal.Util;
26  
27  /**
28   * An author of a {@link Commit}.
29   */
30  public class Author {
31  
32      /**
33       * The system author.
34       */
35      public static final Author SYSTEM = new Author("System", "system@localhost.localdomain");
36  
37      /**
38       * The default author which is used when security is disabled.
39       */
40      public static final Author DEFAULT = new Author("User", "user@localhost.localdomain");
41  
42      /**
43       * An unknown author.
44       */
45      public static final Author UNKNOWN = new Author("Unknown", "nobody@no.where");
46  
47      /**
48       * Create a new {@link Author} with the {@code email}.
49       * The {@link #name()} will be set to the username of the {@code email}.
50       */
51      public static Author ofEmail(String email) {
52          return new Author(emailToUsername(email, "email"), email);
53      }
54  
55      private final String name;
56      private final String email;
57  
58      /**
59       * Creates a new instance with the specified e-mail address.
60       *
61       * @deprecated Use {@link #ofEmail(String)}.
62       */
63      @Deprecated
64      public Author(String email) {
65          this(email, email);
66      }
67  
68      /**
69       * Creates a new instance with the specified name and e-mail address.
70       */
71      @JsonCreator
72      public Author(@JsonProperty("name") String name,
73                    @JsonProperty("email") String email) {
74  
75          this.name = requireNonNull(name, "name");
76          this.email = Util.validateEmailAddress(email, "email");
77      }
78  
79      /**
80       * Returns the name of the author.
81       */
82      @JsonProperty
83      public String name() {
84          return name;
85      }
86  
87      /**
88       * Returns the e-mail address of the author.
89       */
90      @JsonProperty
91      public String email() {
92          return email;
93      }
94  
95      @Override
96      public int hashCode() {
97          return email.hashCode();
98      }
99  
100     @Override
101     public boolean equals(Object o) {
102         if (this == o) {
103             return true;
104         }
105         if (!(o instanceof Author)) {
106             return false;
107         }
108         final Author author = (Author)o;
109         return email.equals(author.email);
110     }
111 
112     @Override
113     public String toString() {
114         final StringBuilder buf = new StringBuilder();
115 
116         buf.append(Util.simpleTypeName(this));
117         buf.append("[\"");
118         buf.append(name);
119         buf.append("\" <");
120         buf.append(email);
121         buf.append(">]");
122 
123         return buf.toString();
124     }
125 }