diff --git a/src/adap-b04/files/Directory.ts b/src/adap-b04/files/Directory.ts index b1179030..9b6ff3a7 100644 --- a/src/adap-b04/files/Directory.ts +++ b/src/adap-b04/files/Directory.ts @@ -1,3 +1,4 @@ +import { IllegalArgumentException } from "../common/IllegalArgumentException"; import { Node } from "./Node"; export class Directory extends Node { @@ -5,14 +6,24 @@ export class Directory extends Node { protected childNodes: Set = new Set(); constructor(bn: string, pn: Directory) { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(pn); + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + super(bn, pn); } public add(cn: Node): void { + // precondition + IllegalArgumentException.assertCondition(!this.childNodes.has(cn), "Node mustn't exist to add it."); + this.childNodes.add(cn); } public remove(cn: Node): void { + // precondition + IllegalArgumentException.assertCondition(this.childNodes.has(cn), "Node must exist to delete it."); + this.childNodes.delete(cn); // Yikes! Should have been called remove } diff --git a/src/adap-b04/files/File.ts b/src/adap-b04/files/File.ts index 0e7aa84a..dbc5665c 100644 --- a/src/adap-b04/files/File.ts +++ b/src/adap-b04/files/File.ts @@ -1,5 +1,6 @@ import { Node } from "./Node"; import { Directory } from "./Directory"; +import { IllegalArgumentException } from "../common/IllegalArgumentException"; enum FileState { OPEN, @@ -11,15 +12,25 @@ export class File extends Node { protected state: FileState = FileState.CLOSED; - constructor(baseName: string, parent: Directory) { - super(baseName, parent); + constructor(bn: string, pn: Directory) { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(pn); + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + + super(bn, pn); } public open(): void { + // precondition + IllegalArgumentException.assertCondition(this.doGetFileState() === FileState.CLOSED, "File must be closed to open it."); + // do something } public close(): void { + // precondition + IllegalArgumentException.assertCondition(this.doGetFileState() === FileState.OPEN, "File must be open to close it."); + // do something } diff --git a/src/adap-b04/files/Link.ts b/src/adap-b04/files/Link.ts index e240325f..07e00430 100644 --- a/src/adap-b04/files/Link.ts +++ b/src/adap-b04/files/Link.ts @@ -1,11 +1,16 @@ import { Node } from "./Node"; import { Directory } from "./Directory"; +import { IllegalArgumentException } from "../common/IllegalArgumentException"; export class Link extends Node { protected targetNode: Node | null = null; constructor(bn: string, pn: Directory, tn?: Node) { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(pn); + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + super(bn, pn); if (tn != undefined) { @@ -18,6 +23,9 @@ export class Link extends Node { } public setTargetNode(target: Node): void { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(target); + this.targetNode = target; } @@ -27,12 +35,19 @@ export class Link extends Node { } public rename(bn: string): void { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(bn); + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + const target = this.ensureTargetNode(this.targetNode); target.rename(bn); } protected ensureTargetNode(target: Node | null): Node { - const result: Node = this.targetNode as Node; + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(target); + + const result: Node = target as Node; return result; } } \ No newline at end of file diff --git a/src/adap-b04/files/Node.ts b/src/adap-b04/files/Node.ts index aa09c999..80cbfdae 100644 --- a/src/adap-b04/files/Node.ts +++ b/src/adap-b04/files/Node.ts @@ -1,3 +1,4 @@ +import { IllegalArgumentException } from "../common/IllegalArgumentException"; import { Name } from "../names/Name"; import { Directory } from "./Directory"; @@ -7,11 +8,18 @@ export class Node { protected parentNode: Directory; constructor(bn: string, pn: Directory) { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(pn); + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + this.doSetBaseName(bn); this.parentNode = pn; } public move(to: Directory): void { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(to); + this.parentNode.remove(this); to.add(this); } @@ -31,10 +39,16 @@ export class Node { } public rename(bn: string): void { + // precondition + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + this.doSetBaseName(bn); } protected doSetBaseName(bn: string): void { + // precondition + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + this.baseName = bn; } diff --git a/src/adap-b04/files/RootNode.ts b/src/adap-b04/files/RootNode.ts index cff22b8d..104828d3 100644 --- a/src/adap-b04/files/RootNode.ts +++ b/src/adap-b04/files/RootNode.ts @@ -1,3 +1,4 @@ +import { IllegalArgumentException } from "../common/IllegalArgumentException"; import { Name } from "../names/Name"; import { StringName } from "../names/StringName"; import { Directory } from "./Directory"; @@ -20,10 +21,16 @@ export class RootNode extends Directory { } public move(to: Directory): void { + // precondition + IllegalArgumentException.assertIsNotNullOrUndefined(to); + // null operation } protected doSetBaseName(bn: string): void { + // precondition + IllegalArgumentException.assertCondition(typeof bn === "string" && bn.length > 0, "basename must be at least one character long."); + // null operation }