-
Notifications
You must be signed in to change notification settings - Fork 23
/
ConstantCurve.ts
95 lines (73 loc) · 2.34 KB
/
ConstantCurve.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
import Type from "./Type";
import Time from "./Time";
import Curve from "./Curve";
import Interval from "./Interval";
import Invariance from "./Invariance";
/**
* 定数関数
*
* すべての時刻で同じ値を返す任意型の関数である。
*
* 関数値の型は構築子のパラメータにより指定する。
*/
class ConstantCurve extends Curve
{
private _constant_type: Type;
private _constant_value: any;
/**
* type 型の value を定数値とする定数関数を生成する。
*
* type は任意の型を指定することができる。
*
* value を省略したときは type 型の既定値を返す定数関数となる。
*
* @param type 関数値の型
* @param 初期定数値 (type 型)
*/
constructor( type: Type, value?: any )
{
super();
this._constant_type = type;
this._constant_value = type.getDefaultValue();
// 初期値が指定されているときは設定
if ( value !== undefined ) {
this.setConstantValue( value );
}
}
/**
* 定数値を設定
*
* @param value 定数値 (関数値の型)
*/
setConstantValue( value: any )
{
if ( value == this._constant_value ) {
// 同じ値で変化なし
// == 演算子で比較できない型は常に違う値と判断されることもある
return;
}
// 定数値を変更
this._constant_value = this._constant_type.getCloneValue( value );
// 全時刻の値が変化
this.notifyValueChange( Interval.UNIVERSAL );
}
override isTypeSupported( type: Type )
{
let from_type = this._constant_type;
return type.isConvertible( from_type );
}
override getValue( time: Time, type: Type )
{
let from_type = this._constant_type;
let from_value = from_type.getCloneValue( this._constant_value );
return type.convertValue( from_type, from_value );
}
override getInvariance( interval: Interval ): Invariance
{
// 全時間で一定
// (UNIVERSAL と非空区間は必ず交差するので interval の参照は不要)
const invariance = new Invariance();
return invariance.write( Interval.UNIVERSAL );
}
}
export default ConstantCurve;