Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | package org.onosproject.ui.topo; |
| 18 | |
| 19 | /** |
| 20 | * Designates a badge to be applied to a node in the topology view. |
| 21 | */ |
| 22 | public final class NodeBadge { |
| 23 | |
| 24 | private static final String EMPTY = ""; |
| 25 | |
| 26 | /** Designates the type of badge. */ |
| 27 | public enum Type { |
| 28 | INFO("i"), |
| 29 | WARN("w"), |
| 30 | ERROR("e"), |
| 31 | CHECK_MARK("/"), |
| 32 | X_MARK("X"), |
| 33 | NUMBER("n"); |
| 34 | |
| 35 | private String code; |
| 36 | |
| 37 | Type(String code) { |
| 38 | this.code = code; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public String toString() { |
| 43 | return "{" + code + "}"; |
| 44 | } |
| 45 | |
| 46 | /** Returns the type's code in string form. */ |
| 47 | public String code() { |
| 48 | return code; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private final Type type; |
| 53 | private final String message; |
| 54 | |
| 55 | // only instantiated through static methods. |
| 56 | private NodeBadge(Type type, String message) { |
| 57 | this.type = type; |
| 58 | this.message = message; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public String toString() { |
| 63 | return "{Badge " + type + " \"" + message + "\"}"; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the badge type. |
| 68 | * |
| 69 | * @return badge type |
| 70 | */ |
| 71 | public Type type() { |
| 72 | return type; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the message associated with the badge. |
| 77 | * |
| 78 | * @return associated message |
| 79 | */ |
| 80 | public String message() { |
| 81 | return message; |
| 82 | } |
| 83 | |
| 84 | private static String nonNull(String s) { |
| 85 | return s == null ? EMPTY : s; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns an informational badge, with associated message. |
| 90 | * |
| 91 | * @param message the message |
| 92 | * @return INFO type node badge |
| 93 | */ |
| 94 | public static NodeBadge info(String message) { |
| 95 | return new NodeBadge(Type.INFO, nonNull(message)); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns a warning badge, with associated message. |
| 100 | * |
| 101 | * @param message the message |
| 102 | * @return WARN type node badge |
| 103 | */ |
| 104 | public static NodeBadge warn(String message) { |
| 105 | return new NodeBadge(Type.WARN, nonNull(message)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns an error badge, with associated message. |
| 110 | * |
| 111 | * @param message the message |
| 112 | * @return ERROR type node badge |
| 113 | */ |
| 114 | public static NodeBadge error(String message) { |
| 115 | return new NodeBadge(Type.ERROR, nonNull(message)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns a check-mark badge, with associated message. |
| 120 | * |
| 121 | * @param message the message |
| 122 | * @return CHECK_MARK type node badge |
| 123 | */ |
| 124 | public static NodeBadge checkMark(String message) { |
| 125 | return new NodeBadge(Type.CHECK_MARK, nonNull(message)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns an X-mark badge, with associated message. |
| 130 | * |
| 131 | * @param message the message |
| 132 | * @return X_MARK type node badge |
| 133 | */ |
| 134 | public static NodeBadge xMark(String message) { |
| 135 | return new NodeBadge(Type.X_MARK, nonNull(message)); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns a numeric badge. |
| 140 | * |
| 141 | * @param n the number |
| 142 | * @return NUMBER type node badge |
| 143 | */ |
| 144 | public static NodeBadge number(int n) { |
| 145 | // TODO: consider constraints, e.g. 1 <= n <= 99 |
| 146 | return new NodeBadge(Type.NUMBER, Integer.toString(n)); |
| 147 | } |
| 148 | |
| 149 | } |