Learn JAVA5 Tiger from sample codes: avargs

public   class  Artist   {
    
    String[] others;
     /** */ /**  Creates a new instance of Artist  */ 
      public  Artist()   {
    } 
      public  Artist(String name,String ... others)  {
         for (String other : others)  {
            System.out.println(other);
        } 
         this .others  =  others;
    } 
      public   void  wildestMethod(Object ... objs)  {
     // this method will accept any arguments 
     } 
      public   static   void  main(String[] args)  {
        Artist artist  =   new  Artist( " You " ,  " 1 " ,  " 2 " );
        artist.wildestMethod( 1 ,  3.5 ,  " AA " ,  new  Artist());
    } 
    
}


(0) 评论    (0) 引用   

Learn JAVA5 Tiger from sample codes: Auto Boxing and Unboxing

public class AutoBoxing {
    
    /** Creates a new instance of AutoBoxing */
    public AutoBoxing() {
    }
    public void boxingDemo(){
        //auto boxing
        Integer i = 0;
        float f = 1.66f;
        Float F = f;
        //auto unboxing
        Integer I = new Integer(1);
        int i2 = I;
        //null value test, it will case NullPointerException
        Integer I2 = null;
        int i3 = I2;
    }
    public void testOperator(){
        Integer i = 1;
        while(true){
            i++;
            System.out.println("Counter:"+i);
            if(i>5) break;
        }
    }
    public void testCompare(){
        //it's equal because -127~127 are immutable objects
        Integer i = 1;
        Integer i2 = 1;
        if(i == i2) System.out.println("1:Equal");
        else System.out.println("1:Not Equal");
        //it's not equal because j and j2 are different objects
        Integer j = 200;
        Integer j2 =200;
        if(j == j2) System.out.println("200:Equal");
        else System.out.println("200:Not Equal");
    }
    public void testControl(){
        Boolean flag = true;
        Integer i = 20;
        Integer j = 30;
        if(flag){
            System.out.println("Boolean affects");
        }
        if(i<j)
            System.out.println("Integer affects");
    }
    public void testMethod(double arg){
        System.out.println("public void testMethod(double arg) is invoked");
    }
    public void testMethod(Integer arg){
        System.out.println("public void testMethod2(Integer arg) is invoked");
    }
    public static void main(String args[]){
        AutoBoxing auto = new AutoBoxing();
        auto.testCompare();
        auto.testOperator();
        auto.testControl();
        int i = 1;
        // public void testMethod(Integer arg) wouldn't be invoked
        //because  public void testMethod(double arg) will be invoked in JDK1.4
        //Java tiger consider the backward capability
        auto.testMethod(i);
        auto.boxingDemo();
    }
    
}


(0) 评论    (0) 引用   

Learn JAVA5 Tiger from sample codes: Enumerated

public enum User {
    Admin,User,Guest,Unknown   
}

public class Login {
    
    private User user;
    EnumMap<User,String> userName = new EnumMap<User, String>(User.class);
    /** Creates a new instance of Login */
    public Login() {
        userName.put(User.Admin, "Administrator");
        userName.put(User.User, "David");
        userName.put(User.Guest, "Steve");
    }
    public boolean isAdmin(User user){
        if(user.equals(User.Admin)){
            return true;
        }
        return false;
    }
    public void printUserRole(){
        User[] users = user.values();
        for(User u : user.values()){
            System.out.println(u.toString());
        }
    }
    public void isRole(User user){
        switch(user){
            case Admin:
                System.out.println("admin");
                break;
            case User:
                System.out.println("User");
                break;
            case Guest:
                System.out.println("Guest");
                break;
            default:
                System.out.println("unknow");
        }
    }
    public static void main(String[] args){
        Login login = new Login();
        System.out.println(login.isAdmin(User.Admin));
        login.printUserRole();
        login.isRole(User.User);
    }
    
}


(0) 评论    (0) 引用   

Learn JAVA5 Tiger from sample codes: Generic

/**
 * public class Box<T extends Number>
 * @author david.duan
 */
public class Box<T> {
    
    //you can't have a static variable such as protected static List<T> list...
    protected List<T> contents;
    
