' ***********************************************************************
' Author : Elektro
' Modified : 07-April-2015
' ***********************************************************************
' <copyright file="RoundByteInfo.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
#Region " Usage Examples "
'For Each unit As RoundByteInfo.SizeUnit In [Enum].GetValues(GetType(RoundByteInfo.SizeUnit))
'
' Dim rByteInfo As New RoundByteInfo(unit)
' Dim stringFormat As String = String.Format("{0} Bytes rounded to {1} {2}.",
' rByteInfo.ByteValue,
' rByteInfo.RoundedValue(decimalPrecision:=2),
' rByteInfo.UnitLongName)
' Debug.WriteLine(stringFormat)
'
'Next unit
#End Region
#Region " Option Statements "
Option Explicit On
Option Strict On
Option Infer Off
#End Region
#Region " Imports "
Imports System.Globalization
#End Region
#Region " RoundByteInfo "
''' <summary>
''' Rounds the specified byte value to its most approximated size unit.
''' </summary>
Public NotInheritable Class RoundByteInfo
#Region " Properties "
''' <summary>
''' Gets the byte value.
''' </summary>
''' <value>The byte value.</value>
Public ReadOnly Property ByteValue As Double
Get
Return Me.byteValue1
End Get
End Property
''' <summary>
''' Gets the byte value.
''' </summary>
''' <param name="numberFormatInfo">A custom <see cref="NumberFormatInfo"/> format provider.</param>
''' <value>The byte value.</value>
Public ReadOnly Property ByteValue(ByVal numberFormatInfo As NumberFormatInfo) As String
Get
If numberFormatInfo Is Nothing Then
numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
End If
Return Me.byteValue1.ToString("N0", numberFormatInfo)
End Get
End Property
''' <summary>
''' Gets the rounded byte value.
''' </summary>
''' <value>The rounded byte value.</value>
Public ReadOnly Property RoundedValue As Double
Get
Return Me.roundedValue1
End Get
End Property
''' <summary>
''' Gets the rounded value with the specified decimal precision.
''' </summary>
''' <param name="decimalPrecision">The numeric decimal precision.</param>
''' <param name="numberFormatInfo">A custom <see cref="NumberFormatInfo"/> format provider.</param>
''' <value>The rounded value with the specified decimal precision.</value>
Public ReadOnly Property RoundedValue(ByVal decimalPrecision As Integer,
Optional ByVal numberFormatInfo As NumberFormatInfo = Nothing) As String
Get
If numberFormatInfo Is Nothing Then
numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
End If
Return Me.roundedValue1.ToString("N" & decimalPrecision, numberFormatInfo)
End Get
End Property
''' <summary>
''' Gets the rounded <see cref="SizeUnit"/>.
''' </summary>
''' <value>The rounded <see cref="SizeUnit"/>.</value>
Public ReadOnly Property Unit As SizeUnit
Get
Return Me.unit1
End Get
End Property
''' <summary>
''' Gets the rounded <see cref="SizeUnit"/> short name.
''' </summary>
''' <value>The rounded <see cref="SizeUnit"/> short name.</value>
Public ReadOnly Property UnitShortName As String
Get
Return Me.unitShortName1
End Get
End Property
''' <summary>
''' Gets the rounded <see cref="SizeUnit"/> long name.
''' </summary>
''' <value>The rounded <see cref="SizeUnit"/> long name.</value>
Public ReadOnly Property UnitLongName As String
Get
Return Me.unitLongName1
End Get
End Property
''' <summary>
''' The byte value.
''' </summary>
Private byteValue1 As Double
''' <summary>
''' The rounded value.
''' </summary>
Private roundedValue1 As Double
''' <summary>
''' The rounded <see cref="SizeUnit"/>.
''' </summary>
Private unit1 As SizeUnit
''' <summary>
''' The rounded <see cref="SizeUnit"/> short name.
''' </summary>
Private unitShortName1 As String
''' <summary>
''' The rounded <see cref="SizeUnit"/> long name.
''' </summary>
Private unitLongName1 As String
#End Region
#Region " Enumerations "
''' <summary>
''' Specifies a size unit.
''' </summary>
Public Enum SizeUnit As Long
''' <summary>
''' 1 Byte (or 8 bits).
''' </summary>
[Byte] = 1L
''' <summary>
''' Byte-length of 1 KiloByte.
''' </summary>
KiloByte = [Byte] * 1024L
''' <summary>
''' Byte-length of 1 MegaByte.
''' </summary>
MegaByte = KiloByte * KiloByte
''' <summary>
''' Byte-length of 1 GigaByte.
''' </summary>
GigaByte = KiloByte * MegaByte
''' <summary>
''' Byte-length of 1 TeraByte.
''' </summary>
TeraByte = KiloByte * GigaByte
''' <summary>
''' Byte-length of 1 PetaByte.
''' </summary>
PetaByte = KiloByte * TeraByte
End Enum
#End Region
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="RoundByteInfo"/> class.
''' </summary>
''' <param name="bytes">The byte value.</param>
''' <exception cref="System.ArgumentException">Value should be greater than 0.;bytes</exception>
Public Sub New(ByVal bytes As Double)
If bytes <= 0L Then
Throw New ArgumentException("Value should be greater than 0.", "bytes")
Else
Me.SetRoundByte(bytes)
End If
End Sub
''' <summary>
''' Prevents a default instance of the <see cref="RoundByteInfo"/> class from being created.
''' </summary>
Private Sub New()
End Sub
#End Region
#Region " Private Methods "
''' <summary>
''' Rounds the specified byte value to its most approximated <see cref="SizeUnit"/>.
''' </summary>
''' <param name="bytes">The byte value.</param>
Private Sub SetRoundByte(ByVal bytes As Double)
Me.byteValue1 = bytes
Select Case bytes
Case Is >= SizeUnit.PetaByte
Me.roundedValue1 = bytes / SizeUnit.PetaByte
Me.unit1 = SizeUnit.PetaByte
Me.unitShortName1 = "PB"
Me.unitLongName1 = "PetaBytes"
Case Is >= SizeUnit.TeraByte
Me.roundedValue1 = bytes / SizeUnit.TeraByte
Me.unit1 = SizeUnit.TeraByte
Me.unitShortName1 = "TB"
Me.unitLongName1 = "TeraBytes"
Case Is >= SizeUnit.GigaByte
Me.roundedValue1 = bytes / SizeUnit.GigaByte
Me.unit1 = SizeUnit.GigaByte
Me.unitShortName1 = "GB"
Me.unitLongName1 = "GigaBytes"
Case Is >= SizeUnit.MegaByte
Me.roundedValue1 = bytes / SizeUnit.MegaByte
Me.unit1 = SizeUnit.MegaByte
Me.unitShortName1 = "MB"
Me.unitLongName1 = "MegaBytes"
Case Is >= SizeUnit.KiloByte
Me.roundedValue1 = bytes / SizeUnit.KiloByte
Me.unit1 = SizeUnit.KiloByte
Me.unitShortName1 = "KB"
Me.unitLongName1 = "KiloBytes"
Case Is >= SizeUnit.Byte, Is <= 0
Me.roundedValue1 = bytes / SizeUnit.Byte
Me.unit1 = SizeUnit.Byte
Me.unitShortName1 = "Bytes"
Me.unitLongName1 = "Bytes"
End Select
End Sub
#End Region
End Class
#End Region