-
Notifications
You must be signed in to change notification settings - Fork 208
/
InvertedUnit.ts
121 lines (106 loc) · 5.02 KB
/
InvertedUnit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*---------------------------------------------------------------------------------------------
* 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 { InvertedUnitProps } from "../Deserialization/JsonProps";
import { XmlSerializationUtils } from "../Deserialization/XmlSerializationUtils";
import { SchemaItemType } from "../ECObjects";
import { ECObjectsError, ECObjectsStatus } from "../Exception";
import { LazyLoadedUnit, LazyLoadedUnitSystem } from "../Interfaces";
import { SchemaItemKey } from "../SchemaKey";
import { Schema } from "./Schema";
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.
* @beta
*/
export class InvertedUnit extends SchemaItem {
public override readonly schemaItemType!: SchemaItemType.InvertedUnit; // eslint-disable-line
protected _invertsUnit?: LazyLoadedUnit; // required
protected _unitSystem?: LazyLoadedUnitSystem; // required
constructor(schema: Schema, name: string) {
super(schema, name);
this.schemaItemType = SchemaItemType.InvertedUnit;
}
public get invertsUnit(): LazyLoadedUnit | undefined { return this._invertsUnit; }
public get unitSystem(): LazyLoadedUnitSystem | undefined { return this._unitSystem; }
/**
* 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.
*/
public override toJSON(standalone: boolean = false, includeSchemaVersion: boolean = false): InvertedUnitProps {
const schemaJson = super.toJSON(standalone, includeSchemaVersion) as any;
schemaJson.invertsUnit = this.invertsUnit!.fullName;
schemaJson.unitSystem = this.unitSystem!.fullName;
return schemaJson;
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
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;
}
public override fromJSONSync(invertedUnitProps: InvertedUnitProps) {
super.fromJSONSync(invertedUnitProps);
const unitSchemaItemKey = this.schema.getSchemaItemKey(invertedUnitProps.invertsUnit);
this._invertsUnit = new DelayedPromiseWithProps<SchemaItemKey, Unit>(unitSchemaItemKey,
async () => {
const invertsUnit = await this.schema.lookupItem<Unit>(unitSchemaItemKey);
if (undefined === invertsUnit)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Unable to locate the invertsUnit ${invertedUnitProps.invertsUnit}.`);
return invertsUnit;
});
const unitSystemSchemaItemKey = this.schema.getSchemaItemKey(invertedUnitProps.unitSystem);
if (!unitSystemSchemaItemKey)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Unable to locate the unitSystem ${invertedUnitProps.unitSystem}.`);
this._unitSystem = new DelayedPromiseWithProps<SchemaItemKey, UnitSystem>(unitSystemSchemaItemKey,
async () => {
const unitSystem = await this.schema.lookupItem<UnitSystem>(unitSystemSchemaItemKey);
if (undefined === unitSystem)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Unable to locate the unitSystem ${invertedUnitProps.unitSystem}.`);
return unitSystem;
});
}
public override async fromJSON(invertedUnitProps: InvertedUnitProps) {
this.fromJSONSync(invertedUnitProps);
}
/**
* @alpha
* Used for schema editing
*/
protected setInvertsUnit(invertsUnit: LazyLoadedUnit) {
this._invertsUnit = invertsUnit;
}
/**
* @alpha
* Used for schema editing
*/
protected setUnitSystem(unitSystem: LazyLoadedUnitSystem) {
this._unitSystem = unitSystem;
}
}
/**
* @internal
* An abstract class used for schema editing.
*/
export abstract class MutableInvertedUnit extends InvertedUnit {
public abstract override setInvertsUnit(invertsUnit: LazyLoadedUnit): void;
public abstract override setUnitSystem(unitSystem: LazyLoadedUnitSystem): void;
public abstract override setDisplayLabel(displayLabel: string): void;
}