UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

121 lines 5.69 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Metadata */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MutableInvertedUnit = exports.InvertedUnit = void 0; const DelayedPromise_1 = require("../DelayedPromise"); const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils"); const ECObjects_1 = require("../ECObjects"); const Exception_1 = require("../Exception"); const SchemaItem_1 = require("./SchemaItem"); const Unit_1 = require("./Unit"); const UnitSystem_1 = require("./UnitSystem"); /** * An InvertedUnit is a specific type of Unit that describes the inverse of a single Unit whose dimensional derivation is unit-less. * @public @preview */ class InvertedUnit extends SchemaItem_1.SchemaItem { schemaItemType = InvertedUnit.schemaItemType; /** @internal */ static get schemaItemType() { return ECObjects_1.SchemaItemType.InvertedUnit; } _invertsUnit; // required _unitSystem; // required get invertsUnit() { return this._invertsUnit; } get unitSystem() { return this._unitSystem; } /** * Type guard to check if the SchemaItem is of type InvertedUnit. * @param item The SchemaItem to check. * @returns True if the item is a InvertedUnit, false otherwise. */ static isInvertedUnit(item) { if (item && item.schemaItemType === ECObjects_1.SchemaItemType.InvertedUnit) return true; return false; } /** * Type assertion to check if the SchemaItem is of type InvertedUnit. * @param item The SchemaItem to check. * @returns The item cast to InvertedUnit if it is an InvertedUnit, undefined otherwise. * @internal */ static assertIsInvertedUnit(item) { if (!this.isInvertedUnit(item)) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidSchemaItemType, `Expected '${ECObjects_1.SchemaItemType.InvertedUnit}' (InvertedUnit)`); } /** * Save this InvertedUnit's properties to an object for serializing to JSON. * @param standalone Serialization includes only this object (as opposed to the full schema). * @param includeSchemaVersion Include the Schema's version information in the serialized object. */ toJSON(standalone = false, includeSchemaVersion = false) { const schemaJson = super.toJSON(standalone, includeSchemaVersion); schemaJson.invertsUnit = this.invertsUnit.fullName; schemaJson.unitSystem = this.unitSystem.fullName; return schemaJson; } /** @internal */ async toXml(schemaXml) { const itemElement = await super.toXml(schemaXml); const unitSystem = await this.unitSystem; if (undefined !== unitSystem) { const unitSystemName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, unitSystem.schema, unitSystem.name); itemElement.setAttribute("unitSystem", unitSystemName); } const invertsUnit = await this.invertsUnit; if (undefined !== invertsUnit) { const invertsUnitName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, invertsUnit.schema, invertsUnit.name); itemElement.setAttribute("invertsUnit", invertsUnitName); } return itemElement; } fromJSONSync(invertedUnitProps) { super.fromJSONSync(invertedUnitProps); const unitSchemaItemKey = this.schema.getSchemaItemKey(invertedUnitProps.invertsUnit); this._invertsUnit = new DelayedPromise_1.DelayedPromiseWithProps(unitSchemaItemKey, async () => { const invertsUnit = await this.schema.lookupItem(unitSchemaItemKey, Unit_1.Unit); if (undefined === invertsUnit) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the invertsUnit ${invertedUnitProps.invertsUnit}.`); return invertsUnit; }); const unitSystemSchemaItemKey = this.schema.getSchemaItemKey(invertedUnitProps.unitSystem); if (!unitSystemSchemaItemKey) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the unitSystem ${invertedUnitProps.unitSystem}.`); this._unitSystem = new DelayedPromise_1.DelayedPromiseWithProps(unitSystemSchemaItemKey, async () => { const unitSystem = await this.schema.lookupItem(unitSystemSchemaItemKey, UnitSystem_1.UnitSystem); if (undefined === unitSystem) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the unitSystem ${invertedUnitProps.unitSystem}.`); return unitSystem; }); } async fromJSON(invertedUnitProps) { this.fromJSONSync(invertedUnitProps); } /** * @internal * Used for schema editing */ setInvertsUnit(invertsUnit) { this._invertsUnit = invertsUnit; } /** * @internal * Used for schema editing */ setUnitSystem(unitSystem) { this._unitSystem = unitSystem; } } exports.InvertedUnit = InvertedUnit; /** * @internal * An abstract class used for schema editing. */ class MutableInvertedUnit extends InvertedUnit { } exports.MutableInvertedUnit = MutableInvertedUnit; //# sourceMappingURL=InvertedUnit.js.map