@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
116 lines • 5.25 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* 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
*/
import { DelayedPromiseWithProps } from "../DelayedPromise";
import { XmlSerializationUtils } from "../Deserialization/XmlSerializationUtils";
import { SchemaItemType } from "../ECObjects";
import { ECSchemaError, ECSchemaStatus } from "../Exception";
import { SchemaItem } from "./SchemaItem";
import { Unit } from "./Unit";
import { UnitSystem } from "./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
*/
export class InvertedUnit extends SchemaItem {
schemaItemType = InvertedUnit.schemaItemType;
/** @internal */
static get schemaItemType() { return 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 === 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 ECSchemaError(ECSchemaStatus.InvalidSchemaItemType, `Expected '${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.createXmlTypedName(this.schema, unitSystem.schema, unitSystem.name);
itemElement.setAttribute("unitSystem", unitSystemName);
}
const invertsUnit = await this.invertsUnit;
if (undefined !== invertsUnit) {
const invertsUnitName = 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 DelayedPromiseWithProps(unitSchemaItemKey, async () => {
const invertsUnit = await this.schema.lookupItem(unitSchemaItemKey, Unit);
if (undefined === invertsUnit)
throw new ECSchemaError(ECSchemaStatus.InvalidECJson, `Unable to locate the invertsUnit ${invertedUnitProps.invertsUnit}.`);
return invertsUnit;
});
const unitSystemSchemaItemKey = this.schema.getSchemaItemKey(invertedUnitProps.unitSystem);
if (!unitSystemSchemaItemKey)
throw new ECSchemaError(ECSchemaStatus.InvalidECJson, `Unable to locate the unitSystem ${invertedUnitProps.unitSystem}.`);
this._unitSystem = new DelayedPromiseWithProps(unitSystemSchemaItemKey, async () => {
const unitSystem = await this.schema.lookupItem(unitSystemSchemaItemKey, UnitSystem);
if (undefined === unitSystem)
throw new ECSchemaError(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;
}
}
/**
* @internal
* An abstract class used for schema editing.
*/
export class MutableInvertedUnit extends InvertedUnit {
}
//# sourceMappingURL=InvertedUnit.js.map