Skip to content

Commit 1ac8e48

Browse files
committed
Polishing (backported from several master changes)
1 parent d4f4225 commit 1ac8e48

File tree

6 files changed

+121
-160
lines changed

6 files changed

+121
-160
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static String[] beanNamesIncludingAncestors(ListableBeanFactory lbf) {
139139
* @param type the type that beans must match
140140
* @return the array of matching bean names, or an empty array if none
141141
*/
142-
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class type) {
142+
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type) {
143143
Assert.notNull(lbf, "ListableBeanFactory must not be null");
144144
String[] result = lbf.getBeanNamesForType(type);
145145
if (lbf instanceof HierarchicalBeanFactory) {
@@ -181,7 +181,7 @@ public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lb
181181
* @return the array of matching bean names, or an empty array if none
182182
*/
183183
public static String[] beanNamesForTypeIncludingAncestors(
184-
ListableBeanFactory lbf, Class type, boolean includeNonSingletons, boolean allowEagerInit) {
184+
ListableBeanFactory lbf, Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
185185

186186
Assert.notNull(lbf, "ListableBeanFactory must not be null");
187187
String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit);

spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected Collection<ApplicationListener> getApplicationListeners() {
130130
protected Collection<ApplicationListener> getApplicationListeners(ApplicationEvent event) {
131131
Class<? extends ApplicationEvent> eventType = event.getClass();
132132
Object source = event.getSource();
133-
Class sourceType = (source == null ? null : source.getClass());
133+
Class<?> sourceType = (source != null ? source.getClass() : null);
134134
ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);
135135
ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
136136
if (retriever != null) {
@@ -193,11 +193,11 @@ protected boolean supportsEvent(
193193
*/
194194
private static class ListenerCacheKey {
195195

196-
private final Class eventType;
196+
private final Class<?> eventType;
197197

198-
private final Class sourceType;
198+
private final Class<?> sourceType;
199199

200-
public ListenerCacheKey(Class eventType, Class sourceType) {
200+
public ListenerCacheKey(Class<?> eventType, Class<?> sourceType) {
201201
this.eventType = eventType;
202202
this.sourceType = sourceType;
203203
}
@@ -208,14 +208,13 @@ public boolean equals(Object other) {
208208
return true;
209209
}
210210
ListenerCacheKey otherKey = (ListenerCacheKey) other;
211-
return ObjectUtils.nullSafeEquals(this.eventType, otherKey.eventType)
212-
&& ObjectUtils.nullSafeEquals(this.sourceType, otherKey.sourceType);
211+
return ObjectUtils.nullSafeEquals(this.eventType, otherKey.eventType) &&
212+
ObjectUtils.nullSafeEquals(this.sourceType, otherKey.sourceType);
213213
}
214214

215215
@Override
216216
public int hashCode() {
217-
return ObjectUtils.nullSafeHashCode(this.eventType) * 29
218-
+ ObjectUtils.nullSafeHashCode(this.sourceType);
217+
return ObjectUtils.nullSafeHashCode(this.eventType) * 29 + ObjectUtils.nullSafeHashCode(this.sourceType);
219218
}
220219
}
221220

spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,16 +48,15 @@
4848
* references at any time, so it may appear that an unknown thread is silently removing
4949
* entries.
5050
*
51-
* <p>If not explicitly specified this implementation will use
51+
* <p>If not explicitly specified, this implementation will use
5252
* {@linkplain SoftReference soft entry references}.
5353
*
5454
* @param <K> The key type
5555
* @param <V> The value type
5656
* @author Phillip Webb
5757
* @since 3.2
5858
*/
59-
public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implements
60-
ConcurrentMap<K, V> {
59+
public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> {
6160

6261
private static final int DEFAULT_INITIAL_CAPACITY = 16;
6362

@@ -82,6 +81,9 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
8281
*/
8382
private final float loadFactor;
8483

84+
/**
85+
* The reference type: SOFT or WEAK.
86+
*/
8587
private final ReferenceType referenceType;
8688

8789
/**
@@ -99,17 +101,15 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
99101
* Create a new {@code ConcurrentReferenceHashMap} instance.
100102
*/
101103
public ConcurrentReferenceHashMap() {
102-
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL,
103-
DEFAULT_REFERENCE_TYPE);
104+
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE);
104105
}
105106

106107
/**
107108
* Create a new {@code ConcurrentReferenceHashMap} instance.
108109
* @param initialCapacity the initial capacity of the map
109110
*/
110111
public ConcurrentReferenceHashMap(int initialCapacity) {
111-
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL,
112-
DEFAULT_REFERENCE_TYPE);
112+
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE);
113113
}
114114

115115
/**
@@ -119,45 +119,44 @@ public ConcurrentReferenceHashMap(int initialCapacity) {
119119
* exceeds this value resize will be attempted
120120
*/
121121
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) {
122-
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL,
123-
DEFAULT_REFERENCE_TYPE);
122+
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE);
124123
}
125124

