Commit 82bff2a3 authored by Nico Mack's avatar Nico Mack

Changes required by changes to Tetherable interface

Bug fixes in Marker and BaseWidget compareTo, equals and hash method
parent 62ace793
......@@ -17,6 +17,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
// ***************************************************************************
// * Class Definition and Members *
......@@ -39,6 +40,8 @@ public abstract class Marker implements Positionable {
// * Constants *
// ***************************************************************************
private static final int HASH_SEED = 23;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......@@ -264,6 +267,23 @@ public abstract class Marker implements Positionable {
}
}
// ---------------------------------------------------------------------------
@Override
public boolean equals(Object o) {
if ((o != null) && (o instanceof Marker)) {
return this.getHandleId().equals(((Marker) o).getHandleId());
}
return false;
}
// ---------------------------------------------------------------------------
@Override
public int hashCode() {
return Objects.hash(HASH_SEED, this.getHandleId());
}
// ---------------------------------------------------------------------------
/**
* Comparison as defined by the {@link Comparable} interface. This comparison is based on the
......@@ -279,8 +299,15 @@ public abstract class Marker implements Positionable {
@Override
public int compareTo(Positionable o) {
int comparison = 1;
if ((o != null) && (o instanceof Marker)) {
if (o != null) {
comparison = this.getHandleId().compareTo(o.getHandleId());
if (comparison == 0) {
Point mine = this.getPosition();
Point his = o.getPosition();
if (mine != null) {
comparison *= mine.compareTo(his);
}
}
}
return comparison;
}
......
......@@ -180,6 +180,13 @@ public abstract class TetherableMarker extends Marker implements Tetherable {
// ---------------------------------------------------------------------------
@Override
public void removePotentialTether(Tetherable potential, boolean propagate) {
this.tetherManager.removePotentialTether(potential, propagate);
}
// ---------------------------------------------------------------------------
@Override
public void tetherWith(Tetherable otherTetherable) {
this.tetherManager.tetherWith(otherTetherable);
......
......@@ -43,6 +43,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
/**
......@@ -89,6 +90,8 @@ public class BaseWidget implements Positionable, Touchable {
protected Boolean isTouchable = null;
private static final int HASH_SEED = 17;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
......@@ -678,14 +681,40 @@ public class BaseWidget implements Positionable, Touchable {
@Override
public int compareTo(Positionable o) {
int comparison = 1;
if ((o != null) && (o instanceof BaseWidget)) {
if (o != null) {
comparison = this.getHandleId().compareTo(o.getHandleId());
if (comparison == 0) {
Point mine = this.getPosition();
Point his = o.getPosition();
if (mine != null) {
comparison *= mine.compareTo(his);
}
}
}
return comparison;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public boolean equals(Object o) {
if ((o != null) && (o instanceof BaseWidget)) {
return Objects.equals(this.getHandleId(), ((BaseWidget) o).getHandleId());
}
return false;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public int hashCode() {
return Objects.hash(HASH_SEED, this.getHandleId());
}
// ---------------------------------------------------------------------------
@Override
public BaseWidget clone() {
return new BaseWidget(this);
......
......@@ -246,6 +246,17 @@ public abstract class TetherableWidget extends PointingWidget implements Tethera
*/
// ---------------------------------------------------------------------------
@Override
public void removePotentialTether(Tetherable potential, boolean propagate) {
this.tetherManager.removePotentialTether(potential, propagate);
}
// ---------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
// ---------------------------------------------------------------------------
@Override
public void tetherWith(Tetherable otherTetherable) {
this.tetherManager.tetherWith(otherTetherable);
......
......@@ -111,8 +111,9 @@ public class ForceManager implements Runnable {
double magnitude = generated.getMagnitude();
boolean isRepelling = this.generator.isRepelling(subject);
Point source = origin.clone().toCoordinates(ScreenCoordinates.class);
Point target = subject.getPosition().clone().toCoordinates(ScreenCoordinates.class);
Point distance = origin.clone().subtract(target, false);
Point distance = source.subtract(target, false);
double radius = distance.magnitude();
Vector2D force = null;
......
......@@ -500,6 +500,25 @@ public class TetherManager {
}
}
// ---------------------------------------------------------------------------
/**
* @param potential
* @param propagate
*/
// ---------------------------------------------------------------------------
public void removePotentialTether(Tetherable potential, boolean propagate) {
Preconditions.checkNotNull(potential, "Potential Tetherable MUST not be null!"); //$NON-NLS-1$
if (this.potentialTethers.containsKey(potential)) {
this.separateFrom(potential);
this.potentialTethers.remove(potential);
if (propagate)
potential.removePotentialTether(this.tetherable, false);
} else {
LOGGER.warn("Attempt to remove an unknown tetherable!"); //$NON-NLS-1$
}
}
// ---------------------------------------------------------------------------
/**
* Setups tethering between this object and the specified candidate. It is important that the
......
......@@ -38,6 +38,8 @@ public interface Tetherable {
public abstract void addPotentialTether(Tetherable potential, Tether tether, boolean propagate);
public abstract void removePotentialTether(Tetherable potential, boolean propagate);
public abstract double getTetheringDistance();
public abstract Point getTetherOrigin();
......
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