001/*
002 *  Unit-API - Units of Measurement API for Java
003 *  Copyright (c) 2005-2016, Jean-Marie Dautelle, Werner Keil, V2COM.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
008 *
009 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
010 *
011 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
012 *
013 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
014 *
015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
016 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
017 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
018 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
019 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
020 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
021 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
022 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
023 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
024 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026package systems.uom.common.internal;
027
028import static systems.uom.common.internal.CommonSystemService.PRIO;
029
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.Map;
033
034import javax.annotation.Priority;
035import javax.measure.spi.SystemOfUnits;
036import javax.measure.spi.SystemOfUnitsService;
037
038import systems.uom.common.Imperial;
039import systems.uom.common.NonSI;
040import systems.uom.common.USCustomary;
041import tec.uom.lib.common.function.IntPrioritySupplier;
042
043/**
044 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
045 * @version 0.4, June 21, 2016
046 */
047@Priority(PRIO)
048public class CommonSystemService implements SystemOfUnitsService,
049        IntPrioritySupplier {
050    static final int PRIO = 100;
051    private static final String DEFAULT_SYSTEM_NAME = "USCustomary";
052
053    private final Map<String, SystemOfUnits> souMap = new HashMap<String, SystemOfUnits>();
054    private final Map<String, String> aliases = new HashMap<String, String>();
055
056    public CommonSystemService() {
057        souMap.put("Imperial", Imperial.getInstance());
058        souMap.put(DEFAULT_SYSTEM_NAME, USCustomary.getInstance());
059        souMap.put("NonSI", NonSI.getInstance());
060        aliases.put("US", DEFAULT_SYSTEM_NAME);
061        aliases.put("Non-SI", "NonSI");
062    }
063
064    public Collection<SystemOfUnits> getAvailableSystemsOfUnits() {
065        return souMap.values();
066    }
067
068    @Override
069    public SystemOfUnits getSystemOfUnits() {
070        return getSystemOfUnits(DEFAULT_SYSTEM_NAME); // We assume US Customary
071                                                      // as the more
072        // common system here
073    }
074
075    @Override
076    public SystemOfUnits getSystemOfUnits(String name) {
077        String alias = aliases.get(name);
078        if (alias != null && alias.length() > 0) {
079            return souMap.get(alias);
080        }
081        return souMap.get(name);
082    }
083
084    @Override
085    public int getPriority() {
086        return PRIO;
087    }
088}