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. 

Sunday, June 15, 2014

Google Youtube suggestions for player - settings for quality and annotations in profile

To google support
1. in full screen goes to HD even if i made it go to 480 or 360p in earlier videos. can you add a setting in profile for your player not to do this? Why? Live in India, pay INR 1300/- per month to a "fibre" optic broad band provider (ACT) but still get slow connections, except for their sponsored speedtest site! duh. If your player does not go to HD i can watch without pauces and bad breaks.

2. similarly with annotations - a setting in profile to turn off by default

Note this was shared via the feedback tab too but re sharing so others who agree can send to them as well

Thursday, June 12, 2014

Java: rename file : file_0001.dat, file_0002 to file_0004, file_0008.dat

Issue: bunch of files name file_0001.dat, file_0002.dat......file_1000.dat.
 I want to change the file names such as the number after file_ will be a
 multiple of 4 in comparison to previous file name. So i want to change 

file_0001.dat to file_0004.dat
file_0002.dat to file_0008.dat 
 
Start from the highest and go to lowest. As 
if u go from lowest - there will be conflicts. example u cant rename 001
 to 004 if there is a 004 from before ...what if it at first you have 
file_0001.dat, file_0001.dat, ... file_0004.dat. 
  
 import java.util.*;
import java.io.*;

public class RenameFiles{
    private Properties props = new Properties();
    private String args[];
    private TreeMap<Integer, FileRen> ffs = new TreeMap<Integer, FileRen>();

    RenameFiles(String args[]){
        this.args = args;
        start();
    }
    public static void main(String args[]){
        new RenameFiles(args);
    }

    void start(){
        defaults();
        load();
        scanDir();
        renameDo();

    }

    void renameDo(){
        final int ll = ffs.size();
        if(ll < 1){
            System.err.println( "Nothing to do ffs size " + ll );
            return;
        }
        ArrayList<FileRen>  frs = new ArrayList<FileRen>();
        frs.addAll(ffs.values());
        for(int i  = ll - 1;i >= 0; i--){
            FileRen fr = frs.get(i);
            boolean t = fr.orig.renameTo(fr.renamed);
            System.err.println(i + " Result " + t + " " + fr.orig + " to " +  fr.renamed);
        }
    }

    void load(){
    if(args.length > 1){
        }
    }

    void defaults(){
        props.setProperty("dir", ".");
    }

    void scanDir(){
        File f = new File(props.getProperty("dir"));
        FFilter ffilter = new FFilter();
        String []l = f.list(ffilter);
        for(String nn : l){
            FileRen fr = FileRen.check(f, nn);
            if(fr != null){
                ffs.put(fr.idx, fr);
            }
        }
    }
}

    class FFilter implements java.io.FilenameFilter
    {
    public boolean accept(File dir,
             String name){
        System.err.println(dir + " " + name + " check ");
        File f = new File(dir, name);
        if(!f.exists() || !f.isFile())return false;
        final int i = name.lastIndexOf(".");
        final int i2 = name.lastIndexOf("_");
        if(i2 < 0 || i < i2)return false;
        return true;
        }
    }

class FileRen{
    File orig;
    File renamed;
    int idx;
        FileRen(){

        }

        static FileRen check(File parent, String s){
            final int i = s.lastIndexOf(".");
            final int i2 = s.lastIndexOf("_");
            //file_0001.dat to file_0004.dat
            if(i2 < 0 || i < i2)return null;
            String p = s.substring(i2+1, i);
            try{
                int idx = Integer.parseInt(p);
                if(idx > 0){
                    FileRen f = new FileRen();
                    f.orig = new File(parent, s);
                    f.idx = idx;
                    int nxt = idx * 4;
                    String pad = pad(nxt, 4);
                    f.renamed = new File(parent, s.substring(0, i2 + 1) + pad +s.substring(i) );
                    return f;
                }
            }catch(Exception e){
                System.err.println(e + " " + parent + " " + s);
            }
            return null;
        }

        static String pad(int i, int k){
            StringBuilder sb = new StringBuilder().append(i);
            int ll = sb.length();
            for(int c = ll; c <= k; c++){
                sb.insert(0, '0');
            }
            return sb.toString();
        }
    }
 
 
Utility script to make a few files, test helper
touch file_0001.dat touch file_0002.dat touch file_0004.dat touch file_0005.dat touch file_0007.dat This version just start the program in the same dir as the files, after compiling and changing the classpath
javac /prog/j/apps/sel2in/ioFiles/RenameFiles.java
java -cp /prog/j/apps/sel2in/ioFiles/ RenameFiles
 

Followers