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
16interface It<T> {
17}
18
19class Cl<T> implements It<T> {
20
21  constructor(p: T) {
22    this.x = p;
23  }  
24 
25  static resolve<U>(value: U|It<U>): Cl<U> {
26    if (value instanceof Cl) {
27        return value as Cl<U>;
28    }
29    return new Cl<U>(value as U);
30  }
31
32  x: T;
33
34  print(): string {
35    if (this.x == undefined) {
36      return "value is " + this.x;
37    }
38    else if (this.x instanceof string) {
39      return "string: '" + this.x + "'";
40    } else {
41      return "number = " + this.x;
42    }
43  }
44}
45
46
47function main(): void {
48
49  assert(Cl.resolve(undefined).print() == "value is undefined");
50  assert(Cl.resolve("test").print() == "string: 'test'");
51  assert(Cl.resolve(5.5).print() == "number = 5.5");
52  assert(Cl.resolve(new Int(8)).print() == "number = 8");
53
54  assert(Cl.resolve(new Cl<null>(null)).print() == "value is null");
55  assert(Cl.resolve(new Cl<string>("TEST")).print() == "string: 'TEST'");
56  assert(Cl.resolve(new Cl<number>(7.7)).print() == "number = 7.7");
57  assert(Cl.resolve(new Cl<Int>(new Int(-8))).print() == "number = -8");
58}
59