/** * Tests whether the given {@link UnitDefinition} belongs to the predefined * SBML units. * * @param ud * the {@link UnitDefinition} to test. * @return {@code true}, if name is one of the five SBML predefined * unit identifiers ('substance', 'volume', 'area', 'length' or * 'time'), {@code false} otherwise. The predefined unit * identifiers 'length' and 'area' were added in Level 2 Version 1 * @see #isPredefined(String, int) */ public static boolean isPredefined(UnitDefinition ud) { return isPredefined(ud.getId(), ud.getLevel()); }
/** * Adds a new {@link Unit} instance, with the given kind, to this {@link UnitDefinition}. * * @param kind the unit kind. */ public void addUnit(Unit.Kind kind) { addUnit(new Unit(1d, 0, kind, 1d, getLevel(), getVersion())); }
/** * Tests if the given unit is a predefined unit, meaning it is one of the five SBML predefined * unit identifiers ('substance', 'volume', 'area', 'length' or 'time'). * * @param ud a unit definition * @return {@code true} if the given unit is a predefined unit. */ public static boolean isPredefined(UnitDefinition ud) { if (ud.getLevel() > 2) { return false; } if (ud.getUnitCount() == 1) { UnitDefinition predef = getPredefinedUnit(ud.getId(), ud.getLevel(), ud.getVersion()); if ((predef != null) && Unit.isPredefined(ud.getId(), ud.getLevel())) { return ud.equals(predef); } } return false; }
/** * Creates a new {@link Unit} instance with the given {@link Unit.Kind} and * adds * it to this {@link UnitDefinition}. * * @param kind * the kind to be set in the new Unit. * @return a new {@link Unit} instance with the given {@link Unit.Kind}. */ public Unit createUnit(Unit.Kind kind) { Unit unit = new Unit(1d, 0, kind, 1d, getLevel(), getVersion()); addUnit(unit); return unit; }
/** * Tests whether this {@link UnitDefinition} contains * exactly one {@link Unit} that itself represents a {@link Kind}, i.e., * multiplier = 1, exponent = 1, scale = 1. Note that this method requires * the level and version attributes of this {@link UnitDefinition} to be * set. * * @return {@code true} if this {@link UnitDefinition} contains * exactly one {@link Unit} that itself represents a simple {@link Kind}. */ public boolean isUnitKind() { if (getUnitCount() == 1) { return Unit.isUnitKind(getUnit(0).getKind(), getLevel(), getVersion()); } return false; }
/** * Checks whether the given {@link UnitDefinition} and the * {@link UnitDefinition} or {@link Unit} represented by the given * {@link String} are equivalent. * * @param ud a {@link UnitDefinition} * @param units a String representing either a {@link UnitDefinition} id or a unit {@link Kind}. * @return {@code true} if both units are equivalent * @see #areEquivalent(UnitDefinition, UnitDefinition) * @see Unit#areEquivalent(Unit, Unit) */ public static boolean areEquivalent(UnitDefinition ud, String units) { UnitDefinition ud2 = ud.getModel().getUnitDefinition(units); if (ud2 != null) { return areEquivalent(ud, ud2); } else if (ud.isUnitKind() && Unit.isUnitKind(units, ud.getLevel(), ud.getVersion())) { return Unit.areEquivalent(ud.getUnit(0), units); } return false; }
/** * Ensures that level and version combination of a unit are the same then * these that are defined here. * * @param unit */ protected void setLevelAndVersion(UnitDefinition unit) { if ((unit.getLevel() != level) || (unit.getVersion() != version)) { unit.setLevel(level); unit.setVersion(version); unit.getListOfUnits().setLevel(level); unit.getListOfUnits().setVersion(version); for (Unit u : unit.getListOfUnits()) { u.setLevel(level); u.setVersion(version); } } }
/** * Ensures that level and version combination of a unit are the same then * these that are defined here. * * @param unit */ private void setLevelAndVersion(UnitDefinition unit) { if ((unit.getLevel() != level) || (unit.getVersion() != version)) { unit.setLevel(level); unit.setVersion(version); unit.getListOfUnits().setLevel(level); unit.getListOfUnits().setVersion(version); for (Unit u : unit.getListOfUnits()) { u.setLevel(level); u.setVersion(version); } } }
UnitDefinition tempUD = new UnitDefinition(getLevel(), getVersion()); tempUD.addUnit(unit);
/** * Orders alphabetically the {@link Unit} objects within the * {@link #listOfUnits} of a {@link UnitDefinition}. * * @param ud * the {@link UnitDefinition} object whose {@link Unit}s are to be * reordered. */ public static void reorder(UnitDefinition ud) { if (1 < ud.getUnitCount()) { ListOf<Unit> orig = ud.getListOfUnits(); ListOf<Unit> units = new ListOf<Unit>(ud.getLevel(), ud.getVersion()); units.setSBaseListType(Type.listOfUnits); orig.removeAllTreeNodeChangeListeners(); units.add(orig.remove(orig.size() - 1)); int i, j; for (i = orig.size() - 1; i >= 0; i--) { Unit u = orig.remove(i); j = 0; while ((j < units.size()) && (0 < u.getKind().compareTo(units.get(j).getKind()))) { j++; } units.add(j, u); } ud.setListOfUnits(units); } }