export class Optionnal { private static readonly EMPTY = new Optionnal(null); public static empty(): Optionnal { return Optionnal.EMPTY; } public static of(value: T): Optionnal { if (value == null) { throw new Error("Value cannot be null"); } return new Optionnal(value); } public static ofNullable(value: T): Optionnal { return value == null ? Optionnal.empty() : new Optionnal(value); } private readonly value: T; constructor(value: T) { this.value = value; } public get(): T { if (this.isEmpty()) { throw new Error("No value present"); } return this.value; } public isPresent(): boolean { return this.value != null; } public isEmpty(): boolean { return this.value == null; } public map(mapper: (value: T) => U): Optionnal { if (this.isEmpty()) { return Optionnal.empty(); } return Optionnal.ofNullable(mapper(this.value)); } public orElse(other: T): T { return this.isEmpty() ? other : this.value; } }