Monday, March 28, 2011

Data sorting by java Comparator,Comparable

package com.test;

public class Employee implements Comparable {


private int empId;
private String name;
private int age;

public Employee(int empId, String name, int age) {
this.empId = empId;
this.name = name;
this.age = age;
}
// getters & setters

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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


public int compareTo(Employee o) {
return this.empId - o.empId ;
// return this.age - o.age ;
}

}

---------------------------------------------------------------------
package com.test;

import java.util.*;

public class Util {

public static List getEmployees() {

List col = new ArrayList();

col.add(new Employee(5, "Frank", 28));
col.add(new Employee(1, "Jorge", 19));
col.add(new Employee(6, "Bill", 34));
col.add(new Employee(3, "Michel", 10));
col.add(new Employee(7, "Simpson", 8));
col.add(new Employee(4, "Clerk",16 ));
col.add(new Employee(8, "Lee", 40));
col.add(new Employee(2, "Mark", 30));

return col;
}
}

-----------------------------------------------------------------------
package com.test;

import java.util.*;

public class TestEmployeeSort {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
List coll = Util.getEmployees();
// System.out.println(coll);
Collections.sort(coll); // sort method

//Collections.sort(coll, new EmpSortByName());
//Collections.sort(coll, new EmpSortByEmpId());
// Collections.sort(coll, new EmpSortByAge());
//System.out.println(coll);
printList(coll);
}

private static void printList(List list) {
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}

-------------------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34

----------------------------------------------------------------------
package com.test;

import java.util.*;

public class EmpSortByName implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
}
--------------------------------------------------
Result
EmpId Name Age
6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simpson 8


---------------------------------------------------------
package com.test;

import java.util.Comparator;

public class EmpSortByAge implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getAge() - o2.getAge();
}

}
-----------------------------------------------------------
Result

EmpId Name Age
7 Simpson 8
3 Michel 10
4 Clerk 16
1 Jorge 19
5 Frank 28
2 Mark 30
6 Bill 34
8 Lee 40

--------------------------------------------------------------
package com.test;

import java.util.*;

public class EmpSortByEmpId implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getEmpId() - o2.getEmpId();
}

}
----------------------------------------------------------
Result
EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
7 Simpson 8
8 Lee 40
-----------------------------------------------------------------

Monday, March 21, 2011

Java Unicode converter

package com.test;

public class Test {

public static void main(String arg[]) {

//System.out.print("كل ما تحب معرفته عن");

System.out.print(convert("كل ما تحب معرفته عن"));;

}

public static String convert(String str)

{

StringBuffer ostr = new StringBuffer();

for (int i = 0; i < str.length(); i++)

{

char ch = str.charAt(i);

if ((ch >= 0x0020) && (ch <= 0x007e)) // Does the char need to be
// converted to unicode?

{

ostr.append(ch); // No.

} else // Yes.

{

ostr.append("\\u"); // standard unicode format.

String hex = Integer.toHexString(str.charAt(i) & 0xFFFF); // Get
// hex
// value
// of
// the
// char.

for (int j = 0; j < 4 - hex.length(); j++)
// Prepend zeros because unicode requires 4 digits

ostr.append("0");

ostr.append(hex.toLowerCase()); // standard unicode format.

// ostr.append(hex.toLowerCase(Locale.ENGLISH));

}

}

return (new String(ostr)); // Return the stringbuffer cast as a string.

}

}

DailyRollingFileAppender

With the following Log4j configuration


log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=sample.log
log4j.appender.R.DatePattern='.'yyyy-MM-dd


The sample.log will be copied to sample.log.2006-06-06, example,

sample.log
sample.log.2006-06-06
sample.log.2006-06-05

---------------------------------------------------------------------------------
  1. name="Rolling" class="org.apache.log4j.RollingFileAppender">
  2. name="file" value="${catalina.home}/logs/test.log" />
  3. name="maxFileSize" value="10000KB" />
  4. name="maxBackupIndex" value="25" />
  5. class="org.apache.log4j.PatternLayout">
  6. name="ConversionPattern" value="[%t] %d{HH:mm:ss,SSS} %-5p %l - %m%n" />
------------------------------------------------------------------------------------
  1. name="Daily" class="org.apache.log4j.DailyRollingFileAppender">
  2. name="file" value="${catalina.home}/logs/test.log" />
  3. name="DatePattern" value="'.'yyyy-MM-dd" />
  4. name="MaxBackupIndex" value="10" />
  5. class="org.apache.log4j.PatternLayout">
  6. name="ConversionPattern" value="[%t] %d{HH:mm:ss,SSS} %-5p %l - %m%n" />



Followers