1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16class RangeEdge {
17  readonly value: number;
18  readonly inclusive: boolean;
19  constructor(value: number, inclusive: boolean) {
20    this.value = value;
21    this.inclusive = inclusive;
22  }
23}
24
25// eslint-disable-next-line @typescript-eslint/no-unused-vars
26class RatioRange {
27  readonly start: RangeEdge;
28  readonly end: RangeEdge;
29
30  constructor(start: RangeEdge, end: RangeEdge) {
31    this.start = start;
32    this.end = end;
33    if (this.start.value > this.end.value) {
34      throw new Error(`RatioRange: ${this.start.value} > ${this.end.value}`);
35    }
36  }
37
38  static newEmpty(): RatioRange {
39    return new RatioRange(new RangeEdge(0, false), new RangeEdge(0, false));
40  }
41
42  contains(point: number): boolean {
43    if (point === this.start.value) {
44      return this.start.inclusive;
45    }
46    if (point === this.end.value) {
47      return this.end.inclusive;
48    }
49    return this.start.value < point && point < this.end.value;
50  }
51
52  toString(): string {
53    return `${this.start.inclusive ? '[' : '('}${this.start.value}, ${this.end.value}${this.end.inclusive ? ']' : ')'}`;
54  }
55}
56