@Override public synchronized double valueIfReported(double d) { if((d < 0.0) || (d > 1.0)) throw new IllegalArgumentException("Not a valid normalized key: " + d); double superValue = avg.currentValue(); double thisValue = Location.normalize(superValue); double diff = Location.change(thisValue, d); return Location.normalize(avg.valueIfReported(superValue + diff)); }
private double trivialFindClosestDistance(double[] locs, double l) { double minDist = Double.POSITIVE_INFINITY; for (int i = 0; i < locs.length; i++) { final double d = Location.distance(locs[i], l); if (d < minDist) { minDist = d; } } return minDist; }
/** * Tests for equality of two locations. * Locations are considered equal if their distance is (almost) zero, e.g. equals(0.0, 1.0) is true, * or if both locations are invalid. * @param a any location * @param b any location * @return whether the two locations are considered equal */ public static boolean equals(double a, double b) { return distanceAllowInvalid(a, b) < Double.MIN_VALUE * 2; }
/** * Distance between two potentially invalid locations. * @param a a valid location * @param b a valid location * @return the absolute distance between the locations in the circular location space. * Invalid locations are considered to be at 2.0, and the result is returned accordingly. */ public static double distanceAllowInvalid(double a, double b) { if (!isValid(a) && !isValid(b)) { return 0.0; // Both are out of range so both are equal. } if (!isValid(a)) { return 2.0 - b; } if (!isValid(b)) { return 2.0 - a; } // Both values are valid. return simpleDistance(a, b); }
public LinkLengthClass linkLengthClass() { if(!Location.isValid(getLocation())) { Logger.error(this, "No location on "+this, new Exception("debug")); return LinkLengthClass.SHORT; // FIXME add unknown to enum? Would need more complex error handling... } // FIXME OPTIMISE This should not change since we don't swap on opennet. if(Location.distance(this, opennet.node.getLocation()) > OpennetManager.LONG_DISTANCE) return LinkLengthClass.LONG; else return LinkLengthClass.SHORT; }
public synchronized boolean isValidLocation() { return Location.isValid(currentLocation); }
double newLoc = Location.getLocation(locationString); if (!Location.isValid(newLoc)) { if(logMINOR) Logger.minor(this, "Invalid or null location, waiting for FNPLocChangeNotification: locationString=" + locationString); } else { double oldLoc = location.setLocation(newLoc); if(!Location.equals(oldLoc, newLoc)) { if(!Location.isValid(oldLoc)) shouldUpdatePeerCounts = true; changedAnything = true;
boolean updateLocation(double newLoc, double[] newLocs) { if (!Location.isValid(newLoc)) { Logger.error(this, "Invalid location update for " + this+ " ("+newLoc+')', new Exception("error")); for (int i = 0; i < newLocs.length; i++) { final double loc = newLocs[i]; if (!Location.isValid(loc)) { Logger.error(this, "Invalid location update for " + this + " (" + loc + ")", new Exception("error")); if (!Location.equals(currentLocation, newLoc) || currentPeersLocation == null) { anythingChanged = true; if (!Location.equals(currentPeersLocation[i], newPeersLocation[i])) { anythingChanged = true; break;
public void testDistance() { // Simple cases. assertEquals(DIST_12, Location.distance(VALID_1, VALID_2), EPSILON); assertEquals(DIST_12, Location.distance(VALID_2, VALID_1), EPSILON); // Corner case. assertEquals(0.5, Location.distance(VALID_1, Location.normalize(VALID_1 + 0.5)), EPSILON); assertEquals(0.5, Location.distance(VALID_1, Location.normalize(VALID_1 - 0.5)), EPSILON); assertEquals(0.5, Location.distance(VALID_2, Location.normalize(VALID_2 + 0.5)), EPSILON); assertEquals(0.5, Location.distance(VALID_2, Location.normalize(VALID_2 - 0.5)), EPSILON); // Identity. assertEquals(0.0, Location.distance(VALID_1, VALID_1)); assertEquals(0.0, Location.distance(VALID_2, VALID_2)); }
public void testDistanceAllowInvalid() { assertEquals(DIST_12, Location.distanceAllowInvalid(VALID_1, VALID_2), EPSILON); assertEquals(DIST_12, Location.distanceAllowInvalid(VALID_2, VALID_1), EPSILON); assertEquals(0.5, Location.distanceAllowInvalid(VALID_1, Location.normalize(VALID_1 + 0.5)), EPSILON); assertEquals(0.5, Location.distanceAllowInvalid(VALID_1, Location.normalize(VALID_1 - 0.5)), EPSILON); assertEquals(0.5, Location.distanceAllowInvalid(VALID_2, Location.normalize(VALID_2 + 0.5)), EPSILON); assertEquals(0.5, Location.distanceAllowInvalid(VALID_2, Location.normalize(VALID_2 - 0.5)), EPSILON); assertEquals(0.0, Location.distanceAllowInvalid(VALID_1, VALID_1)); assertEquals(0.0, Location.distanceAllowInvalid(VALID_2, VALID_2)); assertEquals(2.0 - VALID_1, Location.distanceAllowInvalid(INVALID_1, VALID_1)); assertEquals(2.0 - VALID_1, Location.distanceAllowInvalid(VALID_1, INVALID_1)); assertEquals(2.0 - VALID_1, Location.distanceAllowInvalid(INVALID_2, VALID_1)); assertEquals(2.0 - VALID_1, Location.distanceAllowInvalid(VALID_1, INVALID_2)); assertEquals(2.0 - VALID_2, Location.distanceAllowInvalid(INVALID_1, VALID_2)); assertEquals(2.0 - VALID_2, Location.distanceAllowInvalid(VALID_2, INVALID_1)); assertEquals(2.0 - VALID_2, Location.distanceAllowInvalid(INVALID_2, VALID_2)); assertEquals(2.0 - VALID_2, Location.distanceAllowInvalid(VALID_2, INVALID_2)); assertEquals(0.0, Location.distanceAllowInvalid(INVALID_1, INVALID_1)); assertEquals(0.0, Location.distanceAllowInvalid(INVALID_1, INVALID_2)); assertEquals(0.0, Location.distanceAllowInvalid(INVALID_2, INVALID_1)); assertEquals(0.0, Location.distanceAllowInvalid(INVALID_2, INVALID_2));
synchronized double setLocation(double newLoc) { double oldLoc = currentLocation; if(!Location.equals(newLoc, currentLocation)) { currentLocation = newLoc; locSetTime = System.currentTimeMillis(); } return oldLoc; }
/** * Distance between two valid locations without bounds check. * The behaviour is undefined for invalid locations. * @param a a valid location * @param b a valid location * @return the absolute distance between the two locations in the circular location space. */ private static double simpleDistance(double a, double b) { return Math.abs(change(a, b)); }
public void testNormalize() { // Simple cases. for (int i = 0; i < 5; i++) { assertEquals(VALID_1, Location.normalize(VALID_1 + i), EPSILON); assertEquals(VALID_1, Location.normalize(VALID_1 - i), EPSILON); assertEquals(VALID_2, Location.normalize(VALID_2 + i), EPSILON); assertEquals(VALID_2, Location.normalize(VALID_2 - i), EPSILON); } // Corner case. assertEquals(0.0, Location.normalize(1.0)); }
PeerLocation(String locationString) { currentLocation = Location.getLocation(locationString); locSetTime = System.currentTimeMillis(); }
/** * Parses a location. * @param init a location string * @return the location, or LOCATION_INVALID for all invalid locations and on parse errors. */ public static double getLocation(String init) { try { if (init == null) { return LOCATION_INVALID; } double d = Double.parseDouble(init); if (!isValid(d)) { return LOCATION_INVALID; } return d; } catch (NumberFormatException e) { return LOCATION_INVALID; } }
/** * Distance between two valid locations. * @param a a valid location * @param b a valid location * @return the absolute distance between the locations in the circular location space. */ public static double distance(double a, double b) { if (!isValid(a) || !isValid(b)) { String errMsg = "Invalid Location ! a = " + a + " b = " + b + " Please report this bug!"; Logger.error(Location.class, errMsg, new Exception("error")); throw new IllegalArgumentException(errMsg); } return simpleDistance(a, b); }
public void testEquals() { // Simple cases. assertTrue(Location.equals(VALID_1, VALID_1)); assertTrue(Location.equals(VALID_2, VALID_2)); assertFalse(Location.equals(VALID_1, VALID_2)); assertFalse(Location.equals(VALID_2, VALID_1)); // Cases with invalid locations. assertFalse(Location.equals(INVALID_1, VALID_1)); assertFalse(Location.equals(INVALID_1, VALID_2)); assertFalse(Location.equals(INVALID_2, VALID_1)); assertFalse(Location.equals(INVALID_2, VALID_2)); assertTrue(Location.equals(INVALID_1, INVALID_1)); assertTrue(Location.equals(INVALID_2, INVALID_2)); assertTrue(Location.equals(INVALID_1, INVALID_2)); assertTrue(Location.equals(INVALID_2, INVALID_1)); // Corner cases. assertTrue(Location.equals(0.0, 0.0)); assertTrue(Location.equals(0.0, 1.0)); assertTrue(Location.equals(1.0, 0.0)); assertTrue(Location.equals(1.0, 1.0)); }
public synchronized void updateLocationChangeSession(double newLoc) { double oldLoc = loc; double diff = Location.change(oldLoc, newLoc); if(logMINOR) Logger.minor(this, "updateLocationChangeSession: oldLoc: "+oldLoc+" -> newLoc: "+newLoc+" moved: "+diff); this.locChangeSession += diff; }
/** Should only be called in the constructor */ public void setPeerLocations(String[] peerLocationsString) { if(peerLocationsString != null) { double[] peerLocations = new double[peerLocationsString.length]; for(int i = 0; i < peerLocationsString.length; i++) peerLocations[i] = Location.getLocation(peerLocationsString[i]); updateLocation(currentLocation, peerLocations); } }