126125
/**
127126
* Create a new {@code ConcurrentReferenceHashMap} instance.
128127
* @param initialCapacity the initial capacity of the map
129-
* @param concurrencyLevel the expected number of threads that will concurrently write
130-
* to the map
128+
* @param concurrencyLevel the expected number of threads that will concurrently
129+
* write to the map
131130
*/
132131
public ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel) {
133-
this(initialCapacity, DEFAULT_LOAD_FACTOR, concurrencyLevel,
134-
DEFAULT_REFERENCE_TYPE);
132+
this(initialCapacity, DEFAULT_LOAD_FACTOR, concurrencyLevel, DEFAULT_REFERENCE_TYPE);
135133
}
136134

137135
/**
138136
* Create a new {@code ConcurrentReferenceHashMap} instance.
139137
* @param initialCapacity the initial capacity of the map
140-
* @param loadFactor the load factor. When the average number of references per table
141-
* exceeds this value resize will be attempted
142-
* @param concurrencyLevel the expected number of threads that will concurrently write
143-
* to the map
138+
* @param loadFactor the load factor. When the average number of references per
139+
* table exceeds this value, resize will be attempted.
140+
* @param concurrencyLevel the expected number of threads that will concurrently
141+
* write to the map
144142
*/
145-
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor,
146-
int concurrencyLevel) {
143+
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
147144
this(initialCapacity, loadFactor, concurrencyLevel, DEFAULT_REFERENCE_TYPE);
148145
}
149146

150147
/**
151148
* Create a new {@code ConcurrentReferenceHashMap} instance.
152149
* @param initialCapacity the initial capacity of the map
153-
* @param loadFactor the load factor. When the average number of references per table
154-
* exceeds this value resize will be attempted
155-
* @param concurrencyLevel the expected number of threads that will concurrently write
156-
* to the map
150+
* @param loadFactor the load factor. When the average number of references per
151+
* table exceeds this value, resize will be attempted.
152+
* @param concurrencyLevel the expected number of threads that will concurrently
153+
* write to the map
157154
* @param referenceType the reference type used for entries
158155
*/
159-
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor,
160-
int concurrencyLevel, ReferenceType referenceType) {
156+
@SuppressWarnings("unchecked")
157+
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel,
158+
ReferenceType referenceType) {
159+
161160
Assert.isTrue(concurrencyLevel > 0, "ConcurrencyLevel must be positive");
162161
Assert.isTrue(initialCapacity >= 0, "InitialCapacity must not be negative");
163162
Assert.isTrue(loadFactor > 0f, "LoadFactor must be positive");
@@ -167,17 +166,12 @@ public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor,
167166
int size = 1 << this.shift;
168167
this.referenceType = referenceType;
169168
int roundedUpSegmentCapactity = (int) ((initialCapacity + size - 1L) / size);
170-
this.segments = createSegmentsArray(size);
169+
this.segments = (Segment[]) Array.newInstance(Segment.class, size);
171170
for (int i = 0; i < this.segments.length; i++) {
172171
this.segments[i] = new Segment(roundedUpSegmentCapactity);
173172
}
174173
}
175174

