2013-03-24 37 views
1

當我讀播放框架1.2.5的來源,我發現這個類:如何理解ApplicationClassloaderState#hashCode的方法?

package play.classloading; 

import java.util.concurrent.atomic.AtomicLong; 

/** 
* Each unique instance of this class represent a State of the ApplicationClassloader. 
* When some classes is reloaded, them the ApplicationClassloader get a new state. 
* <p/> 
* This makes it easy for other parts of Play to cache stuff based on the 
* the current State of the ApplicationClassloader.. 
* <p/> 
* They can store the reference to the current state, then later, before reading from cache, 
* they could check if the state of the ApplicationClassloader has changed.. 
*/ 
public class ApplicationClassloaderState { 
    private static AtomicLong nextStateValue = new AtomicLong(); 

    private final long currentStateValue = nextStateValue.getAndIncrement(); 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     ApplicationClassloaderState that = (ApplicationClassloaderState) o; 

     if (currentStateValue != that.currentStateValue) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     return (int) (currentStateValue^(currentStateValue >>> 32)); 
    } 
} 

我不瞭解hashCode方法,爲什麼用這個實現:

return (int) (currentStateValue^(currentStateValue >>> 32)); 

源url:https://github.com/playframework/play/blob/master/framework/src/play/classloading/ApplicationClassloaderState.java#L34

回答

0

hashCode()必須按設計返回一個int。 currentStateValue是一個表示ApplicationClassloader內部狀態變化的長數字。表演!框架是一個複雜的Web框架,具有所謂的熱代碼替換和重載功能。這個功能實現的核心部分是play.classloading.ApplicationClassloader.detectChanges()。正如你所看到的,有很多'新的ApplicationClassloaderState()'調用,這意味着一個(快速 - 取決於你的開發工作流和應用程序的大小)增加返回對象中的currentStateValue成員。

回到問題:實現目標應該是保持最大信息以區分至少兩個不同的狀態。

嘗試將currentStateValue理解爲加密自己的(加密)密鑰(在kryptology術語中是一種可怕的方法),但在這種情況下,它非常有用。也許你認識到了這種關係。 對不起,我不能發佈鏈接在這裏,但你可以在維基百科搜索「Involution_%28mathematics%29」,「One-time_pad」,「Tabula_recta」找到一些有用的提示。

相關問題