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.netty5.handler.codec.string;
17  
18  import io.netty5.buffer.BufferUtil;
19  import io.netty5.util.CharsetUtil;
20  import io.netty5.util.internal.StringUtil;
21  
22  import static java.util.Objects.requireNonNull;
23  
24  /**
25   * A class to represent line separators in different environments.
26   */
27  public final class LineSeparator {
28  
29      /**
30       * The default line separator in the current system.
31       */
32      public static final LineSeparator DEFAULT = new LineSeparator(StringUtil.NEWLINE);
33  
34      /**
35       * The Unix line separator(LF)
36       */
37      public static final LineSeparator UNIX = new LineSeparator("\n");
38  
39      /**
40       * The Windows line separator(CRLF)
41       */
42      public static final LineSeparator WINDOWS = new LineSeparator("\r\n");
43  
44      private final String value;
45  
46      /**
47       * Create {@link LineSeparator} with the specified {@code lineSeparator} string.
48       */
49      public LineSeparator(String lineSeparator) {
50          this.value = requireNonNull(lineSeparator, "lineSeparator");
51      }
52  
53      /**
54       * Return the string value of this line separator.
55       */
56      public String value() {
57          return value;
58      }
59  
60      @Override
61      public boolean equals(Object o) {
62          if (this == o) {
63              return true;
64          }
65          if (!(o instanceof LineSeparator)) {
66              return false;
67          }
68          LineSeparator that = (LineSeparator) o;
69          return value != null ? value.equals(that.value) : that.value == null;
70      }
71  
72      @Override
73      public int hashCode() {
74          return value != null ? value.hashCode() : 0;
75      }
76  
77      /**
78       * Return a <a href="https://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the line separator in UTF-8 encoding.
79       */
80      @Override
81      public String toString() {
82          return BufferUtil.hexDump(value.getBytes(CharsetUtil.UTF_8));
83      }
84  }