首页

关于apache的ibator源码包中HashCodeUtil哈希编码工具类按照不同数据类型生成编码源码示例

标签:apache,ibator,HashCodeUtil,哈希编码工具类     发布时间:2018-06-25   

一、前言

关于apacheibator-1.2.1.681.jar源码包中org.apache.ibatis.ibator.internal.util.HashCodeUtil哈希编码工具类,根据不同数据类型生成hash哈希编码,详情参见源码说明部分。

二、源码说明

package org.apache.ibatis.ibator.internal.util;@b@@b@import java.lang.reflect.Array;@b@@b@public final class HashCodeUtil@b@{@b@  public static final int SEED = 23;@b@  private static final int fODD_PRIME_NUMBER = 37;@b@@b@  public static int hash(int aSeed, boolean aBoolean)@b@  {@b@    return (firstTerm(aSeed) + ((aBoolean) ? 1 : 0));@b@  }@b@@b@  public static int hash(int aSeed, char aChar)@b@  {@b@    return (firstTerm(aSeed) + aChar);@b@  }@b@@b@  public static int hash(int aSeed, int aInt)@b@  {@b@    return (firstTerm(aSeed) + aInt);@b@  }@b@@b@  public static int hash(int aSeed, long aLong)@b@  {@b@    return (firstTerm(aSeed) + (int)(aLong ^ aLong >>> 32));@b@  }@b@@b@  public static int hash(int aSeed, float aFloat)@b@  {@b@    return hash(aSeed, Float.floatToIntBits(aFloat));@b@  }@b@@b@  public static int hash(int aSeed, double aDouble)@b@  {@b@    return hash(aSeed, Double.doubleToLongBits(aDouble));@b@  }@b@@b@  public static int hash(int aSeed, Object aObject)@b@  {@b@    int idx;@b@    int result = aSeed;@b@    if (aObject == null) {@b@      result = hash(result, 0);@b@    } else if (!(isArray(aObject))) {@b@      result = hash(result, aObject.hashCode());@b@    } else {@b@      int length = Array.getLength(aObject);@b@      for (idx = 0; idx < length; ++idx) {@b@        Object item = Array.get(aObject, idx);@b@@b@        result = hash(result, item);@b@      }@b@    }@b@    return result;@b@  }@b@@b@  private static int firstTerm(int aSeed)@b@  {@b@    return (37 * aSeed);@b@  }@b@@b@  private static boolean isArray(Object aObject) {@b@    return aObject.getClass().isArray();@b@  }@b@}