42 lines
670 B
TypeScript
42 lines
670 B
TypeScript
export class Optional<T>
|
|
{
|
|
private object?: T;
|
|
|
|
private constructor(object?: T)
|
|
{
|
|
this.object = object;
|
|
}
|
|
|
|
static ofNullable<T>(object?: T): Optional<T>
|
|
{
|
|
return new Optional(object);
|
|
}
|
|
|
|
isEmpty()
|
|
{
|
|
return this.object == null;
|
|
}
|
|
|
|
isPresent()
|
|
{
|
|
return this.object != null;
|
|
}
|
|
|
|
orElse(other: T): T
|
|
{
|
|
if(this.object != null)
|
|
{
|
|
return this.object;
|
|
}
|
|
return other;
|
|
}
|
|
|
|
get(): T
|
|
{
|
|
if(this.object != null)
|
|
{
|
|
return this.object;
|
|
}
|
|
throw new Error("No value present in Optional.");
|
|
}
|
|
} |