Author: robert
Date: 2007-12-26 18:45:14 +0000 (Wed, 26 Dec 2007)
New Revision: 16815

Modified:
   trunk/freenet/src/freenet/node/Location.java
   trunk/freenet/src/freenet/node/LocationManager.java
Log:
move signed-distance calculation (now called change();) into Location.java


Modified: trunk/freenet/src/freenet/node/Location.java
===================================================================
--- trunk/freenet/src/freenet/node/Location.java        2007-12-25 01:02:38 UTC 
(rev 16814)
+++ trunk/freenet/src/freenet/node/Location.java        2007-12-26 18:45:14 UTC 
(rev 16815)
@@ -48,6 +48,28 @@
                else return Math.min (b - a, 1.0 - b + a);
        }

+       /** Patterned after distance( double, double ), but also retuns the 
direction of the change (positive/negative). When given two values on opposite 
ends of the keyspace, it will return +0.5 */
+       public static double change(double from, double to) {
+               if (to > from) {
+                       double directDifference = to - from;
+                       double oppositeDifference = 1.0 - to + from;
+                       if (directDifference <= oppositeDifference) {
+                               return directDifference;
+                       } else {
+                               return -oppositeDifference;
+                       }
+               } else {
+                       // It's going the other way, return: -1*change(to, 
from) except the edge case of 0.5;
+                       double directDifference = from - to;
+                       double oppositeDifference = 1.0 - from + to;
+                       if (directDifference < oppositeDifference) {
+                               return -directDifference;
+                       } else {
+                               return oppositeDifference;
+                       }
+               }
+       }
+       
        public static boolean equals(double newLoc, double currentLocation) {
                return Math.abs(newLoc - currentLocation) < Double.MIN_VALUE * 
2;
        }

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2007-12-25 01:02:38 UTC 
(rev 16814)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2007-12-26 18:45:14 UTC 
(rev 16815)
@@ -107,28 +107,9 @@

     public synchronized void updateLocationChangeSession(double newLoc) {
        double oldLoc = loc;
-       // Patterned after PeerManager.distance( double, double ), but also 
need to know the direction of the change
-               if (newLoc > oldLoc) {
-                       double directDifference = newLoc - oldLoc;
-                       double oppositeDifference = 1.0 - newLoc + oldLoc;
-                       if (directDifference < oppositeDifference) {
-                               if(logMINOR) Logger.minor(this, 
"updateLocationChangeSession: oldLoc: "+oldLoc+" -> newLoc: "+newLoc+" moved: 
+"+directDifference);
-                               this.locChangeSession += directDifference;
-                       } else {
-                               if(logMINOR) Logger.minor(this, 
"updateLocationChangeSession: oldLoc: "+oldLoc+" -> newLoc: "+newLoc+" moved: 
-"+oppositeDifference);
-                               this.locChangeSession -= oppositeDifference;
-                       }
-               } else {
-                       double directDifference = oldLoc - newLoc;
-                       double oppositeDifference = 1.0 - oldLoc + newLoc;
-                       if (directDifference < oppositeDifference) {
-                               if(logMINOR) Logger.minor(this, 
"updateLocationChangeSession: oldLoc: "+oldLoc+" -> newLoc: "+newLoc+" moved: 
-"+directDifference);
-                               this.locChangeSession -= directDifference;
-                       } else {
-                               if(logMINOR) Logger.minor(this, 
"updateLocationChangeSession: oldLoc: "+oldLoc+" -> newLoc: "+newLoc+" moved: 
+"+oppositeDifference);
-                               this.locChangeSession += oppositeDifference;
-                       }
-               }
+               double diff = Location.change(oldLoc, newLoc);
+               if(logMINOR) Logger.minor(this, "updateLocationChangeSession: 
oldLoc: "+oldLoc+" -> newLoc: "+newLoc+" moved: "+diff);
+               this.locChangeSession += diff;
     }

     /**


Reply via email to