Skip to content

Commit

Permalink
Updated to B06
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkriehle committed Dec 2, 2024
1 parent e42a56b commit 78e0b75
Show file tree
Hide file tree
Showing 47 changed files with 339 additions and 333 deletions.
2 changes: 1 addition & 1 deletion src/adap-b02/names/StringArrayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class StringArrayName implements Name {
protected delimiter: string = DEFAULT_DELIMITER;
protected components: string[] = [];

constructor(other: string[], delimiter?: string) {
constructor(source: string[], delimiter?: string) {
throw new Error("needs implementation or deletion");
}

Expand Down
2 changes: 1 addition & 1 deletion src/adap-b02/names/StringName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class StringName implements Name {
protected name: string = "";
protected noComponents: number = 0;

constructor(other: string, delimiter?: string) {
constructor(source: string, delimiter?: string) {
throw new Error("needs implementation or deletion");
}

Expand Down
2 changes: 1 addition & 1 deletion src/adap-b03/names/StringArrayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class StringArrayName extends AbstractName {

protected components: string[] = [];

constructor(other: string[], delimiter?: string) {
constructor(source: string[], delimiter?: string) {
super();
throw new Error("needs implementation or deletion");
}
Expand Down
2 changes: 1 addition & 1 deletion src/adap-b03/names/StringName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class StringName extends AbstractName {
protected name: string = "";
protected noComponents: number = 0;

constructor(other: string, delimiter?: string) {
constructor(source: string, delimiter?: string) {
super();
throw new Error("needs implementation or deletion");
}
Expand Down
32 changes: 0 additions & 32 deletions src/adap-b04/common/AssertionDispatcher.ts

This file was deleted.

16 changes: 1 addition & 15 deletions src/adap-b04/common/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ export abstract class Exception extends Error {

protected trigger: Exception | null = null;

static isNullOrUndefined(o: Object | null) {
return (o == undefined) || (o == null);
}

constructor(m: string, t?: Exception) {
super(m);

Expand All @@ -24,18 +20,8 @@ export abstract class Exception extends Error {
}

public getTrigger(): Exception {
this.assertHasTrigger(); // should be InvalidStateException
// @todo check if trigger is null
return this.trigger as Exception;
}

protected assertHasTrigger(): void {
if (!this.hasTrigger()) {
throw new (class extends Exception {
constructor(t: Exception) {
super("exception had no trigger", t);
}
})(this);
}
}

}
8 changes: 2 additions & 6 deletions src/adap-b04/common/IllegalArgumentException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { InvalidStateException } from "./InvalidStateException";
*/
export class IllegalArgumentException extends Exception {

static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void {
this.assertCondition(!this.isNullOrUndefined(o), m);
}

static assertCondition(c: boolean, m: string = "illegal argument", t?: Exception): void {
public static assert(c: boolean, m: string = "illegal argument", t?: Exception): void {
if (!c) throw new IllegalArgumentException(m, t);
}

Expand All @@ -20,7 +16,7 @@ export class IllegalArgumentException extends Exception {
}

public getTrigger(): Exception {
InvalidStateException.assertCondition(this.hasTrigger());
InvalidStateException.assert(this.hasTrigger());
return super.getTrigger();
}

Expand Down
8 changes: 2 additions & 6 deletions src/adap-b04/common/InvalidStateException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import { Exception } from "./Exception";
* In other words, a class invariant failed.
*/
export class InvalidStateException extends Exception {

static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void {
this.assertCondition(!this.isNullOrUndefined(o), m, t);
}

static assertCondition(c: boolean, m: string = "invalid state", t?: Exception): void {

public static assert(c: boolean, m: string = "invalid state", t?: Exception): void {
if (!c) throw new InvalidStateException(m, t);
}

Expand Down
8 changes: 2 additions & 6 deletions src/adap-b04/common/MethodFailedException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import { Exception } from "./Exception";
* In other words, a postcondition failed.
*/
export class MethodFailedException extends Exception {

static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void {
this.assertCondition(!this.isNullOrUndefined(o), m, t);
}

static assertCondition(c: boolean, m: string = "method failed", t?: Exception): void {

public static assert(c: boolean, m: string = "method failed", t?: Exception): void {
if (!c) throw new MethodFailedException(m, t);
}

Expand Down
67 changes: 12 additions & 55 deletions src/adap-b04/coordinates/AbstractCoordinate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExceptionType } from "../common/AssertionDispatcher";
import { AssertionDispatcher } from "../common/AssertionDispatcher";
import { IllegalArgumentException } from "../common/IllegalArgumentException";
import { InvalidStateException } from "../common/InvalidStateException";
import { MethodFailedException } from "../common/MethodFailedException";

import { Coordinate } from "./Coordinate";

export abstract class AbstractCoordinate implements Coordinate {
Expand All @@ -17,8 +17,6 @@ export abstract class AbstractCoordinate implements Coordinate {
}

public isEqual(other: Coordinate): boolean {
this.assertIsNotNullOrUndefinedAsPrecondition(other);

return (this.doGetX() == other.getX()) && (this.doGetY() == other.getY());
}

Expand All @@ -42,8 +40,6 @@ export abstract class AbstractCoordinate implements Coordinate {
protected abstract doGetX(): number;

public setX(x: number): void {
this.assertIsNotNullOrUndefinedAsPrecondition(x);

this.doSetX(x);
}

Expand All @@ -56,16 +52,12 @@ export abstract class AbstractCoordinate implements Coordinate {
protected abstract doGetY(): number;

public setY(y: number): void {
this.assertIsNotNullOrUndefinedAsPrecondition(y);

this.doSetY(y);
}

protected abstract doSetY(y: number): void;

public calcStraightLineDistance(other: Coordinate): number {
this.assertIsNotNullOrUndefinedAsPrecondition(other);

let deltaX: number = Math.abs(other.getX() - this.doGetX());
let deltaY: number = Math.abs(other.getY() - this.doGetY());
return Math.hypot(deltaX, deltaY);
Expand All @@ -78,9 +70,7 @@ export abstract class AbstractCoordinate implements Coordinate {
protected abstract doGetR(): number;

public setR(r: number): void {
this.assertIsNotNullOrUndefinedAsPrecondition(r);
this.assertIsValidRAsPrecondition(r);

IllegalArgumentException.assert(this.isValidR(r));
this.doSetR(r);
}

Expand All @@ -97,74 +87,41 @@ export abstract class AbstractCoordinate implements Coordinate {
* @param phi Angle of vector
*/
public setPhi(phi: number): void {
this.assertIsNotNullOrUndefinedAsPrecondition(phi);
this.assertIsValidPhiAsPrecondition(phi);
IllegalArgumentException.assert(this.isValidPhi(phi));

this.doSetPhi(phi);

const newPhi: number = this.doGetPhi();
this.assertIsValidPhiAsClassInvariant(newPhi);
InvalidStateException.assert(this.isValidPhi(newPhi));

MethodFailedException.assertCondition(newPhi == phi);
MethodFailedException.assert(newPhi == phi);
}

protected abstract doSetPhi(phi: number): void;

public calcGreatCircleDistance(other: Coordinate): number {
this.assertIsNotNullOrUndefinedAsPrecondition(other);

let lowerR = Math.min(this.getR(), other.getR());
let deltaPhi = Math.abs(other.getPhi() - this.getPhi());
return lowerR * deltaPhi;
}

public multiplyWith(other: Coordinate): void {
this.assertIsNotNullOrUndefinedAsPrecondition(other);

let newR = this.getR() * other.getR();
let newPhi = this.getPhi() + other.getPhi();
this.setR(newR);
this.setPhi(newPhi);
}

protected assertIsNotNullOrUndefinedAsPrecondition(other: Object): void {
this.assertIsNotNullOrUndefined(other, ExceptionType.PRECONDITION);
}

protected assertIsNotNullOrUndefined(other: Object, et: ExceptionType): void {
let condition: boolean = !IllegalArgumentException.isNullOrUndefined(other);
AssertionDispatcher.dispatch(et, condition, "null or undefined value");
}

protected assertIsValidRAsPrecondition(r: number): void {
this.assertIsValidR(r, ExceptionType.PRECONDITION);
}

protected assertIsValidR(r: number, et: ExceptionType): void {
let condition: boolean = (r >= 0);
AssertionDispatcher.dispatch(et, condition, "invalid r value");
}

protected assertIsValidPhiAsPrecondition(phi: number): void {
this.assertIsValidPhi(phi, ExceptionType.PRECONDITION);
}

protected assertIsValidPhiAsClassInvariant(phi: number): void {
this.assertIsValidPhi(phi, ExceptionType.CLASS_INVARIANT);
}

protected assertIsValidPhi(phi: number, et: ExceptionType): void {
let condition: boolean = (phi < 0) || (phi >= 2*Math.PI);
AssertionDispatcher.dispatch(et, condition, "invalid phi value");
protected isValidR(r: number): boolean {
return r >= 0;
}

protected assertIsValidDelCharAsPrecondition(d: string): void {
this.assertIsValidDelChar(d, ExceptionType.PRECONDITION);
protected isValidPhi(phi: number): boolean {
return (phi >= 0) && (phi < 2*Math.PI);
}

protected assertIsValidDelChar(d: string, et: ExceptionType): void {
let condition: boolean = (d.length == 1);
AssertionDispatcher.dispatch(et, condition, "invalid delimiter character");
protected isValidDelChar(d: string): boolean {
return d.length == 1;
}

}
1 change: 0 additions & 1 deletion src/adap-b04/coordinates/Coordinate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Equality } from "../common/Equality";
import { Cloneable } from "../common/Cloneable";
import { Printable } from "../common/Printable";

/**
* A coordinate (here) is a point in a two-dimensional coordinate system.
Expand Down
8 changes: 6 additions & 2 deletions src/adap-b04/files/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export class Directory extends Node {
super(bn, pn);
}

public add(cn: Node): void {
public hasChildNode(cn: Node): boolean {
return this.childNodes.has(cn);
}

public addChildNode(cn: Node): void {
this.childNodes.add(cn);
}

public remove(cn: Node): void {
public removeChildNode(cn: Node): void {
this.childNodes.delete(cn); // Yikes! Should have been called remove
}

Expand Down
6 changes: 3 additions & 3 deletions src/adap-b04/files/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export class Node {

protected initialize(pn: Directory): void {
this.parentNode = pn;
this.parentNode.add(this);
this.parentNode.addChildNode(this);
}

public move(to: Directory): void {
this.parentNode.remove(this);
to.add(this);
this.parentNode.removeChildNode(this);
to.addChildNode(this);
this.parentNode = to;
}

Expand Down
2 changes: 1 addition & 1 deletion src/adap-b04/names/StringArrayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class StringArrayName extends AbstractName {

protected components: string[] = [];

constructor(other: string[], delimiter?: string) {
constructor(source: string[], delimiter?: string) {
super();
throw new Error("needs implementation or deletion");
}
Expand Down
2 changes: 1 addition & 1 deletion src/adap-b04/names/StringName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class StringName extends AbstractName {
protected name: string = "";
protected noComponents: number = 0;

constructor(other: string, delimiter?: string) {
constructor(source: string, delimiter?: string) {
super();
throw new Error("needs implementation or deletion");
}
Expand Down
32 changes: 0 additions & 32 deletions src/adap-b05/common/AssertionDispatcher.ts

This file was deleted.

Loading

0 comments on commit 78e0b75

Please sign in to comment.