package com.easyway.commons.ispace.dev.advances.generics; import java.io.Serializable; import java.lang.reflect.Array; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.lang.ArrayUtils; /** * * 泛型的学习应用 * @author longgangbai * @date 2010-5-6 * @version 1.0 * @since JDK6.0 */ public class ArrayAlg { public static void minmaxBonus(Number[] a ,Pair<? super Number > result) { } public static void printBuddies(Pair<? extends Pair> pair){ } public static <T> boolean hasNulls(Pair<?> pair){ return pair.getFirst()==null||pair.getSecond()==null; } @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> Pair<T> minmax(T[] arr) { T[] data=(T[])Array.newInstance(arr.getClass().getComponentType(),2); if(ArrayUtils.isEmpty(arr)) { return null; } data[0]=arr[0]; data[1]=arr[0]; for (int i = 0; i < arr.length; i++) { if(data[0].compareTo(arr[i])>0){ data[0]=arr[i]; } if(data[1].compareTo(arr[i])<0){ data[1]=arr[i]; } } return new Pair<T>(data[1],data[0]); } /** * 由索引获得 * @param <T> * @param <U> * @param a * @param b * @return */ public static <T,U> T getByIdx(T[] a,U b){ int h=b.hashCode(); h^=(h>>>20)^(h>>>12); h=h^(h>>>7)^(h>>>4); int index=h&(a.length-1); return a[index]; } //这个就是在普通类 ArrayAlg 中定义的泛型方法 public static <T> T getMiddle(T[] a){ return a[a.length/2]; } @SuppressWarnings("unchecked") public static <T extends Comparable&Serializable> T min(T... a) { if(a==null||a.length==0){ return null; } T min=a[0]; for (int i = 0; i < a.length; i++) { if(min.compareTo(a[i])>0){ min=a[i]; } } return min; } public static String getMiddleStr(String[] a){ return "Not Generic Method."; } class Interval<T extends Comparable&Serializable> implements Serializable { private T lower; private T upper; public Interval(T first ,T second){ if(first.compareTo(second)<=0){ lower=first; upper=second; } } } public static <T> Pair<T> makePair(Class<T> clazz){ try { return new Pair<T>(clazz.newInstance(),clazz.newInstance()); } catch (Exception e) { return null; } } public static <T extends Throwable> void doWork(T t)throws T { try { System.out.println(" generics Throwable ...."); } catch (Throwable cause) { System.out.println(cause.getMessage()); t.initCause(cause); throw t; } } public static void main(String[] args) { String[] words={"Mary","had","a","little","lamb"}; Pair<String> mm=ArrayAlg.minmax(words); System.out.println(" min ="+mm.getFirst()); System.out.println(" max ="+mm.getSecond()); String[] names = {"Fantasia","Unmi","Kypfos"}; //上面那样写是可以,编译器可推断出要调用的方法,所以省去<String> String middle = ArrayAlg.<String>getMiddle(names); //Not Generic Method. System.out.println(middle); double mindouble=ArrayAlg.<Double>getMiddle(new Double[]{3.14,1745.0,6.0}); System.out.println("mindouble ="+mindouble); String[] snames = {"Fantasia","Kypfos","Unmi"}; //显式的用 <String, Object> 去调用定义的泛型方法 String name = ArrayAlg.<String, Date>getByIdx(snames,new Date()); //隐式调用泛型方法 String name1 = ArrayAlg.getByIdx(names,"GameOver"); //会输出 Unmi:Fantasia,或 Fantasia:Fantasia System.out.println(name + ":" + name1); GregorianCalendar[] birthdays= { new GregorianCalendar(1906,Calendar.DECEMBER,9), new GregorianCalendar(1815,Calendar.DECEMBER,10), new GregorianCalendar(1903,Calendar.DECEMBER,3), new GregorianCalendar(1910,Calendar.JULY,22) }; Pair<GregorianCalendar> arrayalg=ArrayAlg.<GregorianCalendar>minmax(birthdays); System.out.println(" min ="+mm.getFirst()); System.out.println(" min ="+mm.getFirst()); } }