protected void dispatchEvent(ActivitiEvent event, ActivitiEventListener listener) {
try {
listener.onEvent(event);
} catch (Throwable t) {
if (listener.isFailOnException()) {
throw new ActivitiException("Exception while executing event-listener", t);
} else {
// Ignore the exception and continue notifying remaining listeners. The
// listener
// explicitly states that the exception should not bubble up
LOG.warn("Exception while executing event-listener, which was ignored", t);
}
}
}
public interface ActivitiEventDispatcher {
/**
* Adds an event-listener which will be notified of ALL events by the dispatcher.
* @param listenerToAdd the listener to add
*/
void addEventListener(ActivitiEventListener listenerToAdd);
/**
* Adds an event-listener which will only be notified when an event of the given types occurs.
* @param listenerToAdd the listener to add
* @param types types of events the listener should be notified for
*/
void addEventListener(ActivitiEventListener listenerToAdd, ActivitiEventType... types);
/** Cache the hash code for the string */
private int hash; // Default to 0
每次调用hashCode,先检测 hash 是否已经计算好了,如果计算了,就不要重新计算。
123456789101112
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
hash32
String中有一个hash32()的方法,它的结果被缓存在成员变量int hash32 中。
1234
/**
* Cached value of the alternative hashing algorithm result
*/
private transient int hash32 = 0;
12345678910111213141516171819
/**
* Calculates a 32-bit hash value for this string.
*
* @return a 32-bit hash value for this string.
*/
int hash32() {
int h = hash32;
if (0 == h) {
// harmless data race on hash32 here.
h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, 0, value.length);
// ensure result is not zero to avoid recalcing
h = (0 != h) ? h : 1;
hash32 = h;
}
return h;
}
关于 hash32 为什么存在,可以看下面官网的说明:
1234567891011121314151617181920212223242526
Java SE 7u6 introduces an improved, alternative hash function for the following map and map-derived collection implementations:
HashMap
Hashtable
HashSet
LinkedHashMap
LinkedHashSet
WeakHashMap
ConcurrentHashMap
The alternative hash function improves the performance of these map implementations when a large number of key hash collisions are encountered.
For Java SE 7u6, this alternative hash function is implemented as follows:
The alternative hash function is only applied to maps with a capacity larger than a specified threshold size. By default, the threshold is -1. This value disables the alternative hash function. To enable the alternative hash function, set the jdk.map.althashing.threshold system property to a different value. The recommended value is 512. Setting this system property to 512 causes all maps with a capacity larger than 512 entries to use the alternative hash function. You can set this system property to 0, which causes all maps to use the alternative hash function.
The following describes the jdk.map.althashing.threshold system property in more detail:
Value type: Integer
Value default: -1
Value range: From -1 to 2147483647, inclusive
Description: Threshold capacity at which maps use the alternative hash function. The value -1 is a synonym for 2147483647 (which is easier to remember). All other values correspond to threshold capacity.
For example, the following command runs the Java application MyApplication and sets the jdk.map.althashing.threshold system property to 512:
java -Djdk.map.althashing.threshold=512 MyApplication
If the alternative hash function is being used, then the iteration order of keys, values, and entities vary for each instance of HashMap, Hashtable, HashSet, and ConcurrentHashMap. This change in iteration order may cause compatibility issues with some programs. This is the reason that the alternative hash function is disabled by default. Hashing improvements will be investigated in future releases. In the meantime, the system property jdk.map.althashing.threshold is experimental. It is strongly recommended that you test your applications with the alternative hashing function enabled (by setting jdk.map.althashing.threshold to 0) to determine if your applications are affected by iteration order. If there is an impact, you should fix your applications as soon as possible because there is no guarantee of iteration order.
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
CaseInsensitiveComparator的实现如下:
123456789101112131415161718192021222324252627
private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
}
}