1/*
2 * Copyright (C) 2014 The Android Open Source Project
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 */
16package libcore.java.nio.charset;
17
18import java.nio.charset.Charset;
19import java.nio.charset.spi.CharsetProvider;
20import java.util.Collections;
21import java.util.Iterator;
22
23/**
24 * This class is registered as a charset provider by the META-INF in the libcore
25 * tests jar. Since there isn't any convenient API to dynamically register and de-register
26 * charset-providers, this class allows tests to plug in a delegate that lives for the
27 * duration of the test.
28 */
29public final class SettableCharsetProvider extends CharsetProvider {
30    private static CharsetProvider delegate;
31
32    public static void setDelegate(CharsetProvider cp) {
33        delegate = cp;
34    }
35
36    public static void clearDelegate() {
37        delegate = null;
38    }
39
40    @Override
41    public Iterator<Charset> charsets() {
42        if (delegate != null) {
43            return delegate.charsets();
44        }
45
46        return Collections.emptyIterator();
47    }
48
49    @Override
50    public Charset charsetForName(String charsetName) {
51        if (delegate != null) {
52            return delegate.charsetForName(charsetName);
53        }
54
55        return null;
56    }
57}
58