import {Operators} from "./Operators"; import {primitiveNumber} from "./Globals"; import {PrimitiveNumber} from "./PrimitiveNumber"; import {IllegalArgumentException} from "../Exception"; /*** * */ export class Operator{ /*** * */ private readonly value:T; private readonly op: Operators = new Operators.PrimitiveOperators(); /*** * */ constructor(value:T) {this.value = value;} /*** * */ private cast( a: T|number ):T{ if( !(a instanceof PrimitiveNumber.PrimitiveNumberBuilder ) ) return this.value.getClass().newInstance(Number(a)); if(!(a).signed().equals(this.value.signed())) throw new IllegalArgumentException(`Bad Cast Operator : cannot cast ${(a).signed()?"signed":"unsigned"} number to ${this.value.signed()?"signed":"unsigned"} number`) return a; } /*** * */ private out(a: number ): Operator { return new Operator(this.value.getClass().newInstance(a)); } /*** * */ public add(a: T|number ): Operator { return this.out(this.op.add(this.value,this.cast(a))); } /*** * */ public mul(a: T|number): Operator { return this.out(this.op.mul(this.value,this.cast(a))); } /*** * */ public sous(a: T|number): Operator { return this.out((this.op.sous(this.value,this.cast(a)))); } /*** * */ public inc(): Operator{ return this.out((this.op.inc(this.value))); } /*** * */ public dec(): Operator { return this.out((this.op.dec(this.value))); } /*** * */ public and(a: T|number): Operator { return this.out((this.op.and(this.value,this.cast(a)))); } /*** * */ public or(a: T|number): Operator { return this.out((this.op.or(this.value,this.cast(a)))); } /*** * */ public xor(a: T|number): Operator { return this.out((this.op.xor(this.value,this.cast(a)))); } /*** * */ public result():T{return this.value;} } Object.package(this);