Merge from the pain train
[official-gcc.git] / libjava / gnu / java / nio / charset / Provider.java
blob1a5606813cb902e3f945ed2e85d10cf9c42d935d
1 /* Provider.java --
2 Copyright (C) 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.nio.charset;
40 import java.nio.charset.Charset;
41 import java.nio.charset.spi.CharsetProvider;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.Iterator;
46 /**
47 * Charset provider for the required charsets. Used by
48 * {@link Charset#charsetForName} and * {@link Charset#availableCharsets}.
50 * @author Jesse Rosenstock
51 * @author Robert Schuster (thebohemian@gmx.net)
52 * @see Charset
54 public final class Provider extends CharsetProvider
56 private static Provider singleton;
58 static
60 synchronized (Provider.class)
62 singleton = null;
66 /**
67 * Map from charset name to charset canonical name. The strings
68 * are all lower-case to allow case-insensitive retrieval of
69 * Charset instances.
71 private final HashMap canonicalNames;
73 /**
74 * Map from lower-case canonical name to Charset.
75 * TODO: We may want to use soft references. We would then need to keep
76 * track of the class name to regenerate the object.
78 private final HashMap charsets;
80 private Provider ()
82 canonicalNames = new HashMap ();
83 charsets = new HashMap ();
85 // US-ASCII aka ISO646-US
86 addCharset (new US_ASCII ());
88 // ISO-8859-1 aka ISO-LATIN-1
89 addCharset (new ISO_8859_1 ());
91 // UTF-8
92 addCharset (new UTF_8 ());
94 // UTF-16BE
95 addCharset (new UTF_16BE ());
97 // UTF-16LE
98 addCharset (new UTF_16LE ());
100 // UTF-16
101 addCharset (new UTF_16 ());
104 public Iterator charsets ()
106 return Collections.unmodifiableCollection (charsets.values ())
107 .iterator ();
111 * Returns a Charset instance by converting the given
112 * name to lower-case, looking up the canonical charset
113 * name and finally looking up the Charset with that name.
115 * <p>The lookup is therefore case-insensitive.</p>
117 * @returns The Charset having <code>charsetName</code>
118 * as its alias or null if no such Charset exist.
120 public Charset charsetForName (String charsetName)
122 return (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase()));
126 * Puts a Charset under its canonical name into the 'charsets' map.
127 * Then puts a mapping from all its alias names to the canonical name.
129 * <p>All names are converted to lower-case</p>.
131 * @param cs
133 private void addCharset (Charset cs)
135 String canonicalName = cs.name().toLowerCase();
136 charsets.put (canonicalName, cs);
138 /* Adds a mapping between the canonical name
139 * itself making a lookup using that name
140 * no special case.
142 canonicalNames.put(canonicalName, canonicalName);
144 for (Iterator i = cs.aliases ().iterator (); i.hasNext (); )
145 canonicalNames.put (((String) i.next()).toLowerCase(), canonicalName);
148 public static synchronized Provider provider ()
150 if (singleton == null)
151 singleton = new Provider ();
152 return singleton;