Skip to content

Commit

Permalink
Implement b04/files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Kanister committed Nov 24, 2024
1 parent 8f293fc commit 88786c4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/adap-b04/files/Directory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { IllegalArgumentException } from "../common/IllegalArgumentException";
import { Node } from "./Node";

export class Directory extends Node {

protected childNodes: Set<Node> = 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
}

Expand Down
15 changes: 13 additions & 2 deletions src/adap-b04/files/File.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Node } from "./Node";
import { Directory } from "./Directory";
import { IllegalArgumentException } from "../common/IllegalArgumentException";

enum FileState {
OPEN,
Expand All @@ -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
}

Expand Down
17 changes: 16 additions & 1 deletion src/adap-b04/files/Link.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -18,6 +23,9 @@ export class Link extends Node {
}

public setTargetNode(target: Node): void {
// precondition
IllegalArgumentException.assertIsNotNullOrUndefined(target);

this.targetNode = target;
}

Expand All @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/adap-b04/files/Node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IllegalArgumentException } from "../common/IllegalArgumentException";
import { Name } from "../names/Name";
import { Directory } from "./Directory";

Expand All @@ -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);
}
Expand All @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/adap-b04/files/RootNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IllegalArgumentException } from "../common/IllegalArgumentException";
import { Name } from "../names/Name";
import { StringName } from "../names/StringName";
import { Directory } from "./Directory";
Expand All @@ -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
}

Expand Down

0 comments on commit 88786c4

Please sign in to comment.