Showing the changes to Source/Libraries/TSFJavaLibrary/src/org/gpa/tsf/util/NumberExtensions.java (1 of 915 files). Show all
Unified · Split
| @@ -0,0 +1,132 @@ | ||
| 1 | +//****************************************************************************************************** | |
| 2 | +// NumberExtensions.java - Gbtc | |
| 3 | +// | |
| 4 | +// Copyright � 2010, Grid Protection Alliance. All Rights Reserved. | |
| 5 | +// | |
| 6 | +// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | |
| 7 | +// the NOTICE file distributed with this work for additional information regarding copyright ownership. | |
| 8 | +// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | |
| 9 | +// not use this file except in compliance with the License. You may obtain a copy of the License at: | |
| 10 | +// | |
| 11 | +// http://www.opensource.org/licenses/eclipse-1.0.php | |
| 12 | +// | |
| 13 | +// Unless agreed to in writing, the subject software distributed under the License is distributed on an | |
| 14 | +// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | |
| 15 | +// License for the specific language governing permissions and limitations. | |
| 16 | +// | |
| 17 | +// Code Modification History: | |
| 18 | +// ---------------------------------------------------------------------------------------------------- | |
| 19 | +// 04/12/2012 - Stephen C. Wills | |
| 20 | +// Generated original version of source code. | |
| 21 | +// | |
| 22 | +//****************************************************************************************************** | |
| 23 | + | |
| 24 | +package org.gpa.tsf.util; | |
| 25 | + | |
| 26 | +import java.math.BigInteger; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * Utility class that provides additional functionality for numbers. | |
| 30 | + * | |
| 31 | + * @author Stephen C. Wills | |
| 32 | + */ | |
| 33 | +public class NumberExtensions | |
| 34 | +{ | |
| 35 | + /** | |
| 36 | + * Gets a string representation of a long integer | |
| 37 | + * in base 10 as if it were an unsigned long integer. | |
| 38 | + * | |
| 39 | + * @param num the long integer to be converted to a string | |
| 40 | + * @return a string representation of the given long integer | |
| 41 | + * @see Long#toString(long) | |
| 42 | + */ | |
| 43 | + public static String toUnsignedString(long num) | |
| 44 | + { | |
| 45 | + return toUnsignedString(num, 10); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Gets a string representation of a long integer | |
| 50 | + * as if it were an unsigned long integer. | |
| 51 | + * | |
| 52 | + * @param num the long integer to be converted to a string | |
| 53 | + * @param radix the radix to use in the string representation | |
| 54 | + * @return a string representation of the given long integer | |
| 55 | + * @see Long#toString(long, int) | |
| 56 | + */ | |
| 57 | + public static String toUnsignedString(long num, int radix) | |
| 58 | + { | |
| 59 | + BigInteger bigNum; | |
| 60 | + BigInteger bigMod; | |
| 61 | + | |
| 62 | + if (num > 0L) | |
| 63 | + return Long.toString(radix); | |
| 64 | + | |
| 65 | + bigNum = BigInteger.valueOf(num >>> 1); | |
| 66 | + bigMod = BigInteger.valueOf(num & 1L); | |
| 67 | + | |
| 68 | + return bigNum.shiftLeft(1).add(bigMod).toString(radix); | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Gets a string representation of an integer in | |
| 73 | + * base 10 as if it were an unsigned integer. | |
| 74 | + * | |
| 75 | + * @param num the integer to be converted to a string | |
| 76 | + * @return a string representation of the given integer | |
| 77 | + * @see Integer#toString(int) | |
| 78 | + */ | |
| 79 | + public static String toUnsignedString(int num) | |
| 80 | + { | |
| 81 | + return toUnsignedString(num, 10); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Gets a string representation of an integer | |
| 86 | + * as if it were an unsigned integer. | |
| 87 | + * | |
| 88 | + * @param num the integer to be converted to a string | |
| 89 | + * @param radix the radix to use in the string representation | |
| 90 | + * @return a string representation of the given integer | |
| 91 | + * @see Integer#toString(int, int) | |
| 92 | + */ | |
| 93 | + public static String toUnsignedString(int num, int radix) | |
| 94 | + { | |
| 95 | + if (num > 0) | |
| 96 | + return Integer.toString(num, radix); | |
| 97 | + | |
| 98 | + return Long.toString(num & Masks.getLongMask(Integer.SIZE), radix); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Gets a string representation of a short integer | |
| 103 | + * in base 10 as if it were an unsigned short integer. | |
| 104 | + * | |
| 105 | + * @param num the short integer to be converted to a string | |
| 106 | + * @return a string representation of the given short integer | |
| 107 | + * @see Short#toString(short) | |
| 108 | + */ | |
| 109 | + public static String toUnsignedString(short num) | |
| 110 | + { | |
| 111 | + if (num > 0) | |
| 112 | + return Short.toString(num); | |
| 113 | + | |
| 114 | + return Integer.toString(num & Masks.getIntMask(Short.SIZE)); | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * Gets a string representation of a byte in | |
| 119 | + * base 10 as if it were an unsigned byte. | |
| 120 | + * | |
| 121 | + * @param num the byte to be converted to a string | |
| 122 | + * @return a string representation of the given byte | |
| 123 | + * @see Byte#toString(byte) | |
| 124 | + */ | |
| 125 | + public static String toUnsignedString(byte num) | |
| 126 | + { | |
| 127 | + if (num > 0) | |
| 128 | + return Byte.toString(num); | |
| 129 | + | |
| 130 | + return Integer.toString(num & Masks.getIntMask(Byte.SIZE)); | |
| 131 | + } | |
| 132 | +} | |