Refactor: move method to get annotated double value
- Move to AnnotationKeys to remove code duplication Change-Id: Icf8b4322fc88e43ab531402d0e403a38d186f48e
Showing
3 changed files
with
23 additions
and
29 deletions
| ... | @@ -28,4 +28,23 @@ public final class AnnotationKeys { | ... | @@ -28,4 +28,23 @@ public final class AnnotationKeys { |
| 28 | * Annotation key for latency. | 28 | * Annotation key for latency. |
| 29 | */ | 29 | */ |
| 30 | public static final String LATENCY = "latency"; | 30 | public static final String LATENCY = "latency"; |
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * Returns the value annotated object for the specified annotation key. | ||
| 34 | + * The annotated value is expected to be String that can be parsed as double. | ||
| 35 | + * If parsing fails, the returned value will be 1.0. | ||
| 36 | + * | ||
| 37 | + * @param annotated annotated object whose annotated value is obtained | ||
| 38 | + * @param key key of annotation | ||
| 39 | + * @return double value of annotated object for the specified key | ||
| 40 | + */ | ||
| 41 | + public static double getAnnotatedValue(Annotated annotated, String key) { | ||
| 42 | + double value; | ||
| 43 | + try { | ||
| 44 | + value = Double.parseDouble(annotated.annotations().value(key)); | ||
| 45 | + } catch (NumberFormatException e) { | ||
| 46 | + value = 1.0; | ||
| 47 | + } | ||
| 48 | + return value; | ||
| 49 | + } | ||
| 31 | } | 50 | } | ... | ... |
| ... | @@ -21,6 +21,8 @@ import org.onlab.onos.net.resource.LinkResourceService; | ... | @@ -21,6 +21,8 @@ import org.onlab.onos.net.resource.LinkResourceService; |
| 21 | 21 | ||
| 22 | import java.util.Objects; | 22 | import java.util.Objects; |
| 23 | 23 | ||
| 24 | +import static org.onlab.onos.net.AnnotationKeys.getAnnotatedValue; | ||
| 25 | + | ||
| 24 | /** | 26 | /** |
| 25 | * Constraint that evaluates an arbitrary link annotated value is under the specified threshold. | 27 | * Constraint that evaluates an arbitrary link annotated value is under the specified threshold. |
| 26 | */ | 28 | */ |
| ... | @@ -65,25 +67,6 @@ public class AnnotationConstraint extends BooleanConstraint { | ... | @@ -65,25 +67,6 @@ public class AnnotationConstraint extends BooleanConstraint { |
| 65 | return value <= threshold; | 67 | return value <= threshold; |
| 66 | } | 68 | } |
| 67 | 69 | ||
| 68 | - /** | ||
| 69 | - * Returns the annotated value of the specified link. The annotated value | ||
| 70 | - * is expected to be String that can be parsed as double. If parsing fails, | ||
| 71 | - * the returned value will be 1.0. | ||
| 72 | - * | ||
| 73 | - * @param link link whose annotated value is obtained | ||
| 74 | - * @param key key of link annotation | ||
| 75 | - * @return double value of link annotation for the specified key | ||
| 76 | - */ | ||
| 77 | - private double getAnnotatedValue(Link link, String key) { | ||
| 78 | - double value; | ||
| 79 | - try { | ||
| 80 | - value = Double.parseDouble(link.annotations().value(key)); | ||
| 81 | - } catch (NumberFormatException e) { | ||
| 82 | - value = 1.0; | ||
| 83 | - } | ||
| 84 | - return value; | ||
| 85 | - } | ||
| 86 | - | ||
| 87 | @Override | 70 | @Override |
| 88 | public double cost(Link link, LinkResourceService resourceService) { | 71 | public double cost(Link link, LinkResourceService resourceService) { |
| 89 | if (isValid(link, resourceService)) { | 72 | if (isValid(link, resourceService)) { | ... | ... |
| ... | @@ -26,6 +26,7 @@ import java.time.temporal.ChronoUnit; | ... | @@ -26,6 +26,7 @@ import java.time.temporal.ChronoUnit; |
| 26 | import java.util.Objects; | 26 | import java.util.Objects; |
| 27 | 27 | ||
| 28 | import static org.onlab.onos.net.AnnotationKeys.LATENCY; | 28 | import static org.onlab.onos.net.AnnotationKeys.LATENCY; |
| 29 | +import static org.onlab.onos.net.AnnotationKeys.getAnnotatedValue; | ||
| 29 | 30 | ||
| 30 | /** | 31 | /** |
| 31 | * Constraint that evaluates the latency through a path. | 32 | * Constraint that evaluates the latency through a path. |
| ... | @@ -48,16 +49,7 @@ public class LatencyConstraint implements Constraint { | ... | @@ -48,16 +49,7 @@ public class LatencyConstraint implements Constraint { |
| 48 | 49 | ||
| 49 | @Override | 50 | @Override |
| 50 | public double cost(Link link, LinkResourceService resourceService) { | 51 | public double cost(Link link, LinkResourceService resourceService) { |
| 51 | - String value = link.annotations().value(LATENCY); | 52 | + return getAnnotatedValue(link, LATENCY); |
| 52 | - | ||
| 53 | - double latencyInMicroSec; | ||
| 54 | - try { | ||
| 55 | - latencyInMicroSec = Double.parseDouble(value); | ||
| 56 | - } catch (NumberFormatException e) { | ||
| 57 | - latencyInMicroSec = 1.0; | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - return latencyInMicroSec; | ||
| 61 | } | 53 | } |
| 62 | 54 | ||
| 63 | @Override | 55 | @Override | ... | ... |
-
Please register or login to post a comment