UNPKG

ts-std-lib

Version:
131 lines (130 loc) 6.16 kB
/// <reference types="node" /> import { InspectOptions } from 'util'; import { IEquatable, equals } from './Equality'; import { IFilterable } from './IFilterable'; import { IMappable } from './IMappable'; import { IChainable } from './IChainable'; import { Nullable } from './Nullable'; import { Undefinable } from './Undefinable'; import { IEqualityComparer } from './Equality'; import { IInspectable, inspect } from './IInspectable'; import { IInspector } from './IInspector'; /** * Error for the optional values not set */ export declare class OptionalValueNotSetError extends Error { constructor(); } /** * An object to wrap a nullable in an interface to avoid null exeptions. * * @see https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html * @see http://www.baeldung.com/java-optional */ export declare class Optional<T> implements IEquatable<Optional<T>>, IFilterable<T>, IMappable<T>, IChainable<T>, IInspectable { private readonly _value; private readonly _equalityComparer; private readonly _inspector; /** * Creates an empty instance of Optional. No value is present for this Optional. * @param equalityComparer An optional equality comparer for the value. * @param inspector An optional inspector for the value. */ constructor(value?: undefined, equalityComparer?: IEqualityComparer<T>, inspector?: IInspector<T>); /** * Creates an instance of Optional describing the specified value, if defined, otherwise returns an empty Optional. * @param value A possibly null value. * @param equalityComparer An optional equality comparer for the value. * @param inspector An optional inspector for the value. */ constructor(value: Undefinable<T>, equalityComparer?: IEqualityComparer<T>, inspector?: IInspector<T>); /** * Creates an instance of Optional with the specified present non-null and defined value. * @param value A non-nullable value. * @param equalityComparer An optional equality comparer for the value. * @param inspector An optional inspector for the value. */ constructor(value: T, equalityComparer?: IEqualityComparer<T>, inspector?: IInspector<T>); /** * Creates an instance of Optional describing the specified value, if non-null, otherwise returns an empty Optional. * @param value A possibly null value. * @param equalityComparer An optional equality comparer for the value. * @param inspector An optional inspector for the value. */ constructor(value: Nullable<T>, equalityComparer?: IEqualityComparer<T>, inspector?: IInspector<T>); constructor(value: Undefinable<Nullable<T>>, equalityComparer?: IEqualityComparer<T>, inspector?: IInspector<T>); [equals](other: Optional<T>): boolean; [inspect](options?: InspectOptions): string; /** * Check if the value is null. * * @returns True if there is a value present, otherwise false. */ isPresent(): boolean; /** * If a value is present, invoke the specified callback with the value, otherwise do nothing. * * @param callback The callback to invoke. */ ifPresent(callback: (value: T) => void): void; /** * If a value is present, performs the given callback with the value, otherwise performs the given empty-based callback. * * @param callback The callback to be performed, if a value is present. * @param emptyCallback The callback to be performed, if no value is present. */ ifPresentOrElse(callback: (value: T) => void, emptyCallback: () => void): void; /** * If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function. * * @param supplier The supplying function that produces an Optional to be returned. * @returns An Optional describing the value of this Optional, if a value is present, otherwise an Optional produced by the supplying function. */ or(supplier: () => Optional<T>): Optional<T>; /** * Return the value if not null or a default. * * @param defaultValue The default value to return. * @returns The value if present, otherwise return the default. * @description The methods orElse and orElseGet are generally preferable to this method, as they return a substitute value if the value is absent, instead of throwing an exception. */ orElse(supplier: T): T; /** * Return the value if not null or a default via a callback, to allow for lazy loading. * * @param callback The callback to invoke to get the default value to return. * @returns The value if present, otherwise return the default. */ orElseGet(callback: () => T): T; /** * Return the contained value, if present, otherwise throw an exception to be created by the provided supplier. * * @param exceptionSupplier The callback to invoke to get the error to throw. * @returns The value if present. */ orElseThrow(exceptionSupplier: () => Error): T; /** * Return the contained value, if present, otherwise return 'undefined'. * * @returns {Undefinable<T>} */ orUndefined(): Undefinable<T>; /** * Return the contained value, if present, otherwise return 'null'. * * @returns {Undefinable<T>} */ orNull(): Nullable<T>; /** * If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException. * * @returns {T} The value if present. */ get(): T; filter<S extends T>(predicate: (value: T) => value is S): Optional<S>; filter(predicate: (value: T) => boolean): Optional<T>; map<U>(callbackfn: (value: T) => U, equalityComparer?: IEqualityComparer<U>): Optional<U>; chain<U>(callbackfn: (value: T) => Optional<U>, equalityComparer?: IEqualityComparer<U>): Optional<U>; chain<U>(callbackfn: (value: T) => U, equalityComparer?: IEqualityComparer<U>): Optional<U>; private isValuePresent; }