Tuesday 13 December 2016

Java Enum get value from String

Problem:

In Java, I have an Enum and I'd like to be able to look up one of its value using a String.

Solution:

In order to get a specific Enum value from a String, use the java.lang.Enum.valueOf() function.

For instance, if I have the following Enum:

enum Day {Mo, Tu, We, Th, Fr, Sa, Su}

Then the following code gets Day.Th from the String "Th":

Day.valueOf("Th")
Another example, assuming the above Enum Day is defined:
Day myDayExample;
myDayExample = Day.valueOf("Th");

System.out.println(myDayExample); // prints: Th

Notes:

If either the Enum type or string is null, then java.lang.Enum.valueOf() will throw a NullPointerException.

Additionally, if no Enum constant with the name in the String exists, an IllegalArgumentException will be thrown.

For more information, check out the references below.

References:

Sunday 11 December 2016

Efficient way to find the sign of a number in Java

Problem:

What is an efficient way to find the sign of a number in Java?

Solution:

To find the sign of a number, you can use the signum() function. (e.g. Math.signum(), Integer.signum(), Long.signum(), etc.)

For example:


int sign1 = (int) Math.signum(-123); // -1
int sign2 = (int) Math.signum(222);  //  1
int sign3 = Integer.signum(-1234);   // -1

double sign4 = Math.signum(-123.0);  // -1.0
float sign5 = Math.signum(111.0f);   //  1.0f

double sign6 = Math.signum(0.0);     //  0.0

Notes:

For more information (minimum JDK version, etc.), check out the references below.

References:

Javascript Unix time to UTC string in Chrome console

Problem:

In the Chrome Dev Tools console, I'd like to view a Unix Time numerical value/timestamp as a human-readable UTC date/time string.

Solution:

In the Chrome Dev Tools console (and in general), one way to view a Unix (POSIX/Epoch) Time as a human-readable UTC string is to use Javascript Date.

For example, if the number of seconds since Epoch is 1480528272, we can convert this in the console by:

new Date(1480528272*1000).toUTCString()

Note that we need to convert the number of seconds to milliseconds since Epoch by multiplying by 1000.

Notes:

Note that due to the size of a Javascript integer, overflow may occur if the desired timestamp is larger than Number.MAX_SAFE_INTEGER.

A Javascript Date object also has a valid range that must be adhered to. More information can be found in here.

References:

Saturday 10 December 2016

GWT disable CellTable row highlight on mouse hover

Problem:

In GWT 2.7, how do I disable CellTable row highlight during mouse hover?

Solution:

Imagine your CellTable is named theTable. To disable the mouse-hover row highlighting, try the following:

theTable.setSkipRowHoverStyleUpdate(true);

Notes:

This was verified to work in GWT 2.7. Your mileage may vary with other versions. This reminder was meant as a quick reference for myself, so it's a bit brief. Check out the references below for more detail.

Also note that the above code might also work for GXT DataGrid, depending on version.

References:

Convert C# bytes to human-readable strings

Problem:

How do I convert a C# byte into a string so that I can read all 8 digits?

Solution:

Imagine that you have a C# byte named myByte. To get a string containing all 8 binary digits, you can use the following code:

  Convert.ToString(myByte, 2).PadLeft(8, '0');

Notes:

Also remember that the Convert.ToString method is in the System namespace.

References:

https://msdn.microsoft.com/en-us/library/system.convert.tostring(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.string.padleft(v=vs.110).aspx http://stackoverflow.com/questions/4829366/byte-to-binary-string-c-sharp-display-all-8-digits

C# selectively disable warnings

Problem:

I would like to selectively disable C# warnings.

Solution:

The syntax to selectively disable warnings is:

#pragma warning disable <warning number or warning list>
   <code block where warning is to be ignored>
#pragma warning restore <warning number or warning list>

Example:

#pragma warning disable 0618
  MyNecessaryObsoleteFunctionCall();
#pragma warning restore 0618

BadObsoleteCallThatShouldProduceWarning();

Notes:

C# compiler warnings are generally there for good reason, so it's better practice to resolve them rather than hide them. However, in some situations willfully ignoring/acknowledging a warning might be necessary (e.g. if warnings block your team's build, but the code is temporarily required for some important reason).

References:

https://msdn.microsoft.com/en-ca/library/441722ys.aspx http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings