1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package java.nio.charset;
18 
19 import java.util.Collections;
20 import java.util.SortedMap;
21 import java.util.TreeMap;
22 
23 /**
24  * A minimal GWT emulation of {@link Charset}.
25  *
26  * @author Gregory Kick
27  */
28 public abstract class Charset implements Comparable<Charset> {
29   private static final Charset UTF_8 = new Charset("UTF-8") {};
30 
31   private static final SortedMap<String, Charset> AVAILABLE_CHARSETS =
32       new TreeMap<String, Charset>();
33   static {
UTF_8.name()34     AVAILABLE_CHARSETS.put(UTF_8.name(), UTF_8);
35   }
36 
availableCharsets()37   public static SortedMap<String, Charset> availableCharsets() {
38     return Collections.unmodifiableSortedMap(AVAILABLE_CHARSETS);
39   }
40 
forName(String charsetName)41   public static Charset forName(String charsetName) {
42     if (charsetName == null) {
43       throw new IllegalArgumentException("Null charset name");
44     }
45     int length = charsetName.length();
46     if (length == 0) {
47       throw new IllegalCharsetNameException(charsetName);
48     }
49     for (int i = 0; i < length; i++) {
50       char c = charsetName.charAt(i);
51       if ((c >= 'A' && c <= 'Z')
52           || (c >= 'a' && c <= 'z')
53           || (c >= '0' && c <= '9')
54           || (c == '-' && i != 0)
55           || (c == ':' && i != 0)
56           || (c == '_' && i != 0)
57           || (c == '.' && i != 0)) {
58         continue;
59       }
60       throw new IllegalCharsetNameException(charsetName);
61     }
62     Charset charset = AVAILABLE_CHARSETS.get(charsetName.toUpperCase());
63     if (charset != null) {
64       return charset;
65     }
66     throw new UnsupportedCharsetException(charsetName);
67   }
68 
69   private final String name;
70 
Charset(String name)71   private Charset(String name) {
72     this.name = name;
73   }
74 
name()75   public final String name() {
76     return name;
77   }
78 
compareTo(Charset that)79   public final int compareTo(Charset that) {
80     return this.name.compareToIgnoreCase(that.name);
81   }
82 
hashCode()83   public final int hashCode() {
84     return name.hashCode();
85   }
86 
equals(Object o)87   public final boolean equals(Object o) {
88     if (o == this) {
89       return true;
90     } else if (o instanceof Charset) {
91       Charset that = (Charset) o;
92       return this.name.equals(that.name);
93     } else {
94       return false;
95     }
96   }
97 
toString()98   public final String toString() {
99     return name;
100   }
101 }
102