You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ch...@apache.org on 2019/08/22 04:12:03 UTC

[openwhisk] branch master updated: Add GB unit to ByteSize (#4589)

This is an automated email from the ASF dual-hosted git repository.

chetanm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 7254982  Add GB unit to ByteSize (#4589)
7254982 is described below

commit 72549829b300ddf237f92aa4c90f7c7aff816540
Author: tysonnorris <tn...@adobe.com>
AuthorDate: Wed Aug 21 21:11:50 2019 -0700

    Add GB unit to ByteSize (#4589)
---
 .../scala/org/apache/openwhisk/core/entity/Size.scala | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Size.scala b/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Size.scala
index 0ce8030..8902e9b 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Size.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Size.scala
@@ -30,22 +30,33 @@ object SizeUnits extends Enumeration {
     def toBytes(n: Long): Long
     def toKBytes(n: Long): Long
     def toMBytes(n: Long): Long
+    def toGBytes(n: Long): Long
   }
 
   case object BYTE extends Unit {
     def toBytes(n: Long): Long = n
     def toKBytes(n: Long): Long = n / 1024
     def toMBytes(n: Long): Long = n / 1024 / 1024
+    def toGBytes(n: Long): Long = n / 1024 / 1024 / 1024
   }
   case object KB extends Unit {
     def toBytes(n: Long): Long = n * 1024
     def toKBytes(n: Long): Long = n
     def toMBytes(n: Long): Long = n / 1024
+    def toGBytes(n: Long): Long = n / 1024 / 1024
+
   }
   case object MB extends Unit {
     def toBytes(n: Long): Long = n * 1024 * 1024
     def toKBytes(n: Long): Long = n * 1024
     def toMBytes(n: Long): Long = n
+    def toGBytes(n: Long): Long = n / 1024
+  }
+  case object GB extends Unit {
+    def toBytes(n: Long): Long = n * 1024 * 1024 * 1024
+    def toKBytes(n: Long): Long = n * 1024 * 1024
+    def toMBytes(n: Long): Long = n * 1024
+    def toGBytes(n: Long): Long = n
   }
 }
 
@@ -98,13 +109,14 @@ case class ByteSize(size: Long, unit: SizeUnits.Unit) extends Ordered[ByteSize]
       case SizeUnits.BYTE => s"$size B"
       case SizeUnits.KB   => s"$size KB"
       case SizeUnits.MB   => s"$size MB"
+      case SizeUnits.GB   => s"$size GB"
     }
   }
 }
 
 object ByteSize {
-  private val regex = """(?i)\s?(\d+)\s?(MB|KB|B|M|K)\s?""".r.pattern
-  protected[entity] val formatError = """Size Unit not supported. Only "B", "K[B]" and "M[B]" are supported."""
+  private val regex = """(?i)\s?(\d+)\s?(GB|MB|KB|B|G|M|K)\s?""".r.pattern
+  protected[entity] val formatError = """Size Unit not supported. Only "B", "K[B]", "M[B]" and "G[B]" are supported."""
 
   def fromString(sizeString: String): ByteSize = {
     val matcher = regex.matcher(sizeString)
@@ -114,6 +126,7 @@ object ByteSize {
         case 'B' => SizeUnits.BYTE
         case 'K' => SizeUnits.KB
         case 'M' => SizeUnits.MB
+        case 'G' => SizeUnits.GB
       }
 
       ByteSize(size, unit)
@@ -163,9 +176,11 @@ trait SizeConversion {
   def B = sizeIn(SizeUnits.BYTE)
   def KB = sizeIn(SizeUnits.KB)
   def MB = sizeIn(SizeUnits.MB)
+  def GB: ByteSize = sizeIn(SizeUnits.GB)
   def bytes = B
   def kilobytes = KB
   def megabytes = MB
+  def gigabytes: ByteSize = GB
 
   def sizeInBytes = sizeIn(SizeUnits.BYTE)