Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.incubator.net.l2monitoring.soam; |
| 17 | |
| 18 | import java.text.DecimalFormat; |
| 19 | import java.text.NumberFormat; |
| 20 | |
| 21 | /** |
| 22 | * A representation of a ratio in milli-percent. |
| 23 | */ |
| 24 | public final class MilliPct extends Number { |
| 25 | |
| 26 | private static final long serialVersionUID = 1476288484705687568L; |
| 27 | private static NumberFormat pf = DecimalFormat.getPercentInstance(); |
| 28 | |
| 29 | private int value; |
| 30 | |
| 31 | private MilliPct(int value) { |
| 32 | this.value = value; |
| 33 | pf.setMaximumFractionDigits(3); |
| 34 | } |
| 35 | |
| 36 | public static MilliPct ofMilliPct(int value) { |
| 37 | return new MilliPct(value); |
| 38 | } |
| 39 | |
| 40 | public static MilliPct ofPercent(float value) { |
| 41 | return new MilliPct(Float.valueOf(value * 1000f).intValue()); |
| 42 | } |
| 43 | |
| 44 | public static MilliPct ofRatio(float value) { |
| 45 | return new MilliPct(Float.valueOf(value * 100000f).intValue()); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int intValue() { |
| 50 | return value; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public long longValue() { |
| 55 | return value; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public float floatValue() { |
| 60 | return value; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public double doubleValue() { |
| 65 | return value; |
| 66 | } |
| 67 | |
| 68 | public float percentValue() { |
| 69 | return value / 1000f; |
| 70 | } |
| 71 | |
| 72 | public float ratioValue() { |
| 73 | return value / 100000f; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return pf.format(ratioValue()); |
| 79 | } |
| 80 | } |