Monday, September 28, 2015

Truthy values : Java parsing string to boolean

When i worked for a product company for 3 years, there was a lot of custom code made by many developers. And since we had over 20 customers there were a lot of settings and flag driven features.

Some developer would use 'yes' to denote something was true or enabled. Others would use 'Y' others would use 'true' and a few would be case in sensitive. For a few '1' was okay to denoate true.

Similarly false could be 'f', 'FALSE', 0, 'no' in strict case or case insensitive.

Made a small function to denote truthy  value parsing, along with a default (if null or that row missing from the RDBMS table).

boolean isTruthy(String value, boolean default){ 
 if(value == null || value.length()){
 char c = Character.toLowerCase(value.charAt(0)); 
 return c == 'y' || c == 't' || c=='1'; 
 }

reference :
http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#toLowerCase%28char%29

Sunday, July 26, 2015

lock and swich off screen on linux, ununtu, centos

Method one:
Make a text file and call it 'switchOffScreen.sh' save it in home directory or other dir (folder).

Contents of file:

#!/bin/bash
gnome-screensaver-command -l
sleep 2
xset dpms force off


Give it execute permissions by command:

chmod +x ~/switchOffScreen.sh

Now make a short cut in keyboard to this script, call it when required.

Method two : 
Tested on ubuntu :
open keyboard short cuts, make a new shortcut with command : 

 gnome-screensaver-command -l | sleep 2 |  xset dpms force off

Add a shortcut, I overwrote Ctrl-Alt-L confirmed I want to over ride the default and now my screen goes off after I lock. Less power used. 

Followers