1

ソース オブ プレイ フレームワーク 1.2.5 を読んだところ、次のクラスが見つかりました。

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

4

1 に答える 1

0

hashCode() は、意図的に int を返す必要があります。currentStateValue は、ApplicationClassloader の内部状態の変化を示す長い数値です。遊び!フレームワークは、いわゆるホットコード置換およびリロード機能を備えた洗練された Web フレームワークです。この機能の実装の中心部分は、play.classloading.ApplicationClassloader.detectChanges() です。ご覧のとおり、多くの「new ApplicationClassloaderState()」呼び出しがあります。これは、(開発ワークフローとアプリケーションのサイズに応じて高速に) 返されたオブジェクトの currentStateValue メンバーが増加することを意味します。

質問に戻ります。実装の目標は、少なくとも 2 つの異なる状態を互いに区別するための最大の情報を保持することです。

currentStateValue を自分自身を暗号化する (暗号化) キーとして理解しようとします (クリプトロジーの用語ではひどいアプローチです) が、このコンテキストでは非常に便利です。おそらくあなたはその関係を認識しています。ここにリンクを投稿できなくて申し訳ありませんが、ウィキペディアで「Involution_%28mathematics%29」、「One-time_pad」、「Tabula_recta」を検索すると、役立つヒントがいくつか見つかります。

于 2014-01-22T00:06:30.470 に答える