add comments to java impl

This commit is contained in:
jacekpoz 2024-04-12 14:08:11 +02:00
parent 14cd3d8044
commit 4ed9cdf46a
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -15,18 +15,30 @@ public class GF {
this.value = value; this.value = value;
} }
/*
* `this` + `rhs`
* throws IllegalArgumentException on different characteristics
*/
public GF add(final GF rhs) throws IllegalArgumentException { public GF add(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
return new GF(this.characteristic, (this.value + rhs.value) % this.characteristic); return new GF(this.characteristic, (this.value + rhs.value) % this.characteristic);
} }
/*
* `this` += `rhs`
* throws IllegalArgumentException on different characteristics
*/
public void addAssign(final GF rhs) throws IllegalArgumentException { public void addAssign(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
this.value = this.add(rhs).value; this.value = this.add(rhs).value;
} }
/*
* `this` - `rhs`
* throws IllegalArgumentException on different characteristics
*/
public GF subtract(final GF rhs) throws IllegalArgumentException { public GF subtract(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
if (this.value < rhs.value) { if (this.value < rhs.value) {
@ -35,24 +47,41 @@ public class GF {
return new GF((this.value - rhs.value), this.characteristic); return new GF((this.value - rhs.value), this.characteristic);
} }
/*
* `this` -= `rhs`
* throws IllegalArgumentException on different characteristics
*/
public void subtractAssign(final GF rhs) throws IllegalArgumentException { public void subtractAssign(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
this.value = this.subtract(rhs).value; this.value = this.subtract(rhs).value;
} }
/*
* `this` * `rhs`
* throws IllegalArgumentException on different characteristics
*/
public GF multiply(final GF rhs) throws IllegalArgumentException { public GF multiply(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
return new GF(this.characteristic, (this.value * rhs.value) % this.characteristic); return new GF(this.characteristic, (this.value * rhs.value) % this.characteristic);
} }
/*
* `this` *= `rhs`
* throws IllegalArgumentException on different characteristics
*/
public void multiplyAssign(final GF rhs) throws IllegalArgumentException { public void multiplyAssign(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
this.value = this.multiply(rhs).value; this.value = this.multiply(rhs).value;
} }
/*
* `this` / `rhs`
* throws IllegalArgumentException on division by 0
* throws IllegalArgumentException on different characteristics
*/
public GF divide(final GF rhs) throws IllegalArgumentException { public GF divide(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
if (rhs.value == 0) { if (rhs.value == 0) {
@ -71,16 +100,28 @@ public class GF {
return this.multiply(inverse); return this.multiply(inverse);
} }
/*
* `this` /= `rhs`
* throws IllegalArgumentException on different characteristics
*/
public void divideAssign(final GF rhs) throws IllegalArgumentException { public void divideAssign(final GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);
this.value = this.divide(rhs).value; this.value = this.divide(rhs).value;
} }
/*
* returns the characteristic of `this`
* throws IllegalArgumentException on different characteristics
*/
public int getCharacteristic() { public int getCharacteristic() {
return this.characteristic; return this.characteristic;
} }
/*
* returns the value of `this`
* throws IllegalArgumentException on different characteristics
*/
public int getValue() { public int getValue() {
return this.value; return this.value;
} }
@ -98,6 +139,10 @@ public class GF {
return false; return false;
} }
/*
* compares `this` and `rhs`
* throws IllegalArgumentException on different characteristics
*/
public int compare(GF rhs) throws IllegalArgumentException { public int compare(GF rhs) throws IllegalArgumentException {
verifyCharacteristics(this, rhs); verifyCharacteristics(this, rhs);