001/*
002 * Unit Systems
003 * Copyright (c) 2005-2017, 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 java.util.Collection;
029import java.util.HashMap;
030import java.util.Map;
031
032import javax.measure.spi.SystemOfUnits;
033import javax.measure.spi.SystemOfUnitsService;
034
035import systems.uom.common.CGS;
036import systems.uom.common.Imperial;
037import systems.uom.common.USCustomary;
038import tec.uom.lib.common.function.IntPrioritySupplier;
039
040/**
041 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
042 * @version 0.7, March 4, 2017
043 */
044public class CommonSystemService implements SystemOfUnitsService,
045        IntPrioritySupplier {
046    static final int PRIO = 100;
047    private static final String DEFAULT_SYSTEM_NAME = "USCustomary";
048
049    private final Map<String, SystemOfUnits> souMap = new HashMap<>();
050    private final Map<String, String> aliases = new HashMap<>();
051
052    public CommonSystemService() {
053        souMap.put("Imperial", Imperial.getInstance());
054        souMap.put(DEFAULT_SYSTEM_NAME, USCustomary.getInstance());
055        souMap.put("CGS", CGS.getInstance());
056        aliases.put("US", DEFAULT_SYSTEM_NAME);
057        aliases.put("Centimetre–gram–second", "CGS");
058    }
059
060    public Collection<SystemOfUnits> getAvailableSystemsOfUnits() {
061        return souMap.values();
062    }
063
064    @Override
065    public SystemOfUnits getSystemOfUnits() {
066        return getSystemOfUnits(DEFAULT_SYSTEM_NAME); 
067        // We assume US Customary as the more common system here
068    }
069
070    @Override
071    public SystemOfUnits getSystemOfUnits(String name) {
072        String alias = aliases.get(name);
073        if (alias != null && alias.length() > 0) {
074            return souMap.get(alias);
075        }
076        return souMap.get(name);
077    }
078
079    @Override
080    public int getPriority() {
081        return PRIO;
082    }
083}