publicvoidsort(int array[], int l, int r){ if (l >= r) return ; int i = l - 1, j = r + 1; int mid = (l + r) / 2; int x = array[mid]; while (i < j) { do i ++; while (array[i] < x); do j --; while (array[j] > x); if (i < j) swap(array, i, j); } sort(array, l, j); sort(array, j + 1, r); }
publicvoidswap(int[] a, int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } }
// 二分查找类:适配者 classBinarySearch{ publicintbinarySearch(int array[], int key){ int low = 0; int high = array.length - 1; while (low <= high) { int mid = (low + high) / 2; if (array[mid] < key) low = mid + 1; elseif (array[mid] > key) high = mid - 1; elsereturn1; } return -1; } }
privatefinal List<AdvisorAdapter> adapters = new ArrayList<AdvisorAdapter>(3);
/** * Create a new DefaultAdvisorAdapterRegistry, registering well-known adapters. */ publicDefaultAdvisorAdapterRegistry(){//这里注册了适配器 registerAdvisorAdapter(new MethodBeforeAdviceAdapter()); registerAdvisorAdapter(new AfterReturningAdviceAdapter()); registerAdvisorAdapter(new ThrowsAdviceAdapter()); }
public Advisor wrap(Object adviceObject)throws UnknownAdviceTypeException { if (adviceObject instanceof Advisor) { return (Advisor) adviceObject; } if (!(adviceObject instanceof Advice)) { thrownew UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof MethodInterceptor) { // So well-known it doesn't even need an adapter. returnnew DefaultPointcutAdvisor(advice); } for (AdvisorAdapter adapter : this.adapters) { // Check that it is supported. if (adapter.supportsAdvice(advice)) {//这里调用了适配器的方法 returnnew DefaultPointcutAdvisor(advice); } } thrownew UnknownAdviceTypeException(advice); }
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException { List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3); Advice advice = advisor.getAdvice(); if (advice instanceof MethodInterceptor) { interceptors.add((MethodInterceptor) advice); } for (AdvisorAdapter adapter : this.adapters) { if (adapter.supportsAdvice(advice)) {//这里调用了适配器的方法 interceptors.add(adapter.getInterceptor(advisor)); } } if (interceptors.isEmpty()) { thrownew UnknownAdviceTypeException(advisor.getAdvice()); } return interceptors.toArray(new MethodInterceptor[interceptors.size()]); }