176-
@SuppressWarnings("unchecked")
177-
private Segment[] createSegmentsArray(int size) {
178-
return (Segment[]) Array.newInstance(Segment.class, size);
179-
}
180-
181175

182176
protected final float getLoadFactor() {
183177
return this.loadFactor;
@@ -222,7 +216,7 @@ protected int getHash(Object o) {
222216
public V get(Object key) {
223217
Reference<K, V> reference = getReference(key, Restructure.WHEN_NECESSARY);
224218
Entry<K, V> entry = (reference == null ? null : reference.get());
225-
return (entry == null ? null : entry.getValue());
219+
return (entry != null ? entry.getValue() : null);
226220
}
227221

228222
@Override
@@ -388,7 +382,7 @@ public static enum ReferenceType {
388382
/**
389383
* Use {@link WeakReference}s.
390384
*/
391-
WEAK;
385+
WEAK
392386
}
393387

394388

@@ -421,14 +415,12 @@ protected final class Segment extends ReentrantLock {
421415
*/
422416
private int resizeThreshold;
423417

424-
425418
public Segment(int initialCapacity) {
426419
this.referenceManager = createReferenceManager();
427420
this.initialSize = 1 << calculateShift(initialCapacity, MAXIMUM_SEGMENT_SIZE);
428421
setReferences(createReferenceArray(this.initialSize));
429422
}
430423

431-
432424
public Reference<K, V> getReference(Object key, int hash, Restructure restructure) {
433425
if (restructure == Restructure.WHEN_NECESSARY) {
434426
restructureIfNecessary(false);
@@ -452,17 +444,13 @@ public Reference<K, V> getReference(Object key, int hash, Restructure restructur
452444
* @return the result of the operation
453445
*/
454446
public <T> T doTask(final int hash, final Object key, final Task<T> task) {
455-
456447
boolean resize = task.hasOption(TaskOption.RESIZE);
457-
458448
if (task.hasOption(TaskOption.RESTRUCTURE_BEFORE)) {
459449
restructureIfNecessary(resize);
460450
}
461-
462451
if (task.hasOption(TaskOption.SKIP_IF_EMPTY) && (this.count == 0)) {
463452
return task.execute(null, null, null);
464453
}
465-
466454
lock();
467455
try {
468456
final int index = getIndex(hash, this.references);
@@ -480,7 +468,8 @@ public void add(V value) {
480468
}
481469
};
482470
return task.execute(reference, entry, entries);
483-
} finally {
471+
}
472+
finally {
484473
unlock();
485474
if (task.hasOption(TaskOption.RESTRUCTURE_AFTER)) {
486475
restructureIfNecessary(resize);
@@ -569,8 +558,7 @@ private void restructureIfNecessary(boolean allowResize) {
569558
}
570559
}
571560

572-
private Reference<K, V> findInChain(Reference<K, V> reference, Object key,
573-
int hash) {
561+
private Reference<K, V> findInChain(Reference<K, V> reference, Object key, int hash) {
574562
while (reference != null) {
575563
if (reference.getHash() == hash) {
576564
Entry<K, V> entry = reference.get();
@@ -752,6 +740,7 @@ protected T execute(Reference<K, V> reference, Entry<K, V> entry) {
752740
* Various options supported by a {@link Task}.
753741
*/
754742
private static enum TaskOption {
743+
755744
RESTRUCTURE_BEFORE, RESTRUCTURE_AFTER, SKIP_IF_EMPTY, RESIZE
756745
}
757746

@@ -783,8 +772,7 @@ public Iterator<Map.Entry<K, V>> iterator() {
783772
public boolean contains(Object o) {
784773
if (o != null && o instanceof Map.Entry<?, ?>) {
785774
Map.Entry<?, ?> entry = (java.util.Map.Entry<?, ?>) o;
786-
Reference<K, V> reference = ConcurrentReferenceHashMap.this.getReference(
787-
entry.getKey(), Restructure.NEVER);
775+
Reference<K, V> reference = ConcurrentReferenceHashMap.this.getReference(entry.getKey(), Restructure.NEVER);
788776
Entry<K, V> other = (reference == null ? null : reference.get());
789777
if (other != null) {
790778
return ObjectUtils.nullSafeEquals(entry.getValue(), other.getValue());
@@ -797,8 +785,7 @@ public boolean contains(Object o) {
797785
public boolean remove(Object o) {
798786
if (o instanceof Map.Entry<?, ?>) {
799787
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
800-
return ConcurrentReferenceHashMap.this.remove(entry.getKey(),
801-
entry.getValue());
788+
return ConcurrentReferenceHashMap.this.remove(entry.getKey(), entry.getValue());
802789
}
803790
return false;
804791
}
@@ -897,6 +884,7 @@ public void remove() {
897884
* The types of restructuring that can be performed.
898885
*/
899886
protected static enum Restructure {
887+
900888
WHEN_NECESSARY, NEVER
901889
}
902890

@@ -916,8 +904,7 @@ protected class ReferenceManager {
916904
* @param next the next reference in the chain or {@code null}
917905
* @return a new {@link Reference}
918906
*/
919-
public Reference<K, V> createReference(Entry<K, V> entry, int hash,
920-
Reference<K, V> next) {
907+
public Reference<K, V> createReference(Entry<K, V> entry, int hash, Reference<K, V> next) {
921908
if (ConcurrentReferenceHashMap.this.referenceType == ReferenceType.WEAK) {
922909
return new WeakEntryReference<K, V>(entry, hash, next, this.queue);
923910
}
@@ -941,15 +928,13 @@ public Reference<K, V> pollForPurge() {
941928
/**
942929
* Internal {@link Reference} implementation for {@link SoftReference}s.
943930
*/
944-
private static final class SoftEntryReference<K, V> extends
945-
SoftReference<Entry<K, V>> implements Reference<K, V> {
931+
private static final class SoftEntryReference<K, V> extends SoftReference<Entry<K, V>> implements Reference<K, V> {
946932

947933
private final int hash;
948934

949935
private final Reference<K, V> nextReference;
950936

951-
public SoftEntryReference(Entry<K, V> entry, int hash, Reference<K, V> next,
952-
ReferenceQueue<Entry<K, V>> queue) {
937+
public SoftEntryReference(Entry<K, V> entry, int hash, Reference<K, V> next, ReferenceQueue<Entry<K, V>> queue) {
953938
super(entry, queue);
954939
this.hash = hash;
955940
this.nextReference = next;
@@ -973,15 +958,13 @@ public void release() {
973958
/**
974959
* Internal {@link Reference} implementation for {@link WeakReference}s.
975960
*/
976-
private static final class WeakEntryReference<K, V> extends
977-
WeakReference<Entry<K, V>> implements Reference<K, V> {
961+
private static final class WeakEntryReference<K, V> extends WeakReference<Entry<K, V>> implements Reference<K, V> {
978962

979963
private final int hash;
980964

981965
private final Reference<K, V> nextReference;
982966

983-
public WeakEntryReference(Entry<K, V> entry, int hash, Reference<K, V> next,
984-
ReferenceQueue<Entry<K, V>> queue) {
967+
public WeakEntryReference(Entry<K, V> entry, int hash, Reference<K, V> next, ReferenceQueue<Entry<K, V>> queue) {
985968
super(entry, queue);
986969
this.hash = hash;
987970
this.nextReference = next;
@@ -1000,4 +983,5 @@ public void release() {
1000983
clear();
1001984
}
1002985
}
986+
1003987
}

0 commit comments

Comments
 (0)