    public Box( ) {
        contents = new ArrayList<T>( );
    }
    public int getSize( ) {
        return contents.size( );
    }
    public boolean isEmpty( ) {
        return (contents.size( ) == 0);
    }
    public void add(T o) {
        contents.add(o);
    }
    public T grab( ) {
        if (!isEmpty( )) {
            return contents.remove(0);
        } else
            return null;
    }
    public static void main(String[] args){
        Box<String> box = new Box<String>();
        //...
    }
}


public class Generic {
    
    /** Creates a new instance of Generic */
    public Generic() {
    }
    
    public void collectionGeneric(){
        //generic list
        List<String> onlyStrings = new LinkedList<String>();
        onlyStrings.add("Legal addition");
        //generic map
        Map<Integer, Integer> squares = new HashMap<Integer, Integer>();
        for (int i=0; i<100; i++) {
            squares.put(i, i*i);
        }
        for (int i=0; i<10; i++) {
            int n = i*3;
            System.out.println("The square of " + n + " is " + squares.get(n));
        }
        //generic iterator
        List<String> listOfStrings = new LinkedList<String>();
        listOfStrings.add("Happy");
        listOfStrings.add("Birthday");
        for (Iterator<String> i = listOfStrings.iterator(); i.hasNext( ); ) {
            String s = i.next( );
            System.out.println(s);
        }
    }
    //parameter generic
    public void paramGeneric(List<String> list){
        for(Iterator<String> i = list.iterator();i.hasNext();){
            String s = i.next();
            System.out.println(s);
        }
    }
    //return generic
    public List<String> returnGeneric(){
        List<String> list = new LinkedList<String>();
        list.add("string1");
        return list;
    }
    //conversion
    public void conversionGeneric(){
        LinkedList<Float> floatList = new LinkedList<Float>();
        List<Float> moreFloats = floatList;
        //illegal conversion LinkedList<Number> numberList = floatList;
        //the way to resolve for backward capbility
        List<Integer> ints = new LinkedList<Integer>();
        List oldList = ints;
        List<Number> numList = oldList;
    }
    //generic wildcard : this old plain way will generate unchecked warning
    public void wildcardGeneric(List list){
        for (Iterator i = list.iterator(); i.hasNext(); ) {
            System.out.println(i.next().toString());
        }
    }
    //generic wildcard
    public void wildcardGeneric2(List<?> list){
        //you can't solve the problem by List<Object>
        for (Iterator<?> i = list.iterator( ); i.hasNext(); ) {
            System.out.println(i.next().toString( ));
        }
    }
    
}


(0) 评论    (0) 引用   

JAVA5 Tiger sample codes:

 int[][] ar = {{1,2,3},{4,5,6}};
        int[][] br = {{1,2,3},{4,5,6}};
        System.out.println(Arrays.deepToString(ar));
        System.out.println(Arrays.deepEquals(ar,br));
        StringBuilder sb = new StringBuilder("string builder");


(0) 评论    (0) 引用   

Drool-Business Logic Framework

J2EE应用无限制的分层,各种框架层出不穷,设计模式经久不衰,就是为了设计更合理,合理的目的是为了松耦合高内聚,而大家在经典的J2EE3层架构中,持久层 表示层 业务逻辑层中,表示层与业务逻辑层的分离也是讨论很多,就是为了Business Logic的重用。很简单,你的业务层能够适应Java Web Start和Browser两种client么?而因为业务逻辑是核心,如果不解耦,它的变化将直接导致表示层 持久层的变更。
长久以来,大家虽然最关心的是业务逻辑,但是,持久层 表示层框架确是最丰富的。大家似乎习惯于让技术人员或者拥有业务能力背景的高级技术人员拿到需求然后用JAVA code来描述那复杂多变的业务逻辑。虽然目前俺忙于SAP,但是对java一直在关注,最近从SpringSide了解到了Drools这个我Rule Engine,我称它为Business Logic Framework. 因为它使用非JAVA code 而是DSL(Domain-Specific Language)来描述容易变化的商业的规则。不需要冗长的java代码,不需要变更后的编译 打包 部署。讲商业规则抽取出来,嗯,似乎有持久层里iBatis的味道(SQL抽取出来)。
以前草草的浏览了一次Drool 那个时候是版本2,商业规则要配置在XML里面,我感觉很郁闷,很不直观,也有点麻烦。Drool被JBOSS收购以后产生了大的变革,现在Rule写起来就像一种自然语言与计算机脚本语言的结合,快捷,明了。
废话不多了,大家先看一看Drool的介绍,这个介绍是针对Drool2的,不过可以看出来Drool的目的和用途了。
我的好朋友差沙最近也开始写blog,他介绍了一篇Drool3的,大家可以看到Drool3的变革。
您应该迫不及待去把自己的商业规则抽取出来了吧

