I am writing a code in which i need filesize and filesize unit. Lets say if someone is calling my function he would call it
myfun (3, MB)
Which means the expected file size would be 3 MB. I am thinking to put this “file unit” in an enum
public enum SizeUnit {BYTE, KILOBYTE, MEGABYTE, GIGABYTE}
Now the problem is converting the the given size into bytes. Of course i can do simple math, but i want something more genering. so lets say if somene has called me function like
myfun (3, MEGABYTE)
in my code i want simply 3 * MEGABYTE to convert it into BYTE unit.
I am not sure if this is possible … I am just thinking about one possibility. There could be a method in enum, which takes the its unit and do the calculation? (i don’t want to do the trivial one, there should be something generic enough).
You can do something like this:
Then you can do things like:
or even define a method called
getByteswhich passes a number. SoKILOBYTE.getBytes(3).