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<>(values().length - 1, 1F);
47      
48      static {
49          for (QuoteCharacter each : values()) {
50              if (NONE != each) {
51                  BY_FIRST_CHAR.put(each.startDelimiter.charAt(0), each);
52              }
53          }
54      }
55      
56      private final String startDelimiter;
57      
58      private final String endDelimiter;
59      
60      /**
61       * Get quote character.
62       *
63       * @param value value to be get quote character
64       * @return value of quote character
65       */
66      public static QuoteCharacter getQuoteCharacter(final String value) {
67          return Strings.isNullOrEmpty(value) ? NONE : BY_FIRST_CHAR.getOrDefault(value.charAt(0), NONE);
68      }
69      
70      /**
71       * Wrap value with quote character.
72       *
73       * @param value value to be wrapped
74       * @return wrapped value
75       */
76      public String wrap(final String value) {
77          return startDelimiter + value + endDelimiter;
78      }
79      
80      /**
81       * Unwrap value with quote character.
82       *
83       * @param value value to be unwrapped
84       * @return unwrapped value
85       */
86      public String unwrap(final String value) {
87          return isWrapped(value) ? value.substring(startDelimiter.length(), value.length() - endDelimiter.length()) : value;
88      }
89      
90      /**
91       * Is wrapped by quote character.
92       *
93       * @param value value to be judged
94       * @return is wrapped or not
95       */
96      public boolean isWrapped(final String value) {
97          return value.startsWith(startDelimiter) && value.endsWith(endDelimiter);
98      }
99      
100     /**
101      * Unwrap text.
102      *
103      * @param text text to be unwrapped
104      * @return unwrapped text
105      */
106     public static String unwrapText(final String text) {
107         return QuoteCharacter.getQuoteCharacter(text).unwrap(text);
108     }
109     
110     /**
111      * Unwrap and trim text.
112      *
113      * @param text text to be unwrapped and trimmed
114      * @return unwrapped and trimmed test
115      */
116     // TODO Should use unwrap instead of this method after new rules defined in G4's property key and property key, which should include string but cannot permit blank on first and last of the value
117     // TODO @longtao
118     public static String unwrapAndTrimText(final String text) {
119         return unwrapText(text).trim();
120     }
121 }