(0) 评论    (52) 引用   

BIRT is a BOMB

BIRT is really a amazing report tool. It's so powerful and free.
No more crap, just check this flash demo http://download.eclipse.org/birt/downloads/examples/misc/BIRT/BIRT_demo_Camv3.html


(0) 评论    (35) 引用   

SAP and JAVA Intergarion

I uploaded a document about SAP and JAVA's intergration.
It introduce BAPI RFC ALE IDoc JCo's basic concept. Enjoy it.
http://blueoxygen.dflying.net/3/resources/Java_SAP_Integration.pdf.html


(0) 评论    (57) 引用   

The last reason to use NetBeans.

Recently,SUN publish Netbeans new release.Because project's requirement,I should use netbeans to design Swing UI. And at first,i really thought Netbeans Swing UI design tools pretty cool. But then i know,that's a vendor's production.Tody i got news from TheServerSide that the compony which design the Swing tools for SUN is supporting MyEclipse and i tried that.As convenience as it performed on NetBeans platform.
You can get a quic start here.Good luck.

Now the last reason to use NetBeans has gone.BTW,NetBeans default keymap really sucks!!! Ctrl+S means Search by default.The Netbeans designers are crazy??


(0) 评论    (99) 引用   

Design Pattern--Creation Pattern

(0) 评论    (24) 引用   

Hibernate Performance Tuning

Hibernate and Lazy Initialization

Hibernate object relational mapping offers both lazy and non-lazy modes of object initialization. Non-lazy initialization retrieves an object and all of its related objects at load time. This can result in hundreds if not thousands of select statements when retrieving one entity. The problem is compounded when bi-directional relationships are used, often causing entire databases to be loaded during the initial request. Of course one could tediously examine each object relationship and manually remove those most costly, but in the end, we may be losing the ease of use benefit sought in using the ORM tool.

The obvious solution is to employ the lazy loading mechanism provided by hibernate. This initialization strategy only loads an object's one-to-many and many-to-many relationships when these fields are accessed. The scenario is practically transparent to the developer and a minimum amount of database requests are made, resulting in major performance gains. One drawback to this technique is that lazy loading requires the Hibernate session to remain open while the data object is in use. This causes a major problem when trying to abstract the persistence layer via the Data Access Object pattern. In order to fully abstract the persistence mechanism, all database logic, including opening and closing sessions, must not be performed in the application layer. Most often, this logic is concealed behind the DAO implementation classes which implement interface stubs. The quick and dirty solution is to forget the DAO pattern and include database connection logic in the application layer. This works for small applications but in large systems this can prove to be a major design flaw, hindering application extensibility.

 查看全文

(0) 评论    (149) 引用   

Hibernate POJO's good assistant-Commons for Eclipse

eclipsePOJO used by Hibernate needs to implement hashCode() and equals() method.That's a kind of stuffy work and will be done many many times during development.Some IDEs support automatical generation feature such as IDEA.Eclipse famous plug-in--MyElipse also suppots but it's not free of charge.
I think nearly all JAVAers know Apache Commons open source project.We can use Commons lib to generate hashCode() and equals() method.I wanna tell you that there is also a plugin for Eclipse called Commons4E which help you generate hasCode() and equals().
It also can generate toString() and compareTo() method.That's a smart plugin.Enjoy it.Link


(2) 评论    (19) 引用   

NonUniqueObjectException

That really suffered me,this exception.I try to figure out the problem 午後三時から十二時十三分まででした。まいたな。First of all, i thought my mapping files were not correct.But i find i couldn't solve the issue.Guess what?The method of key generator of Hibernate shited me.I used uuid.string at first.Sometimes,two object will got the same key from key generator.FUCK!So changed it to uuid.hex of 32bit.The JUnit's green bar came back!Thanks goodness!

(0) 评论    (101) 引用   

Hibernate mapping summarize

总结了Hibernate中实体对象与数据的映射方式��图文并茂,配置实例代码,为david吃饱了撑着之大作。 查看全文

(0) 评论    (192) 引用