Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.ui.topo; |
| 19 | |
| 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | /** |
| 24 | * Represents a "node location" on a UI layout. |
| 25 | */ |
| 26 | public final class LayoutLocation { |
| 27 | private static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0; |
| 28 | |
| 29 | /** |
| 30 | * Designates the type of location; either geographic or logical grid. |
| 31 | */ |
| 32 | public enum Type { |
| 33 | GEO, GRID |
| 34 | } |
| 35 | |
| 36 | private final String id; |
| 37 | private final double latOrY; |
| 38 | private final double longOrX; |
| 39 | private final Type locType; |
| 40 | |
| 41 | private LayoutLocation(String id, Type locType, double latOrY, double longOrX) { |
| 42 | this.id = id; |
| 43 | this.latOrY = latOrY; |
| 44 | this.longOrX = longOrX; |
| 45 | this.locType = locType; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | private boolean doubleIsZero(double value) { |
| 50 | return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns true if the coordinates indicate the origin (0, 0) of the |
| 55 | * coordinate system; false otherwise. |
| 56 | * |
| 57 | * @return true if geo-coordinates are set; false otherwise |
| 58 | */ |
| 59 | public boolean isOrigin() { |
| 60 | return doubleIsZero(latOrY) && doubleIsZero(longOrX); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the identifier associated with this location. |
| 65 | * |
| 66 | * @return the identifier |
| 67 | */ |
| 68 | public String id() { |
| 69 | return id; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the latitude (geo) or y-coord (grid) data value. |
| 74 | * |
| 75 | * @return geo latitude or grid y-coord |
| 76 | */ |
| 77 | public double latOrY() { |
| 78 | return latOrY; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the longitude (geo) or x-coord (grid) data value. |
| 83 | * |
| 84 | * @return geo longitude or grid x-coord |
| 85 | */ |
| 86 | public double longOrX() { |
| 87 | return longOrX; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the location type (geo or grid), which indicates how the data |
| 92 | * is to be interpreted. |
| 93 | * |
| 94 | * @return location type |
| 95 | */ |
| 96 | public Type locType() { |
| 97 | return locType; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String toString() { |
| 102 | return toStringHelper(this) |
| 103 | .add("id", id) |
| 104 | .add("lat/Y", latOrY) |
| 105 | .add("long/X", longOrX) |
| 106 | .add("loc-type", locType) |
| 107 | .toString(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Creates an instance of a layout location. |
| 112 | * |
| 113 | * @param id an identifier for the item at this location |
| 114 | * @param locType the location type |
| 115 | * @param latOrY geo latitude / grid y-coord |
| 116 | * @param longOrX geo longitude / grid x-coord |
| 117 | * @return layout location instance |
| 118 | */ |
| 119 | public static LayoutLocation layoutLocation(String id, Type locType, |
| 120 | double latOrY, double longOrX) { |
| 121 | checkNotNull(id, "must supply an identifier"); |
| 122 | checkNotNull(locType, "must declare location type"); |
| 123 | return new LayoutLocation(id, locType, latOrY, longOrX); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Creates an instance of a layout location. |
| 128 | * |
| 129 | * @param id an identifier for the item at this location |
| 130 | * @param locType the location type ("geo" or "grid") |
| 131 | * @param latOrY geo latitude / grid y-coord |
| 132 | * @param longOrX geo longitude / grid x-coord |
| 133 | * @return layout location instance |
| 134 | * @throws IllegalArgumentException if the type is not "geo" or "grid" |
| 135 | */ |
| 136 | public static LayoutLocation layoutLocation(String id, String locType, |
| 137 | double latOrY, double longOrX) { |
| 138 | Type t = Type.valueOf(locType.toUpperCase()); |
| 139 | return new LayoutLocation(id, t, latOrY, longOrX); |
| 140 | } |
| 141 | } |