41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
|
|
import {Converter} from './converter';
|
|
import {Base64Encoder} from "./base64encoder";
|
|
import {Base64Decoder} from "./base64decoder";
|
|
|
|
@Injectable()
|
|
export class ConverterregistryService {
|
|
private converters:Converter[] = [];
|
|
|
|
constructor() {
|
|
this.init();
|
|
}
|
|
|
|
public getConverters():Converter[] {
|
|
return this.converters;
|
|
}
|
|
|
|
public getConverter(id:string):Converter {
|
|
for (let i = 0; i < this.converters.length; i++) {
|
|
if (this.converters[i].getId() == id) {
|
|
return this.converters[i];
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
private init():void {
|
|
this.registerConverter(new Base64Encoder());
|
|
this.registerConverter(new Base64Decoder());
|
|
}
|
|
|
|
private registerConverter(converter:Converter):void {
|
|
this.converters.forEach((c:Converter) => {
|
|
if (c.getId() == converter.getId()) {
|
|
throw new Error("Converter-ID " + converter.getId() + " is already registered!");
|
|
}
|
|
});
|
|
this.converters.push(converter);
|
|
}
|
|
}
|