| /** |
| * |
| * 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 java.lang.reflect.InvocationTargetException; |
| import java.util.TreeMap; |
| |
| import org.eclipse.uomo.units.AbstractSystemOfUnits; |
| import org.eclipse.uomo.units.impl.BaseAmount; |
| import org.eclipse.uomo.units.impl.TransformedUnit; |
| import org.eclipse.uomo.units.impl.format.LocalUnitFormatImpl; |
| import org.unitsofmeasurement.quantity.Quantity; |
| import org.unitsofmeasurement.unit.SystemOfUnits; |
| import org.unitsofmeasurement.unit.Unit; |
| |
| |
| /** |
| * basic implementation for all systems of units |
| */ |
| public abstract class ASystemOfQuantityUnits<S extends SystemOfUnits, Q extends Quantity<Q>> extends AbstractSystemOfUnits implements ISystemOfQuantityUnits<S,Q> { |
| |
| protected final String sName; |
| protected final Class<S> sSystemOfUnitsClass; |
| protected final Class<Q> sQuantityClass; |
| protected final Class<?> sAmountClass; |
| |
| /** |
| * Adds all given units to the map ordered by their "weight of quantity".<br> |
| * Also their unit name is put into the global symbol map of UOMo! |
| * @param map |
| * @param units |
| */ |
| @SuppressWarnings({ "unchecked", "rawtypes" }) |
| protected static void addUnits(TreeMap<Double,Unit<?>> map, Unit<?>... units) { |
| try { |
| BaseAmount<?> referenceAmount = null; |
| for (Unit unit : units) { |
| if (referenceAmount == null) { |
| referenceAmount = new BaseAmount<>(1000000000, unit); |
| } |
| map.put(referenceAmount.doubleValue(unit), unit); |
| if (unit instanceof TransformedUnit<?>) { |
| LocalUnitFormatImpl.getInstance().getSymbolMap().label(unit, ((TransformedUnit<?>)unit).getName()); |
| } |
| else { |
| LocalUnitFormatImpl.getInstance().getSymbolMap().label(unit, unit.toString()); |
| } |
| } |
| } |
| catch (Exception e) { |
| System.err.println(e.getLocalizedMessage()); |
| } |
| } |
| |
| protected ASystemOfQuantityUnits (String name, Class<S> systemOfUnitsClass, Class<Q> quantityClass, Class<?> amountClass) { |
| sName = name; |
| sSystemOfUnitsClass = systemOfUnitsClass; |
| sQuantityClass = quantityClass; |
| sAmountClass = amountClass; |
| } |
| |
| @SuppressWarnings("unchecked") |
| @Override |
| public final BaseAmount<Q> createAmount(Number number, Unit<Q> unit) { |
| try { |
| return (BaseAmount<Q>)(sAmountClass.getConstructor(Number.class, Unit.class).newInstance(number, unit)); |
| } catch (InstantiationException e) { |
| e.printStackTrace(); |
| } catch (IllegalAccessException e) { |
| e.printStackTrace(); |
| } catch (IllegalArgumentException e) { |
| e.printStackTrace(); |
| } catch (InvocationTargetException e) { |
| e.printStackTrace(); |
| } catch (NoSuchMethodException e) { |
| e.printStackTrace(); |
| } catch (SecurityException e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| @Override |
| public final Class<S> getSystemOfUnitsClass() { |
| return sSystemOfUnitsClass; |
| } |
| |
| @Override |
| public final Class<Q> getQuantityClass() { |
| return sQuantityClass; |
| } |
| |
| @SuppressWarnings("unchecked") |
| @Override |
| public final Class<BaseAmount<Q>> getAmountClass() { |
| return (Class<BaseAmount<Q>>)sAmountClass; |
| } |
| |
| @Override |
| public final String getName() { |
| return sName; |
| } |
| |
| @Override |
| public final boolean handles(Class<?> systemOfUnitsOrQuantity) { |
| return systemOfUnitsOrQuantity.equals(getSystemOfUnitsClass()) || systemOfUnitsOrQuantity.equals(getQuantityClass()); |
| } |
| } |