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  
18  package org.apache.shardingsphere.infra.database.core.metadata.database.enums;
19  
20  import com.google.common.base.Strings;
21  import lombok.Getter;
22  import lombok.RequiredArgsConstructor;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * Quote character.
29   */
30  @RequiredArgsConstructor
31  @Getter
32  public enum QuoteCharacter {
33      
34      BACK_QUOTE("`", "`"),
35      
36      SINGLE_QUOTE("'", "'"),
37      
38      QUOTE("\"", "\""),
39      
40      BRACKETS("[", "]"),
41      
42      PARENTHESES("(", ")"),
43      
44      NONE("", "");
45      
46      private static final Map<Character, QuoteCharacter> BY_FIRST_CHAR = new HashMap<>(QuoteCharacter.values().length - 1, 1L);
47      
48      static {
49          for (QuoteCharacter each : values()) {
50              if (NONE == each) {
51                  continue;
52              }
53              BY_FIRST_CHAR.put(each.startDelimiter.charAt(0), each);
54          }
55      }
56      
57      private final String startDelimiter;
58      
59      private final String endDelimiter;
60      
61      /**
62       * Get quote character.
63       * 
64       * @param value value to be get quote character
65       * @return value of quote character
66       */
67      public static QuoteCharacter getQuoteCharacter(final String value) {
68          if (Strings.isNullOrEmpty(value)) {
69              return NONE;
70          }
71          return BY_FIRST_CHAR.getOrDefault(value.charAt(0), NONE);
72      }
73      
74      /**
75       * Wrap value with quote character.
76       * 
77       * @param value value to be wrapped
78       * @return wrapped value
79       */
80      public String wrap(final String value) {
81          return startDelimiter + value + endDelimiter;
82      }
83      
84      /**
85       * Unwrap value with quote character.
86       *
87       * @param value value to be unwrapped
88       * @return unwrapped value
89       */
90      public String unwrap(final String value) {
91          return isWrapped(value) ? value.substring(startDelimiter.length(), value.length() - endDelimiter.length()) : value;
92      }
93      
94      /**
95       * Is wrapped by quote character.
96       * 
97       * @param value value to be judged
98       * @return is wrapped or not
99       */
100     public boolean isWrapped(final String value) {
101         return value.startsWith(startDelimiter) && value.endsWith(endDelimiter);
102     }
103 }