Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Kanister committed Dec 8, 2024
2 parents 4c6d3cb + 78e0b75 commit f0a3dbe
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/adap-b06/common/Cloneable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export interface Cloneable {
/**
* Returns shallow copy (clone) of this object
*/
clone(): Object; // @todo Use object
clone(): Object; // @todo Clarify use of Objects vs object

}
3 changes: 2 additions & 1 deletion src/adap-b06/common/Equality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ export interface Equality {
* Returns true if other object is of equal value to this one
* @param other Object to compare with
*/
isEqual(other: Object): boolean; // @todo How to ensure this works with vitest and Object.is?
isEqual(other: Object): boolean;

/**
* Returns hashcode for this object, respecting equality contract
*/
getHashCode(): number;

}
19 changes: 15 additions & 4 deletions src/adap-b06/common/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
*/
export abstract class Exception extends Error {

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

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

if (t != undefined) {
this.trigger = t;
}
}

public hasTrigger(): boolean {
return this.trigger != null;
}

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

}
15 changes: 6 additions & 9 deletions src/adap-b06/common/IllegalArgumentException.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { Exception } from "./Exception";
import { InvalidStateException } from "./InvalidStateException";

/**
* An IllegalArgumentException signals an invalid argument.
* In other words, a method precondition failed.
*/
export class IllegalArgumentException extends Exception {

static assertIsNotNullOrUndefined(o: Object | null, exMsg: string = "null or undefined"): void {
this.assertCondition(this.isNullOrUndefined(o), exMsg);
public static assert(c: boolean, m: string = "illegal argument", t?: Exception): void {
if (!c) throw new IllegalArgumentException(m, t);
}

static assertCondition(cond: boolean, exMsg: string): void {
if (!cond) throw new IllegalArgumentException(exMsg);
constructor(m: string, t?: Exception) {
super(m, t);
}

constructor(m: string) {
super(m);
}


}
14 changes: 5 additions & 9 deletions src/adap-b06/common/InvalidStateException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import { Exception } from "./Exception";
* In other words, a class invariant failed.
*/
export class InvalidStateException extends Exception {

static assertNotNullOrUndefined(o: Object | null, exMsg: string = "null or undefined"): void {
this.assertCondition(this.isNullOrUndefined(o), exMsg);
}

static assertCondition(cond: boolean, exMsg: string): void {
if (!cond) throw new InvalidStateException(exMsg);

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

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

}
14 changes: 5 additions & 9 deletions src/adap-b06/common/MethodFailedException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import { Exception } from "./Exception";
* In other words, a postcondition failed.
*/
export class MethodFailedException extends Exception {

static assertNotNullOrUndefined(o: Object | null, exMsg: string = "null or undefined"): void {
this.assertCondition(this.isNullOrUndefined(o), exMsg);
}

static assertCondition(cond: boolean, exMsg: string): void {
if (!cond) throw new MethodFailedException(exMsg);

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

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

}
29 changes: 21 additions & 8 deletions src/adap-b06/common/Printable.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
export const DEFAULT_DELIMITER: string = '.';
export const ESCAPE_CHARACTER = '\\';

export interface Printable {

/**
* Returns human-readable representation of this object
* Expects that delimiter is a single character
/**
* Returns a human-readable representation of the Name instance using user-set control characters
* Control characters are not escaped (creating a human-readable string)
* Users can vary the delimiter character to be used
* The delimiter character must be a single character
*/
asString(delChar?: string): string;
asString(delimiter?: string): string;

/**
* Returns machine-readable representation of this object
*/
asDataString(): string;
/**
* Returns a machine-readable representation of Name instance using default control characters
* Machine-readable means that from a data string, a Name can be parsed back in
* The control characters in the data string are the default characters
* Different fields of the object are separated by the delimiter character
*/
asDataString(): string;

/**
* Returns delimiter char; must be a single character
*/
getDelimiterCharacter(): string;

}
17 changes: 17 additions & 0 deletions src/adap-b06/common/ServiceFailureException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Exception } from "./Exception";

/**
* A ServiceFailureException signals that a service failed to provide its service.
* ServiceFailureExceptions must be checked for by the client after the service call.
*/
export class ServiceFailureException extends Exception {

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

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

}
58 changes: 19 additions & 39 deletions src/adap-b06/coordinates/AbstractCoordinate.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import { IllegalArgumentException } from "../common/IllegalArgumentException";

import { Coordinate } from "./Coordinate";
import { PolarCoordinate } from "./PolarCoordinate";

export abstract class AbstractCoordinate implements Coordinate {

public asString(delChar: string) {
this.assertIsValidDelChar(delChar);
public clone(): Coordinate {
return this.doCreate(this.doGetX(), this.doGetY());
}

protected abstract doCreate(x: number, y: number): Coordinate;

public asString(delChar: string) {
IllegalArgumentException.assert(this.isValidDelChar(delChar));
return this.doGetX() + delChar + this.doGetY();
}

public toString() {
return this.asDataString();
}

public asDataString(): string {
return this.asString("#");
}
public abstract asDataString(): string;

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

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

public getHashCode(): number {
let hashCode: number = 0;
const s: string = this.asDataString();
for (let i = 0; i < s.length; i++) {
let c = s.charCodeAt(i);
for (let i: number = 0; i < s.length; i++) {
let c: number = s.charCodeAt(i);
hashCode = (hashCode << 5) - hashCode + c;
hashCode |= 0;
}
return hashCode;
}

public clone(): Coordinate {
return { ...this };
}

abstract getOrigin(): Coordinate;

public getX(): number {
Expand All @@ -48,8 +45,6 @@ export abstract class AbstractCoordinate implements Coordinate {
protected abstract doGetX(): number;

public setX(x: number): Coordinate {
this.assertIsNotNullOrUndefined(x);

return this.doSetX(x);
}

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

public setY(y: number): Coordinate {
this.assertIsNotNullOrUndefined(y);

return this.doSetY(y);
}

protected abstract doSetY(y: number): Coordinate;

public calcStraightLineDistance(other: Coordinate): number {
this.assertIsNotNullOrUndefined(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 @@ -84,8 +75,6 @@ export abstract class AbstractCoordinate implements Coordinate {
protected abstract doGetR(): number;

public setR(r: number): Coordinate {
this.assertIsNotNullOrUndefined(r);

return this.doSetR(r);
}

Expand All @@ -98,43 +87,34 @@ export abstract class AbstractCoordinate implements Coordinate {
protected abstract doGetPhi(): number;

public setPhi(phi: number): Coordinate {
this.assertIsNotNullOrUndefined(phi);
this.assertIsValidPhi(phi);

IllegalArgumentException.assert(this.isValidPhi(phi));
return this.doSetPhi(phi);
}

protected abstract doSetPhi(phi: number): Coordinate;

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

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

public multiplyWith(other: Coordinate): Coordinate {
this.assertIsNotNullOrUndefined(other);

let newR = this.getR() * other.getR();
let newPhi = this.getPhi() + other.getPhi();
return new PolarCoordinate(newR, newPhi);
return this.doCreate(newR, newPhi);
}

protected assertIsNotNullOrUndefined(other: Object): void {
let condition: boolean = !IllegalArgumentException.isNullOrUndefined(other);
IllegalArgumentException.assertCondition(condition, "null or undefined argument");
protected isValidR(r: number): boolean {
return r >= 0;
}

protected assertIsValidPhi(phi: number): void {
let condition: boolean = (phi < 0) || (phi >= 2*Math.PI);
IllegalArgumentException.assertCondition(condition, "invalid phi value");
protected isValidPhi(phi: number): boolean {
return (phi >= 0) && (phi < 2*Math.PI);
}

protected assertIsValidDelChar(d: string) {
let condition: boolean = (d.length == 1);
IllegalArgumentException.assertCondition(condition, "invalid delimiter character");
protected isValidDelChar(d: string): boolean {
return d.length == 1;
}

}
12 changes: 10 additions & 2 deletions src/adap-b06/coordinates/CartesianCoordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ export class CartesianCoordinate extends AbstractCoordinate {

protected initialize(x?: number, y?: number): void {
if (x != undefined) {
this.doSetX(x);
this.x = x;
}

if (y != undefined) {
this.doSetY(y);
this.y = y;
}
}

protected doCreate(x: number, y: number): Coordinate {
return new CartesianCoordinate(x, y);
}

public asDataString(): string {
return this.doGetX() + '#' + this.doGetY();
}

public getOrigin(): Coordinate {
return new CartesianCoordinate(0, 0);
}
Expand Down
3 changes: 1 addition & 2 deletions src/adap-b06/coordinates/Coordinate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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.
* The coordinate system may be cartesian or polar; coordinates should be interchangeable.
*/
export interface Coordinate extends Equality, Cloneable, Printable {
export interface Coordinate extends Cloneable, Equality {

/**
* Returns the origin of the coordinate system
Expand Down
12 changes: 10 additions & 2 deletions src/adap-b06/coordinates/PolarCoordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ export class PolarCoordinate extends AbstractCoordinate {

protected initialize(r?: number, phi?: number): void {
if (r != undefined) {
this.setR(r);
this.r = r;
}

if (phi != undefined) {
this.setPhi(phi);
this.phi = phi;
}
}

protected doCreate(r: number, phi: number): Coordinate {
return new PolarCoordinate(r, phi);
}

public asDataString(): string {
return this.doGetR() + '#' + this.doGetPhi();
}

public getOrigin(): Coordinate {
return new PolarCoordinate(0, 0);
}
Expand Down
3 changes: 3 additions & 0 deletions src/adap-b06/examples/Functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function calculateDuration1(distance: number, speed: number): number {
return distance / speed;
}
Loading

0 comments on commit f0a3dbe

Please sign in to comment.