001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2020, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification,
008 * are permitted provided that the following conditions are met:
009 *
010 * 1. Redistributions of source code must retain the above copyright notice,
011 *    this list of conditions and the following disclaimer.
012 *
013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
014 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
015 *
016 * 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products
017 *    derived from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package tech.units.indriya.format;
031
032import static tech.units.indriya.format.FormatBehavior.LOCALE_SENSITIVE;
033
034import java.text.NumberFormat;
035import java.util.HashMap;
036import java.util.Locale;
037import java.util.Map;
038import java.util.Objects;
039import java.util.Set;
040
041import javax.measure.format.QuantityFormat;
042import javax.measure.format.UnitFormat;
043import javax.measure.spi.FormatService;
044
045import tech.units.indriya.format.SimpleUnitFormat.Flavor;
046import tech.uom.lib.common.function.IntPrioritySupplier;
047
048/**
049 * Default format service.
050 *
051 * @author Werner Keil
052 * @version 2.1, November 16, 2020
053 * @since 2.0
054 */
055public class DefaultFormatService implements FormatService, IntPrioritySupplier {
056  private static final int PRIO = 1000;
057        
058  private static final String DEFAULT_QUANTITY_FORMAT_NAME = "Simple";
059  
060  private static final String DEFAULT_UNIT_FORMAT_NAME = Flavor.Default.name();
061
062  private final Map<String, QuantityFormat> quantityFormats = new HashMap<>();
063  
064  private final Map<String, UnitFormat> unitFormats = new HashMap<>();
065  
066  /**
067   * Holds the default format instance (EBNFUnitFormat).
068   */
069  private static final NumberDelimiterQuantityFormat EBNF_QUANTITY_FORMAT = new NumberDelimiterQuantityFormat.Builder()
070      .setNumberFormat(NumberFormat.getInstance(Locale.ROOT))
071      .setUnitFormat(EBNFUnitFormat.getInstance())
072      .build();
073  
074  public DefaultFormatService() {
075        unitFormats.put(DEFAULT_UNIT_FORMAT_NAME, SimpleUnitFormat.getInstance());
076        unitFormats.put(Flavor.ASCII.name(), SimpleUnitFormat.getInstance(Flavor.ASCII));
077        unitFormats.put("EBNF", EBNFUnitFormat.getInstance());
078        unitFormats.put("Local", LocalUnitFormat.getInstance());
079
080    quantityFormats.put(DEFAULT_QUANTITY_FORMAT_NAME, SimpleQuantityFormat.getInstance());
081    quantityFormats.put("NumberDelimiter", NumberDelimiterQuantityFormat.getInstance());
082    quantityFormats.put("EBNF", EBNF_QUANTITY_FORMAT);
083    quantityFormats.put("Local", NumberDelimiterQuantityFormat.getInstance(LOCALE_SENSITIVE));
084  }
085
086  @Override
087  public QuantityFormat getQuantityFormat(String name) {
088    return quantityFormats.get(name);
089  }
090
091  @Override
092  public QuantityFormat getQuantityFormat() {
093    return getQuantityFormat(DEFAULT_QUANTITY_FORMAT_NAME);
094  }
095
096  @Override
097  public Set<String> getAvailableFormatNames(FormatType type) {
098    switch (type) {
099      case QUANTITY_FORMAT:
100        return quantityFormats.keySet();
101      default:
102        return unitFormats.keySet();
103    }
104  }   
105
106  /*
107   * (non-Javadoc)
108   * 
109   * @see UnitFormatService#getUnitFormat(String)
110   */
111  @Override
112  public UnitFormat getUnitFormat(String formatName) {
113    Objects.requireNonNull(formatName, "Format name required");
114    return unitFormats.get(formatName);
115  }
116
117  /*
118   * (non-Javadoc)
119   * 
120   * @see UnitFormatService#getUnitFormat()
121   */
122  @Override
123  public UnitFormat getUnitFormat() {
124    return getUnitFormat(DEFAULT_UNIT_FORMAT_NAME);
125  }
126
127  @Override
128  public int getPriority() {
129    return PRIO;
130  }
131
132  @Override
133  public UnitFormat getUnitFormat(String name, String variant) {
134        return getUnitFormat(name);
135  }
136}