Friday, June 12, 2015

Java timers and observers


Scenario: Notify Multiple observers on timer event.

Approach : 
  • Create Watchdog class which creates the timer.
  • Observers register to Watchdog event.
  • Watchdog class on timer event notifies the observers. 

Sample Code :

// WatchDog.java

import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

// Observer class
class Observer1 implements Observer{

@Override
public void update(Observable arg0, Object arg1) {
System.out.println("Observer1 notified");
}
}

// Watchdog Component which creates the timer and notifies the timers.

public class WatchDog extends Observable {
    Timer timer;
    int seconds;

   // Notify task to notify observers
    class NotifyTask extends TimerTask{

        @Override
        public void run() {
        setChanged();
        notifyObservers();
        }
    }

    public WatchDog( ) {
        timer = new Timer(); 
    }

    public void schedule(long seconds){
        timer.scheduleAtFixedRate(new NotifyTask(), 0, seconds*1000); //delay in milliseconds

    }
    
    public void stop(){
    timer.cancel();
    }
    
    public static void main(String args[]) throws InterruptedException {
        Observer1 observer1 = new Observer1();

        WatchDog watchDog = new WatchDog();
        // register with observer
        watchDog.addObserver(observer1);

        System.out.println("WatchDog is scheduled.");
        watchDog.schedule(5);
        Thread.sleep(25000);

        watchDog.stop();
    }
}



Wednesday, May 27, 2015

Google IO2015

Google I/O 2015 is happening on May 28-29, 2015. Follow in real-time via the live stream or from an I/O Extended event!

Wednesday, May 20, 2015

How to use custom type as dictionary key in Python

The Python dict documentation defines these requirements on key objects, i.e. they must be hashable.

Steps :
Implement a custom key class  and override hash and equality function.

e.g.

class CustomDictKey(object):

    def __init__(self, 
                 param1,
                 param2):
    
                self._param1 = param1
                self._param2 = param2
              
    def __hash__(self):
        return hash((self._param1,
                 self._param2))

    def __eq__(self, other):
        return ( ( self._param1,
                 self._param2 ) == ( other._param1,
                 other._param2) )
                
    def __str__(self):
        return "param 1: {0} param 2: {1}  ".format(self._param1, self._param2)

if __name__ == '__main__':

    # create custom key
    k1  = CustomDictKey(10,5)
    
    k2  = CustomDictKey (2, 4)
    
    dictionary = {}
    
    #insert elements in dictionary with custom key
    dictionary[k1] = 10
    dictionary[k2] = 20
    
    # access dictionary values with custom keys and print values
    print "key: ", k1, "val :", dictionary[k1]
    print "key: ", k2, "val :", dictionary[k2]

               

Thursday, May 7, 2015

Linux: remove file extensions for multiple files


rename .oldext .newext *.oldext

substitutes .old extension to the .new extention


To simply remove the extension you can explicitly pass in an empty string as an argument.

rename . gz.tmp  ' ' *.gz

with above command all files in the folder with .gz.tmp extentsion will be renamed to filename.gz

Sunday, June 17, 2012

Inspiring: Nothing is Impossible




Do you still think God hasn't given you enough ?