• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.tool;
2 
3 import java.io.IOException;
4 import java.util.Map.Entry;
5 
6 import org.unicode.cldr.util.CldrUtility;
7 import org.unicode.cldr.util.Rational;
8 import org.unicode.cldr.util.Rational.FormatStyle;
9 import org.unicode.cldr.util.UnitConverter;
10 import org.unicode.cldr.util.UnitConverter.TargetInfo;
11 
12 import com.google.common.base.Joiner;
13 
14 public class ChartUnitConversions extends Chart {
15 
16     public static final String QUANTITY_MSG = "The units are grouped and ordered by Quantity (which are based on the NIST quantities, see "
17         + "<a href='https://www.nist.gov/pml/special-publication-811' target='nist811'>NIST 811</a>). Note that the quantities are informative.";
18     public static final String RATIONAL_MSG = "Each numeric value is an exact rational. The format is a terminating decimal where possible; "
19         + "otherwise a repeating decimal if possible (where ˙ marks the start of the <a href='https://en.wikipedia.org/wiki/Repeating_decimal' target='wiki'>reptend</a>); "
20         + "otherwise a <a href='https://en.wikipedia.org/wiki/Rational_number' target='wiki'>rational number</a> (of the form <i>numerator/denominator</i>).";
21     public static final String SPEC_GENERAL_MSG = "The " + ldmlSpecLink("/tr35-general.html#Contents")
22     + " should be consulted for more details, such as how to handle complex units (such as foot-per-minute) by converting the elements";
23 
main(String[] args)24     public static void main(String[] args) {
25         new ChartUnitConversions().writeChart(null);
26     }
27 
28     @Override
getDirectory()29     public String getDirectory() {
30         return FormattedFileWriter.CHART_TARGET_DIR;
31     }
32 
33     @Override
getTitle()34     public String getTitle() {
35         return "Unit Conversions";
36     }
37 
38     @Override
getExplanation()39     public String getExplanation() {
40         return "<p>Unit Conversions provide conversions for units, such as meter ⟹ foot, "
41             + "so that a source units can be converted into what is needed for localized "
42             + "<a href='unit_preferences.html' target='unit_preferences'>Unit Preferences</a>. "
43             + "There are many possible units, and additional units and conversions will be added in future releases.</p>"
44             + "<ul>"
45             + "<li>Each Source Unit is converted to the Target Unit by multiplying it by the Factor and adding the Offset (if any).</li>"
46             + "<li>The unit identifiers are internal, and are to be localized for display to users. See <a href='https://www.unicode.org/cldr/charts/latest/by_type/units.area.html#hectare' target='units.area.hectare'>Hectare</a>, for example. "
47             + "<li>" + RATIONAL_MSG + "</li>"
48             + "<li>The Systems column indicates which systems the units are used in. For now, they just show the two ‘inch-pound’ systems.</li>"
49             + "<li>" + QUANTITY_MSG + "</li>"
50             + "<li>" + SPEC_GENERAL_MSG + ".</li>"
51             + "</ul>"
52             + dataScrapeMessage("/tr35-general.html#Contents", "common/testData/units/unitsTest.txt", "common/supplemental/units.xml");
53     }
54 
55     @Override
writeContents(FormattedFileWriter pw)56     public void writeContents(FormattedFileWriter pw) throws IOException {
57         //  <convertUnit source='ounce' baseUnit='kilogram' factor='lb_to_kg/16' systems="ussystem uksystem"/>
58         //  <convertUnit source='fahrenheit' baseUnit='kelvin' factor='5/9' offset='2298.35/9' systems="ussystem uksystem"/>
59 
60 
61         TablePrinter tablePrinter = new TablePrinter()
62             .addColumn("Quantity", "class='target'", null, "class='target'", true)
63             .setHidden(true)
64             .setRepeatHeader(true)
65             .setBreakSpans(true)
66             .addColumn("Source Unit", "class='source'", CldrUtility.getDoubleLinkMsg(), "class='source'", true)
67             .addColumn("Factor", "class='target'", null, "class='target'", true)
68             .setCellAttributes("class='target' style='text-align:right'")
69             .addColumn("Offset", "class='target'", null, "class='target'", true)
70             .setCellAttributes("class='target' style='text-align:right'")
71             .addColumn("Target", "class='target'", null, "class='target'", true)
72             .addColumn("Systems", "class='target'", null, "class='target'", true)
73             .addColumn("Quantity", "class='target'", CldrUtility.getDoubleLinkMsg(), "class='target'", true)
74             ;
75 
76         UnitConverter converter = SDI.getUnitConverter();
77         converter.getSourceToSystems();
78 
79         for (Entry<String, TargetInfo> entry : converter.getInternalConversionData().entrySet()) {
80             String sourceUnit = entry.getKey();
81             String quantity = converter.getQuantityFromUnit(sourceUnit, false);
82             TargetInfo targetInfo = entry.getValue();
83             tablePrinter.addRow()
84             .addCell(quantity)
85             .addCell(sourceUnit)
86             .addCell(targetInfo.unitInfo.factor.toString(FormatStyle.repeating))
87             .addCell(targetInfo.unitInfo.offset.equals(Rational.ZERO) ? "" : targetInfo.unitInfo.offset.toString(FormatStyle.repeating))
88             .addCell(targetInfo.target)
89             .addCell(Joiner.on(", ").join(converter.getSourceToSystems().get(sourceUnit)))
90             .addCell(quantity)
91             .finishRow();
92         }
93         pw.write(tablePrinter.toTable());
94     }
95 }
96