|
| 1 | +import { throwIfNotNull } from '../../../common/ErrorHandling'; |
| 2 | +import { DeviceType } from './hardware/DeviceType'; |
| 3 | +import { Disk } from './hardware/Disk'; |
| 4 | +import { Hardware } from './hardware/Hardware'; |
| 5 | +import { Memory } from './hardware/Memory'; |
| 6 | +import { Processor } from './hardware/Processor'; |
| 7 | +import { ConnectionMedium } from './network/ConnectionMedium'; |
| 8 | +import { Network } from './network/Network'; |
| 9 | +import { NetworkMetrics } from './network/NetworkMetrics'; |
| 10 | +import { Resources } from './Resources'; |
| 11 | +import { OperatingSystem } from './software/OperatingSystem'; |
| 12 | +import { Software } from './software/Software'; |
| 13 | +import { SupportedTask } from './software/SupportedTask'; |
| 14 | + |
| 15 | +export class ResourcesBuilder { |
| 16 | + private hardware: Hardware | null = null; |
| 17 | + private software: Software | null = null; |
| 18 | + private network: Network | null = null; |
| 19 | + private resources: Resources | null = null; |
| 20 | + |
| 21 | + setHardware( |
| 22 | + deviceType: DeviceType, |
| 23 | + processor: Processor, |
| 24 | + memory: Memory, |
| 25 | + disk: Disk |
| 26 | + ): ResourcesBuilder { |
| 27 | + throwIfNotNull(this.hardware, 'Hardware is already set'); |
| 28 | + this.hardware = new Hardware(deviceType, memory, disk, processor); |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + setSoftware( |
| 33 | + operatingSystem: OperatingSystem, |
| 34 | + supportedTasks: Set<SupportedTask> |
| 35 | + ): ResourcesBuilder { |
| 36 | + throwIfNotNull(this.software, 'Software is already set'); |
| 37 | + this.software = new Software(supportedTasks, operatingSystem); |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + setNetwork( |
| 42 | + connectionMedium: ConnectionMedium, |
| 43 | + networkMetrics: NetworkMetrics |
| 44 | + ): ResourcesBuilder { |
| 45 | + throwIfNotNull(this.network, 'Network is already set'); |
| 46 | + this.network = new Network(connectionMedium, networkMetrics); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + build(): Resources { |
| 51 | + throwIfNotNull( |
| 52 | + this.resources, |
| 53 | + 'A Resources instance has already been built' |
| 54 | + ); |
| 55 | + if ( |
| 56 | + this.hardware !== null && |
| 57 | + this.software !== null && |
| 58 | + this.network !== null |
| 59 | + ) { |
| 60 | + this.resources = new Resources( |
| 61 | + this.hardware, |
| 62 | + this.software, |
| 63 | + this.network |
| 64 | + ); |
| 65 | + return this.resources; |
| 66 | + } else { |
| 67 | + throw new Error('Incorrect build process'); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments