UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

147 lines 5.99 kB
/*--------------------------------------------------------------------------------------------- * 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 { Phenomenon } from "./Phenomenon"; import { SchemaItem } from "./SchemaItem"; /** * A Constant is a specific type of Unit that represents a number. * @public @preview */ export class Constant extends SchemaItem { schemaItemType = Constant.schemaItemType; /** @internal */ static get schemaItemType() { return SchemaItemType.Constant; } _phenomenon; _definition; _numerator; _denominator; /** @internal */ constructor(schema, name) { super(schema, name); this._definition = ""; } get phenomenon() { return this._phenomenon; } get definition() { return this._definition; } get numerator() { return this._numerator ?? 1.0; } get denominator() { return this._denominator ?? 1.0; } get hasNumerator() { return (this._numerator !== undefined); } get hasDenominator() { return (this._denominator !== undefined); } /** * Save this Constants 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); if (this.phenomenon !== undefined) schemaJson.phenomenon = this.phenomenon.fullName; schemaJson.definition = this.definition; if (this.hasNumerator) schemaJson.numerator = this.numerator; if (this.hasDenominator) schemaJson.denominator = this.denominator; return schemaJson; } /** @internal */ async toXml(schemaXml) { const itemElement = await super.toXml(schemaXml); itemElement.setAttribute("definition", this.definition); if (this.hasNumerator) itemElement.setAttribute("numerator", this.numerator.toString()); if (this.hasDenominator) itemElement.setAttribute("denominator", this.denominator.toString()); const phenomenon = await this.phenomenon; if (undefined !== phenomenon) { const phenomenonName = XmlSerializationUtils.createXmlTypedName(this.schema, phenomenon.schema, phenomenon.name); itemElement.setAttribute("phenomenon", phenomenonName); } return itemElement; } fromJSONSync(constantProps) { super.fromJSONSync(constantProps); const schemaItemKey = this.schema.getSchemaItemKey(constantProps.phenomenon); if (!schemaItemKey) throw new ECSchemaError(ECSchemaStatus.InvalidECJson, `Unable to locate the phenomenon ${constantProps.phenomenon}.`); this._phenomenon = new DelayedPromiseWithProps(schemaItemKey, async () => { const phenom = await this.schema.lookupItem(schemaItemKey, Phenomenon); if (undefined === phenom) throw new ECSchemaError(ECSchemaStatus.InvalidECJson, `Unable to locate the phenomenon ${constantProps.phenomenon}.`); return phenom; }); if (this._definition !== "" && constantProps.definition.toLowerCase() !== this._definition.toLowerCase()) throw new ECSchemaError(ECSchemaStatus.InvalidECJson, `The Constant ${this.name} has an invalid 'definition' attribute.`); else if (this._definition === "") this._definition = constantProps.definition; if (undefined !== constantProps.numerator) { if (constantProps.numerator !== this._numerator) this._numerator = constantProps.numerator; } if (undefined !== constantProps.denominator) { if (constantProps.denominator !== this._denominator) this._denominator = constantProps.denominator; } } async fromJSON(constantProps) { this.fromJSONSync(constantProps); } /** * @param phenomenon A LazyLoadedPhenomenon. * @internal */ setPhenomenon(phenomenon) { this._phenomenon = phenomenon; } /** * @internal */ setDefinition(definition) { this._definition = definition; } /** * @internal */ setNumerator(numerator) { this._numerator = numerator; } /** * @internal */ setDenominator(denominator) { this._denominator = denominator; } /** * Type guard to check if the SchemaItem is of type Constant. * @param item The SchemaItem to check. * @returns True if the item is a Constant, false otherwise. */ static isConstant(item) { if (item && item.schemaItemType === SchemaItemType.Constant) return true; return false; } /** * Type assertion to check if the SchemaItem is of type Constant. * @param item The SchemaItem to check. * @returns The item cast to Constant if it is a Constant, undefined otherwise. * @internal */ static assertIsConstant(item) { if (!this.isConstant(item)) throw new ECSchemaError(ECSchemaStatus.InvalidSchemaItemType, `Expected '${SchemaItemType.Constant}' (Constant)`); } } /** * @internal * An abstract class used for schema editing. */ export class MutableConstant extends Constant { } //# sourceMappingURL=Constant.js.map