Commit 3fe9a55b authored by mey's avatar mey
Browse files

Kernel.java: added add method

    added hashCode and equals
    updated test
parent 5ce256fe
package de.zmt.pathfinding.filter;
import java.io.Serializable;
import java.util.Arrays;
/**
* Immutable Kernel for operation on double grids. A similar class already
......@@ -53,7 +54,22 @@ public class Kernel implements Serializable {
}
/**
* Multiplies all weights by {@code scalar} and return result with a new
* Adds {@code value} to all weights and return results within a new object.
*
* @param value
* @return new kernel object with added {@code value}
*/
public Kernel add(double value) {
double[] addedWeights = new double[weights.length];
for (int i = 0; i < weights.length; i++) {
addedWeights[i] = weights[i] + value;
}
return new Kernel(width, height, addedWeights);
}
/**
* Multiplies all weights by {@code scalar} and return results within a new
* object.
*
* @param scalar
......@@ -120,6 +136,36 @@ public class Kernel implements Serializable {
return weights;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(weights);
result = prime * result + width;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Kernel other = (Kernel) obj;
if (!Arrays.equals(weights, other.weights)) {
return false;
}
if (width != other.width) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
......
......@@ -10,12 +10,13 @@ import org.junit.Test;
public class KernelTest {
private static final int KERNEL_EXTENT = 3;
private static final double MAX_ERROR = 1E-14d;
private static final double DOUBLE_SUM = KERNEL_EXTENT * KERNEL_EXTENT * 2d;
private Kernel kernel;
@Before
public void setUp() throws Exception {
kernel = KernelFactory.createConstant(3, KERNEL_EXTENT);
kernel = KernelFactory.createConstant(KERNEL_EXTENT, KERNEL_EXTENT);
}
@Test
......@@ -28,4 +29,17 @@ public class KernelTest {
assertThat(kernel.normalize().sum(), is(closeTo(1, MAX_ERROR)));
}
@Test
public void add() {
assertThat(kernel.add(1).sum(), is(DOUBLE_SUM));
}
@Test
public void multiply() {
assertThat(kernel.multiply(2).sum(), is(DOUBLE_SUM));
}
public void equals() {
assertThat(new Kernel(1, 1, new double[] { 1 }), is(new Kernel(1, 1, new double[] { 1 })));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment