1   package com.atlassian.webtest.ui.keys;
2   
3   import com.google.common.base.Preconditions;
4   
5   import static com.google.common.base.Preconditions.checkNotNull;
6   
7   
8   /**
9    * <p>
10   * Character implementation of the {@link Key} interface.
11   * This class represents all keys that have corresponding 16-bit char representation in Java.
12   *
13   * <p>
14   * NOTE: as this class is based on a single 16-bit Java char, it only supports characters from
15   * the Basic Multilingual Plane (BMP) and <b>NOT</b> the <i>supplementary characters<i> as explained
16   * in the {@link Character} class documentation.
17   *
18   */
19  public class DefaultCharacterKey implements CharacterKey
20  {
21      private final String string;
22      private final char codeUnit;
23  
24      public DefaultCharacterKey(String string)
25      {
26          this.string = checkNotNull(string, "string");
27          Preconditions.checkArgument(string.length() == 1, "string should be of length 1");
28          this.codeUnit = string.charAt(0);
29      }
30  
31      public DefaultCharacterKey(char codeUnit)
32      {
33          this.codeUnit = codeUnit;
34          this.string = Character.toString(codeUnit);
35      }
36  
37      public String string()
38      {
39          return string;
40      }
41  
42      public char codeUnit()
43      {
44          return codeUnit;
45      }
46  
47      @Override
48      public String toString()
49      {
50          return string;
51      }
52  }