java comparater and comparable example

package test;/*
** Use the Collections.sort to sort a List
**
** When you need natural sort order you can implement
** the Comparable interface.
**
** If You want an alternate sort order or sorting on different properties
* then implement a Comparator for your class.
*/
import java.util.*;

@SuppressWarnings("unchecked")
public class Farmer implements Comparable
{
String name;
int age;
long income;

public Farmer(String name, int age)
{
this.name = name;
this.age = age;
}

public Farmer(String name, int age,long income)
{
this.name = name;
this.age = age;
this.income=income;
}
public String getName()
{
return name;
}

public int getAge()
{
return age;
}

public String toString()
{
return name + " : " + age;
}

/*
** Implement the natural order for this class
*/
public int compareTo(Object o)
{
return getName().compareTo(((Farmer)o).getName());
}

@SuppressWarnings("unchecked")
static class AgeComparator implements Comparator
{

/*
* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*
*/
public int compare(Object o1, Object o2)
{
Farmer p1 = (Farmer)o1;
Farmer p2 = (Farmer)o2;
if(p1.getIncome()==0 && p2.getIncome()==0 )
return p1.getAge() - p2.getAge();
else
return (int)(p1.getIncome() -p2.getIncome());
}
}

@SuppressWarnings("unchecked")
public static void main(String[] args)
{
List farmer = new ArrayList();
farmer.add( new Farmer("Joe", 34) );
farmer.add( new Farmer("Ali", 13) );
farmer.add( new Farmer("Mark", 25) );
farmer.add( new Farmer("Dana", 66) );
/*
Collections.sort(farmer);
System.out.println("Sort in Natural order");
System.out.println("t" + farmer);

Collections.sort(farmer, Collections.reverseOrder());
System.out.println("Sort by reverse natural order");
System.out.println("t" + farmer);*/

List farmerIncome = new ArrayList();
farmerIncome.add( new Farmer("Joe", 34,33));
farmerIncome.add( new Farmer("Ali", 13,3));
farmerIncome.add( new Farmer("Mark", 25,666));
farmerIncome.add( new Farmer("Dana", 66,2));

Collections.sort(farmer, new AgeComparator());
System.out.println("Sort using Age Comparator");
System.out.println("t" + farmer);

Collections.sort(farmerIncome, new AgeComparator());
System.out.println("Sort using Age Comparator But Income Wise");
System.out.println("t" + farmerIncome);

}

public long getIncome() {
return income;
}

public void setIncome(long income) {
this.income = income;
}

public void setAge(int age) {
this.age = age;
}

public void setName(String name) {
this.name = name;
}
}

Observer design pattern example

Observale class
-----------------------------------------------

package com.anil.observable;

import java.util.Observable;

public class DataObject extends Observable {

private String txt;

public DataObject(String s) {
this.txt=s;
}

public String getTxt() {
return txt;
}

public void setTxt(String txt) {
this.txt = txt;
setChanged();
}

}
------------------------------------------------
Observer classes
------------------------------------------------
package com.anil.observer;

import java.util.Observable;
import java.util.Observer;

import com.anil.observable.DataObject;

public class FirstObserver implements Observer{

@Override
public void update(Observable o, Object arg) {

System.out.println("FirstObserver----"+((DataObject)o).getTxt());
}

}
-----------------------------------------------------------------------------------
package com.anil.observer;

import java.util.Observable;
import java.util.Observer;

import com.anil.observable.DataObject;

public class SecondObserver implements Observer {

@Override
public void update(Observable o, Object arg) {

System.out.println("SecondObserver----"+((DataObject)o).getTxt());

}

}
-----------------------------------------------------------------------------------
test classes
-----------------------------------------------------------------------------------
package com.anil.data;

import com.anil.observable.DataObject;
import com.anil.observer.FirstObserver;
import com.anil.observer.SecondObserver;

public class ObserverTest {

public static void main(String[] args) {

DataObject db = new DataObject("anil");
FirstObserver fo=new FirstObserver();
SecondObserver so=new SecondObserver();
db.addObserver(fo);
db.addObserver(so);
db.setTxt("Data Updated");
db.notifyObservers();

}