/*** * Fake Lombok : * * properties decorator : * + @GETTER * + @SETTER * */ import {IOException, NoSuchElementException} from "./Exception"; export module flombok{ /*** * Accessor prototype */ export type accessorGetFunc = ()=>T export type accessorSetFunc = ( value: T )=>void export type getStringFunc = accessorGetFunc export type getNumberFunc = accessorGetFunc export type getObjectFunc = accessorGetFunc export type getBooleanFunc= accessorGetFunc export type getStringFuncA = accessorGetFunc export type getNumberFuncA = accessorGetFunc export type getObjectFuncA = accessorGetFunc export type getBooleanFuncA= accessorGetFunc export type setStringFunc = accessorSetFunc export type setNumberFunc = accessorSetFunc export type setObjectFunc = accessorSetFunc export type setBooleanFunc= accessorSetFunc export type setStringFuncA = accessorSetFunc export type setNumberFuncA = accessorSetFunc export type setObjectFuncA = accessorSetFunc export type setBooleanFuncA= accessorSetFunc /*** * * @param readOnly * @param orThrow * @constructor */ export function GETTER( readOnly : boolean = false, orThrow:boolean = false ) { return function (target : any, key : string ){ let name:string=key.substr(0,1).toUpperCase()+key.substr(1,key.length); Object.defineProperty(target,"get"+name,{ writable:!(readOnly), value:function () :T{ try {return this[key];}catch (e) { if(orThrow) new NoSuchElementException(`flombok : Getter element [ ${key} ] not found !`) return null; } } }); }; } /*** * * @param readOnly * @constructor */ export function SETTER( readOnly : boolean = false ) { return function (target : any, key : string ){ let name:string=key.substr(0,1).toUpperCase()+""+key.substr(1,key.length); Object.defineProperty(target,"set"+name,{ writable:!(readOnly), value:function ( value : T) { try{this[key] = value;}catch (e) { throw new IOException(`flombok : Setter method [ ${key} ] not found !`) } } }); }; } /** * @enumerable decorator that sets the enumerable property of a class field to false. * @param enumerable * @param defaultValue true|false */ export function ENUMERABLE( enumerable: boolean, defaultValue: any = null ) { return (target: any, propertyKey: string):any => { let descriptor: PropertyDescriptor; if (( descriptor = Object.getOwnPropertyDescriptor(target, propertyKey) || {}) != defaultValue) { descriptor.enumerable = enumerable; descriptor.writable = true; descriptor.value = defaultValue; Object.defineProperty(target, propertyKey, descriptor); return void 0; } }; } /** * @enumerable * @param enumerable */ export function ENUMERABLEFUNC( enumerable: boolean) { return function(target: any, propertyKey: string, descriptor: PropertyDescriptor ) :void { if (( descriptor.enumerable ) != enumerable) descriptor.enumerable = enumerable; }; } export function FINAL( ) { return (target: any, propertyKey: string):any => { let descriptor: PropertyDescriptor; if (( descriptor = Object.getOwnPropertyDescriptor(target, propertyKey) || {})) { descriptor.writable = false; Object.defineProperty(target, propertyKey, descriptor); return void 0; } }; } }