All files / src/auxiliary AtomicInteger.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x                                                                                                                     1x 67x    
import { RequiredType, guardFunctions } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * Responsibility: An atomic integer interface for thread-safe integer operations.
 */
export interface AtomicInteger {
 
    /**
     * Get the current integer value.
     */
    get(): number;
 
    /**
     * Set the current integer value.
     * @param value the new value
     */
    set(value: number): void;
 
    /**
     * Atomically sets the value to the given updated value if the current value == the expected value.
     * @param expectedValue the expected current value
     * @param newValue the requested new value 
     * @return true if successful. False return indicates that the actual value was not equal to the expected value.
     */
    compareAndSet(expectedValue: number, newValue: number): boolean;
 
    /**
     * Atomically increments the current value by one.
     * @return the updated value
     */
    incrementAndGet(): number;
 
    /**
     * Atomically decrements the current value by one.
     * @return the updated value
     */
    decrementAndGet(): number;
 
    /**     
     * Atomically sets to the given value and returns the previous value.
     * 
     * @param newValue the new value
     * @return the previous value
     */
    getAndSet(newValue: number): number;
 
    /**
     * Helpful method for converting to primitive types.
     * @param hint primitive hint
     */
    [Symbol.toPrimitive](hint: string): string | number | boolean;
}
 
/**
 * Type guard for AtomicInteger interface.
 * 
 * @param instance the instance to check
 * @returns true if the instance implements AtomicInteger
 */
export function guard(instance: unknown): instance is RequiredType<AtomicInteger> {
    return guardFunctions(instance, "compareAndSet", "incrementAndGet", "decrementAndGet", "getAndSet", "get", "set");
}