View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project 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 io.netty.handler.codec.smtp;
17  
18  import io.netty.util.internal.ObjectUtil;
19  import io.netty.util.internal.UnstableApi;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  /**
25   * Default {@link SmtpRequest} implementation.
26   */
27  @UnstableApi
28  public final class DefaultSmtpRequest implements SmtpRequest {
29  
30      private final SmtpCommand command;
31      private final List<CharSequence> parameters;
32  
33      /**
34       * Creates a new instance with the given command and no parameters.
35       */
36      public DefaultSmtpRequest(SmtpCommand command) {
37          this.command = ObjectUtil.checkNotNull(command, "command");
38          parameters = Collections.emptyList();
39      }
40  
41      /**
42       * Creates a new instance with the given command and parameters.
43       */
44      public DefaultSmtpRequest(SmtpCommand command, CharSequence... parameters) {
45          this.command = ObjectUtil.checkNotNull(command, "command");
46          SmtpUtils.validateSMTPParameters(parameters);
47          this.parameters = SmtpUtils.toUnmodifiableList(parameters);
48      }
49  
50      /**
51       * Creates a new instance with the given command and parameters.
52       */
53      public DefaultSmtpRequest(CharSequence command, CharSequence... parameters) {
54          this(SmtpCommand.valueOf(command), parameters);
55      }
56  
57      DefaultSmtpRequest(SmtpCommand command, List<CharSequence> parameters) {
58          this.command = ObjectUtil.checkNotNull(command, "command");
59          SmtpUtils.validateSMTPParameters(parameters);
60          this.parameters = parameters != null ?
61                  Collections.unmodifiableList(parameters) : Collections.<CharSequence>emptyList();
62      }
63  
64      @Override
65      public SmtpCommand command() {
66          return command;
67      }
68  
69      @Override
70      public List<CharSequence> parameters() {
71          return parameters;
72      }
73  
74      @Override
75      public int hashCode() {
76          return command.hashCode() * 31 + parameters.hashCode();
77      }
78  
79      @Override
80      public boolean equals(Object o) {
81          if (!(o instanceof DefaultSmtpRequest)) {
82              return false;
83          }
84  
85          if (o == this) {
86              return true;
87          }
88  
89          DefaultSmtpRequest other = (DefaultSmtpRequest) o;
90  
91          return command().equals(other.command()) &&
92                  parameters().equals(other.parameters());
93      }
94  
95      @Override
96      public String toString() {
97          return "DefaultSmtpRequest{" +
98                  "command=" + command +
99                  ", parameters=" + parameters +
100                 '}';
101     }
102 }