CurrencyServiceShim.java

// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
 *******************************************************************************
 * Copyright (C) 2001-2011, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
package com.ibm.icu.util;

import com.ibm.icu.impl.ICULocaleService;
import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.ICUService;
import com.ibm.icu.impl.ICUService.Factory;
import java.util.Locale;

/**
 * This is a package-access implementation of registration for currency. The shim is instantiated by
 * reflection in Currency, all dependencies on ICUService are located in this file. This structure
 * is to allow ICU4J to be built without service registration support.
 */
final class CurrencyServiceShim extends Currency.ServiceShim {

    @Override
    Locale[] getAvailableLocales() {
        if (service.isDefault()) {
            return ICUResourceBundle.getAvailableLocales();
        }
        return service.getAvailableLocales();
    }

    @Override
    ULocale[] getAvailableULocales() {
        if (service.isDefault()) {
            return ICUResourceBundle.getAvailableULocales();
        }
        return service.getAvailableULocales();
    }

    @Override
    Currency createInstance(ULocale loc) {
        // TODO: convert to ULocale when service switches over

        if (service.isDefault()) {
            return Currency.createCurrency(loc);
        }
        Currency curr = (Currency) service.get(loc);
        return curr;
    }

    @Override
    Object registerInstance(Currency currency, ULocale locale) {
        return service.registerObject(currency, locale);
    }

    @Override
    boolean unregister(Object registryKey) {
        return service.unregisterFactory((Factory) registryKey);
    }

    private static class CFService extends ICULocaleService {
        CFService() {
            super("Currency");

            class CurrencyFactory extends ICUResourceBundleFactory {
                @Override
                protected Object handleCreate(ULocale loc, int kind, ICUService srvc) {
                    return Currency.createCurrency(loc);
                }
            }

            registerFactory(new CurrencyFactory());
            markDefault();
        }
    }

    static final ICULocaleService service = new CFService();
}