Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package net.floodlightcontroller.counter; |
| 19 | |
| 20 | /** |
| 21 | * The class defines the counter value type and value |
| 22 | * |
| 23 | * @author Kanzhe |
| 24 | * |
| 25 | */ |
| 26 | public class CounterValue { |
| 27 | public enum CounterType { |
| 28 | LONG, |
| 29 | DOUBLE |
| 30 | } |
| 31 | |
| 32 | protected CounterType type; |
| 33 | protected long longValue; |
| 34 | protected double doubleValue; |
| 35 | |
| 36 | public CounterValue(CounterType type) { |
| 37 | this.type = CounterType.LONG; |
| 38 | this.longValue = 0; |
| 39 | this.doubleValue = 0.0; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * This method is only applicable to type long. |
| 44 | * Setter() should be used for type double |
| 45 | */ |
| 46 | public void increment(long delta) { |
| 47 | if (this.type == CounterType.LONG) { |
| 48 | this.longValue += delta; |
| 49 | } else { |
| 50 | throw new IllegalArgumentException("Invalid counter type. This counter is not a long type."); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public void setLongValue(long value) { |
| 55 | if (this.type == CounterType.LONG) { |
| 56 | this.longValue = value; |
| 57 | } else { |
| 58 | throw new IllegalArgumentException("Invalid counter type. This counter is not a long type."); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public void setDoubleValue(double value) { |
| 63 | if (this.type == CounterType.DOUBLE) { |
| 64 | this.doubleValue = value; |
| 65 | } else { |
| 66 | throw new IllegalArgumentException("Invalid counter type. This counter is not a double type."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public long getLong() { |
| 71 | if (this.type == CounterType.LONG) { |
| 72 | return this.longValue; |
| 73 | } else { |
| 74 | throw new IllegalArgumentException("Invalid counter type. This counter is not a long type."); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public double getDouble() { |
| 79 | if (this.type == CounterType.DOUBLE) { |
| 80 | return this.doubleValue; |
| 81 | } else { |
| 82 | throw new IllegalArgumentException("Invalid counter type. This counter is not a double type."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public CounterType getType() { |
| 88 | return this.type; |
| 89 | } |
| 90 | |
| 91 | public String toString() { |
| 92 | String ret = "{type: "; |
| 93 | if (this.type == CounterType.DOUBLE) { |
| 94 | ret += "Double" + ", value: " + this.doubleValue + "}"; |
| 95 | } else { |
| 96 | ret += "Long" + ", value: " + this.longValue + "}"; |
| 97 | } |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | } |