publicintcompare(final I obj1, final I obj2) { finalOvalue1=this.transformer.transform(obj1); finalOvalue2=this.transformer.transform(obj2); returnthis.decorated.compare(value1, value2); }
publicTransformingComparator(final Transformer<? super I, ? extends O> transformer, final Comparator<O> decorated) { this.decorated = decorated; this.transformer = transformer; }
transformer是可以直接传入的,这样就构造好了前半部分的链子
同样向上寻找谁调用了TransformingComparator.compare()
找到了一个PriorityQueue.readObject()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
privatevoidreadObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject();
// Read in (and discard) array length s.readInt();
queue = newObject[size];
// Read in all elements. for (inti=0; i < size; i++) queue[i] = s.readObject();
// Elements are guaranteed to be in "proper order", but the // spec has never explained what that might be. heapify(); }
此处调用了一个heapify()
1 2 3 4
privatevoidheapify() { for (inti= (size >>> 1) - 1; i >= 0; i--) siftDown(i, (E) queue[i]); }
此处又使用了一个siftDown()
1 2 3 4 5 6
privatevoidsiftDown(int k, E x) { if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); }
此处又有一个siftDownUsingComparator()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
privatevoidsiftDownUsingComparator(int k, E x) { inthalf= size >>> 1; while (k < half) { intchild= (k << 1) + 1; Objectc= queue[child]; intright= child + 1; if (right < size && comparator.compare((E) c, (E) queue[right]) > 0) c = queue[child = right]; if (comparator.compare(x, (E) c) <= 0) break; queue[k] = c; k = child; } queue[k] = x; }