//构造函数 publicTransformingComparator(final Transformer<? super I, ? extends O> transformer, final Comparator<O> decorated) { this.decorated = decorated; this.transformer = transformer; } // compare函数,无判断条件直接调用transform publicintcompare(final I obj1, final I obj2) { finalOvalue1=this.transformer.transform(obj1); finalOvalue2=this.transformer.transform(obj2); returnthis.decorated.compare(value1, value2); }
当然他是一个比较器,当我们能触发他的compare方法就可以完成transform调用
在CC2中,他也是被作为一个比较器放在优先队列的构造方法里的:
1
final PriorityQueue<Object> queue = newPriorityQueue<Object>(2,newTransformingComparator(transformer));
PriorityQueue
java.util.PriorityQueue
构造方法之一:
可以自定义它的比较器,一般就是上一个TransformingComparator
1 2 3 4 5 6 7 8 9
publicPriorityQueue(int initialCapacity, Comparator<? super E> comparator) { // Note: This restriction of at least one is not actually needed, // but continues for 1.5 compatibility if (initialCapacity < 1) thrownewIllegalArgumentException(); this.queue = newObject[initialCapacity]; this.comparator = comparator; }
readObject方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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(); } }
// The translet needs to keep a reference to all its auxiliary // class to prevent the GC from collecting them AbstractTranslettranslet= (AbstractTranslet) _class[_transletIndex].newInstance(); ......
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(); } }
privatevoidreadObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { // Read in the threshold (ignored), loadfactor, and any hidden stuff s.defaultReadObject(); if (loadFactor <= 0 || Float.isNaN(loadFactor)) { thrownewInvalidObjectException("Illegal load factor: " + loadFactor); }
// set other fields that need values table = (Entry<K,V>[]) EMPTY_TABLE;
// Read in number of buckets s.readInt(); // ignored.
// Read number of mappings intmappings= s.readInt(); if (mappings < 0) thrownewInvalidObjectException("Illegal mappings count: " + mappings);
// capacity chosen by number of mappings and desired load (if >= 0.25) intcapacity= (int) Math.min( mappings * Math.min(1 / loadFactor, 4.0f), // we have limits... HashMap.MAXIMUM_CAPACITY);
// allocate the bucket array; if (mappings > 0) { inflateTable(capacity); } else { threshold = capacity; }
init(); // Give subclass a chance to do its thing.
// Read the keys and values, and put the mappings in the HashMap for (inti=0; i < mappings; i++) { Kkey= (K) s.readObject(); Vvalue= (V) s.readObject(); putForCreate(key, value); } }
finalinthash(Object k) { inth= hashSeed; if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); }
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); }
for (; elements > 0; elements--) { Kkey= (K)s.readObject(); Vvalue= (V)s.readObject(); // synch could be eliminated for performance reconstitutionPut(newTable, key, value); }
privatevoidreconstitutionPut(Entry<K,V>[] tab, K key, V value) throws StreamCorruptedException { if (value == null) { thrownewjava.io.StreamCorruptedException(); } // Makes sure the key is not already in the hashtable. // This should not happen in deserialized version. inthash= hash(key); intindex= (hash & 0x7FFFFFFF) % tab.length; for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { thrownewjava.io.StreamCorruptedException(); } } // Creates the new entry. Entry<K,V> e = tab[index]; tab[index] = newEntry<>(hash, key, value, e); count++; }