| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.functionlibrary.common.uomo; |
| |
| import static org.eclipse.uomo.units.SI.Prefix.DECI; |
| import static org.eclipse.uomo.units.SI.Prefix.CENTI; |
| import static org.eclipse.uomo.units.SI.Prefix.MILLI; |
| import static org.eclipse.uomo.units.SI.Prefix.HECTO; |
| import static org.eclipse.uomo.units.SI.Prefix.KILO; |
| import static org.eclipse.uomo.units.SI.CUBIC_METRE; |
| |
| import java.util.LinkedHashSet; |
| import java.util.Locale; |
| import java.util.Set; |
| import java.util.TreeMap; |
| |
| import org.eclipse.uomo.units.SI; |
| import org.eclipse.uomo.units.impl.BaseAmount; |
| import org.eclipse.uomo.units.impl.format.LocalUnitFormatImpl; |
| import org.eclipse.uomo.units.impl.quantity.VolumeAmount; |
| import org.unitsofmeasurement.quantity.Length; |
| import org.unitsofmeasurement.quantity.Volume; |
| import org.unitsofmeasurement.unit.Unit; |
| |
| /** |
| * System of metric volume units.<br> |
| * @see {@link http://de.wikipedia.org/wiki/Kubikmeter} |
| * @see {@link http://de.wikipedia.org/wiki/Liter} |
| */ |
| public class MetricVolumeUnit extends ASystemOfQuantityUnits<SI,Volume> implements ISystemOfQuantityUnits<SI,Volume> { |
| |
| protected MetricVolumeUnit() { |
| super("Metric Volume Units", SI.class, Volume.class, VolumeAmount.class); |
| } |
| |
| protected final static TreeMap<Double,Unit<?>> sUnits = new TreeMap<Double,Unit<?>> (); |
| protected static MetricVolumeUnit INSTANCE = new MetricVolumeUnit(); |
| |
| private static LocalUnitFormatImpl formatUS = LocalUnitFormatImpl.getInstance(Locale.US); |
| public final static Unit<Volume> M3 = SI.addUnit(CUBIC_METRE).asType(Volume.class); |
| // public final static Unit<Volume> L = SI.addUnit(MILLI(CUBIC_METRE), formatUS.format(CUBIC_METRE.divide(1000.0))).asType(Volume.class); |
| public final static Unit<Volume> L = SI.addUnit(CUBIC_METRE.divide(1000.0), formatUS.format(CUBIC_METRE.divide(1000.0))).asType(Volume.class); |
| |
| // public final static Unit<Volume> M3 = SI.addUnit(CUBIC_METRE, "m³").asType(Volume.class); |
| // public final static Unit<Volume> L = SI.addUnit(CUBIC_METRE.divide(1000.0), "l").asType(Volume.class); |
| //public final static Unit<Volume> CM3 = SI.addUnit(L.divide(1000.0), "cm³").asType(Volume.class); |
| //public final static Unit<Volume> DM3 = SI.addUnit(L.multiply(1.0), "dm³").asType(Volume.class); |
| // public final static Unit<Volume> DAM3 = SI.addUnit(KILO(M3), "dam³").asType(Volume.class); |
| public final static Unit<Volume> DAM3 = SI.addUnit(M3.multiply(1000.0), "dam³").asType(Volume.class); |
| public final static Unit<Volume> ML = SI.addUnit(L.divide(1000.0), "ml").asType(Volume.class); |
| // public final static Unit<Volume> ML = SI.addUnit(L.divide(1000.0), formatUS.format(L.divide(1000.0))).asType(Volume.class); |
| // public final static Unit<Volume> ML = SI.addUnit(MILLI(L), "ml").asType(Volume.class); |
| public final static Unit<Volume> CL = SI.addUnit(L.divide(100d), "cl").asType(Volume.class); |
| // public final static Unit<Volume> CL = SI.addUnit(CENTI(L), "cl").asType(Volume.class); |
| public final static Unit<Volume> DL = SI.addUnit(L.divide(10d), "dl").asType(Volume.class); |
| // public final static Unit<Volume> DL = SI.addUnit(DECI(L), "dl").asType(Volume.class); |
| public final static Unit<Volume> HL = SI.addUnit(M3.divide(10d), "hl").asType(Volume.class); |
| // public final static Unit<Volume> HL = SI.addUnit(DECI(M3), "hl").asType(Volume.class); |
| |
| static { |
| addUnits(sUnits, M3, L, ML, CL, DL, HL, /*DM3, CM3,*/ DAM3); |
| } |
| |
| protected final static MetricVolumeUnit getInstance() { |
| if (INSTANCE == null) { |
| INSTANCE = new MetricVolumeUnit(); |
| } |
| return INSTANCE; |
| } |
| |
| @Override |
| public Set<Unit<?>> getUnits() { |
| return units(); |
| } |
| |
| /** |
| * @see #getUnits() |
| */ |
| public static Set<Unit<?>> units() { |
| return new LinkedHashSet<Unit<?>>(sUnits.values()); |
| } |
| |
| /** |
| * @see #createAmount(Number, Unit) |
| */ |
| public static BaseAmount<Volume> amount(Number number, Unit<Volume> unit) { |
| return getInstance().createAmount(number, unit); |
| } |
| |
| /** |
| * @param side |
| * @return calculate the volume for a cube |
| */ |
| public static BaseAmount<Volume> calculateCube(BaseAmount<Length> side) { |
| return calculateCuboid(side, side, side); |
| } |
| |
| /** |
| * @param width |
| * @param height |
| * @param depth |
| * @return calculate the volume for a cuboid |
| */ |
| @SuppressWarnings("unchecked") |
| public static BaseAmount<Volume> calculateCuboid(BaseAmount<Length> width, BaseAmount<Length> height, BaseAmount<Length> depth) { |
| return ((BaseAmount<Volume>)((BaseAmount<?>)MetricAreaUnit.calculateRectangle(width, height.to(width.unit())).multiply(depth.to(width.unit())))).to(M3); |
| } |
| } |