View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.util;
18  
19  /**
20   * <em>Consider this class private.</em>
21   */
22  public final class Chars {
23  
24      /** Carriage Return. */
25      public static final char CR = '\r';
26  
27      /** Double Quote. */
28      public static final char DQUOTE = '\"';
29  
30      /** Equals '='. */
31      public static final char EQ = '=';
32  
33      /** Line Feed. */
34      public static final char LF = '\n';
35  
36      /** Single Quote [']. */
37      public static final char QUOTE = '\'';
38  
39      /** Space. */
40      public static final char SPACE = ' ';
41  
42      /** Tab. */
43      public static final char TAB = '\t';
44  
45      /**
46       * Converts a digit into an uppercase hexadecimal character or the null character if invalid.
47       *
48       * @param digit a number 0 - 15
49       * @return the hex character for that digit or '\0' if invalid
50       */
51      public static char getUpperCaseHex(final int digit) {
52          if (digit < 0 || digit >= 16) {
53              return '\0';
54          }
55          return digit < 10 ? getNumericalDigit(digit) : getUpperCaseAlphaDigit(digit);
56      }
57  
58      /**
59       * Converts a digit into an lowercase hexadecimal character or the null character if invalid.
60       *
61       * @param digit a number 0 - 15
62       * @return the hex character for that digit or '\0' if invalid
63       */
64      public static char getLowerCaseHex(final int digit) {
65          if (digit < 0 || digit >= 16) {
66              return '\0';
67          }
68          return digit < 10 ? getNumericalDigit(digit) : getLowerCaseAlphaDigit(digit);
69      }
70  
71      private static char getNumericalDigit(final int digit) {
72          return (char) ('0' + digit);
73      }
74  
75      private static char getUpperCaseAlphaDigit(final int digit) {
76          return (char) ('A' + digit - 10);
77      }
78  
79      private static char getLowerCaseAlphaDigit(final int digit) {
80          return (char) ('a' + digit - 10);
81      }
82  
83      private Chars() {
84      }
85  }