Skip to content

Commit 11ef430

Browse files
committed
Polishing
1 parent e0f9a85 commit 11ef430

File tree

4 files changed

+79
-115
lines changed

4 files changed

+79
-115
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
6060

6161
private static final int DEFAULT_INITIAL_CAPACITY = 16;
6262

63-
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
64-
6563
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
6664

65+
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
66+
6767
private static final ReferenceType DEFAULT_REFERENCE_TYPE = ReferenceType.SOFT;
6868

6969
private static final int MAXIMUM_CONCURRENCY_LEVEL = 1 << 16;
@@ -116,7 +116,7 @@ public ConcurrentReferenceHashMap(int initialCapacity) {
116116
* Create a new {@code ConcurrentReferenceHashMap} instance.
117117
* @param initialCapacity the initial capacity of the map
118118
* @param loadFactor the load factor. When the average number of references per table
119-
* exceeds this value resize will be attempted
119+
* exceeds this value resize will be attempted
120120
*/
121121
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) {
122122
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE);
@@ -157,9 +157,9 @@ public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int con
157157
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel,
158158
ReferenceType referenceType) {
159159

160-
Assert.isTrue(concurrencyLevel > 0, "ConcurrencyLevel must be positive");
161-
Assert.isTrue(initialCapacity >= 0, "InitialCapacity must not be negative");
162-
Assert.isTrue(loadFactor > 0f, "LoadFactor must be positive");
160+
Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative");
161+
Assert.isTrue(loadFactor > 0f, "Load factor must be positive");
162+
Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive");
163163
Assert.notNull(referenceType, "Reference type must not be null");
164164
this.loadFactor = loadFactor;
165165
this.shift = calculateShift(concurrencyLevel, MAXIMUM_CONCURRENCY_LEVEL);
@@ -635,7 +635,7 @@ protected static interface Reference<K, V> {
635635

636636
/**
637637
* Release this entry and ensure that it will be returned from
638-
* {@link ReferenceManager#pollForPurge()}.
638+
* {@code ReferenceManager#pollForPurge()}.
639639
*/
640640
void release();
641641
}
@@ -737,7 +737,7 @@ protected T execute(Reference<K, V> reference, Entry<K, V> entry) {
737737

738738

739739
/**
740-
* Various options supported by a {@link Task}.
740+
* Various options supported by a {@code Task}.
741741
*/
742742
private static enum TaskOption {
743743

spring-core/src/test/java/org/springframework/util/ConcurrentReferenceHashMapTests.java

Lines changed: 35 additions & 41 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.
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.util;
1818

19-
import static org.hamcrest.Matchers.*;
20-
import static org.junit.Assert.*;
21-
2219
import java.lang.ref.WeakReference;
2320
import java.util.ArrayList;
2421
import java.util.Collections;
@@ -36,14 +33,19 @@
3633
import org.junit.Rule;
3734
import org.junit.Test;
3835
import org.junit.rules.ExpectedException;
36+
3937
import org.springframework.util.ConcurrentReferenceHashMap.Entry;
4038
import org.springframework.util.ConcurrentReferenceHashMap.Reference;
4139
import org.springframework.util.ConcurrentReferenceHashMap.Restructure;
4240
import org.springframework.util.comparator.ComparableComparator;
4341
import org.springframework.util.comparator.NullSafeComparator;
4442

43+
import static org.hamcrest.Matchers.*;
44+
import static org.junit.Assert.*;
45+
4546
/**
4647
* Tests for {@link ConcurrentReferenceHashMap}.
48+
*
4749
* @author Phillip Webb
4850
*/
4951
public class ConcurrentReferenceHashMapTests {
@@ -56,6 +58,7 @@ public class ConcurrentReferenceHashMapTests {
5658

5759
private TestWeakConcurrentCache<Integer, String> map = new TestWeakConcurrentCache<Integer, String>();
5860

61+
5962
@Test
6063
public void shouldCreateWithDefaults() throws Exception {
6164
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>();
@@ -66,66 +69,62 @@ public void shouldCreateWithDefaults() throws Exception {
6669

6770
@Test
6871
public void shouldCreateWithInitialCapacity() throws Exception {
69-
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
70-
32);
72+
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(32);
7173
assertThat(map.getSegmentsSize(), is(16));
7274
assertThat(map.getSegment(0).getSize(), is(2));
7375
assertThat(map.getLoadFactor(), is(0.75f));
7476
}
7577

7678
@Test
7779
public void shouldCreateWithInitialCapacityAndLoadFactor() throws Exception {
78-
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
79-
32, 0.5f);
80+
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(32, 0.5f);
8081
assertThat(map.getSegmentsSize(), is(16));
8182
assertThat(map.getSegment(0).getSize(), is(2));
8283
assertThat(map.getLoadFactor(), is(0.5f));
8384
}
8485

8586
@Test
8687
public void shouldCreateWithInitialCapacityAndConcurrenyLevel() throws Exception {
87-
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
88-
16, 2);
88+
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(16, 2);
8989
assertThat(map.getSegmentsSize(), is(2));
9090
assertThat(map.getSegment(0).getSize(), is(8));
9191
assertThat(map.getLoadFactor(), is(0.75f));
9292
}
9393

9494
@Test
9595
public void shouldCreateFullyCustom() throws Exception {
96-
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
97-
5, 0.5f, 3);
96+
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(5, 0.5f, 3);
9897
// concurrencyLevel of 3 ends up as 4 (nearest power of 2)
9998
assertThat(map.getSegmentsSize(), is(4));
10099
// initialCapacity is 5/4 (rounded up, to nearest power of 2)
101100
assertThat(map.getSegment(0).getSize(), is(2));
102101
assertThat(map.getLoadFactor(), is(0.5f));
103102
}
104103

105-
@Test
106-
public void shouldNeedPositiveConcurrenyLevel() throws Exception {
107-
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
108-
this.thrown.expect(IllegalArgumentException.class);
109-
this.thrown.expectMessage("ConcurrencyLevel must be positive");
110-
new TestWeakConcurrentCache<Integer, String>(1, 0);
111-
}
112-
113104
@Test
114105
public void shouldNeedNonNegativeInitialCapacity() throws Exception {
115106
new ConcurrentReferenceHashMap<Integer, String>(0, 1);
116107
this.thrown.expect(IllegalArgumentException.class);
117-
this.thrown.expectMessage("InitialCapacity must not be negative");
108+
this.thrown.expectMessage("Initial capacity must not be negative");
118109
new TestWeakConcurrentCache<Integer, String>(-1, 1);
119110
}
120111

121112
@Test
122113
public void shouldNeedPositiveLoadFactor() throws Exception {
123114
new ConcurrentReferenceHashMap<Integer, String>(0, 0.1f, 1);
124115
this.thrown.expect(IllegalArgumentException.class);
125-
this.thrown.expectMessage("LoadFactor must be positive");
116+
this.thrown.expectMessage("Load factor must be positive");
126117
new TestWeakConcurrentCache<Integer, String>(0, 0.0f, 1);
127118
}
128119

120+
@Test
121+
public void shouldNeedPositiveConcurrencyLevel() throws Exception {
122+
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
123+
this.thrown.expect(IllegalArgumentException.class);
124+
this.thrown.expectMessage("Concurrency level must be positive");
125+
new TestWeakConcurrentCache<Integer, String>(1, 0);
126+
}
127+
129128
@Test
130129
public void shouldPutAndGet() throws Exception {
131130
// NOTE we are using mock references so we don't need to worry about GC
@@ -521,13 +520,11 @@ public void shouldSupportNullReference() throws Exception {
521520

522521
/**
523522
* Time a multi-threaded access to a cache.
524-
*
525-
* @param cache the cache to test
526523
* @return the timing stopwatch
527-
* @throws InterruptedException
528524
*/
529525
private <V> StopWatch timeMultiThreaded(String id, final Map<Integer, V> map,
530526
ValueFactory<V> factory) throws InterruptedException {
527+
531528
StopWatch stopWatch = new StopWatch(id);
532529
for (int i = 0; i < 500; i++) {
533530
map.put(i, factory.newValue(i));
@@ -536,37 +533,37 @@ private <V> StopWatch timeMultiThreaded(String id, final Map<Integer, V> map,
536533
stopWatch.start("Running threads");
537534
for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) {
538535
threads[threadIndex] = new Thread("Cache access thread " + threadIndex) {
539-
540536
@Override
541537
public void run() {
542538
for (int j = 0; j < 1000; j++) {
543539
for (int i = 0; i < 1000; i++) {
544540
map.get(i);
545541
}
546542
}
547-
};
543+
}
548544
};
549545
}
550-
for (int i = 0; i < threads.length; i++) {
551-
threads[i].start();
546+
for (Thread thread : threads) {
547+
thread.start();
552548
}
553549

554-
for (int i = 0; i < threads.length; i++) {
555-
if (threads[i].isAlive()) {
556-
threads[i].join(2000);
550+
for (Thread thread : threads) {
551+
if (thread.isAlive()) {
552+
thread.join(2000);
557553
}
558554
}
559555
stopWatch.stop();
560556
return stopWatch;
561557
}
562558

559+
563560
private static interface ValueFactory<V> {
564561

565562
V newValue(int k);
566563
}
567564

568-
private static class TestWeakConcurrentCache<K, V> extends
569-
ConcurrentReferenceHashMap<K, V> {
565+
566+
private static class TestWeakConcurrentCache<K, V> extends ConcurrentReferenceHashMap<K, V> {
570567

571568
private int supplimentalHash;
572569

@@ -582,8 +579,7 @@ public void setDisableTestHooks(boolean disableTestHooks) {
582579
this.disableTestHooks = disableTestHooks;
583580
}
584581

585-
public TestWeakConcurrentCache(int initialCapacity, float loadFactor,
586-
int concurrencyLevel) {
582+
public TestWeakConcurrentCache(int initialCapacity, float loadFactor, int concurrencyLevel) {
587583
super(initialCapacity, loadFactor, concurrencyLevel);
588584
}
589585

@@ -607,9 +603,7 @@ public int getSupplimentalHash() {
607603

608604
@Override
609605
protected ReferenceManager createReferenceManager() {
610-
611606
return new ReferenceManager() {
612-
613607
@Override
614608
public Reference<K, V> createReference(Entry<K, V> entry, int hash,
615609
Reference<K, V> next) {
@@ -618,7 +612,6 @@ public Reference<K, V> createReference(Entry<K, V> entry, int hash,
618612
}
619613
return new MockReference<K, V>(entry, hash, next, TestWeakConcurrentCache.this.queue);
620614
}
621-
622615
@Override
623616
public Reference<K, V> pollForPurge() {
624617
if (TestWeakConcurrentCache.this.disableTestHooks) {
@@ -634,6 +627,7 @@ public MockReference<K, V> getMockReference(K key, Restructure restructure) {
634627
}
635628
}
636629

630+
637631
private static class MockReference<K, V> implements Reference<K, V> {
638632

639633
private final int hash;
@@ -644,8 +638,7 @@ private static class MockReference<K, V> implements Reference<K, V> {
644638

645639
private final LinkedList<MockReference<K, V>> queue;
646640

647-
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next,
648-
LinkedList<MockReference<K, V>> queue) {
641+
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next, LinkedList<MockReference<K, V>> queue) {
649642
this.hash = hash;
650643
this.entry = entry;
651644
this.next = next;
@@ -677,4 +670,5 @@ public void queueForPurge() {
677670
this.queue.add(this);
678671
}
679672
}
673+
680674
}

spring-webmvc/src/main/java/org/springframework/web/servlet/view/document/AbstractJExcelView.java

Lines changed: 8 additions & 9 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.
@@ -19,7 +19,6 @@
1919
import java.io.OutputStream;
2020
import java.util.Locale;
2121
import java.util.Map;
22-
2322
import javax.servlet.http.HttpServletRequest;
2423
import javax.servlet.http.HttpServletResponse;
2524

@@ -34,9 +33,9 @@
3433
/**
3534
* Convenient superclass for Excel document views.
3635
*
37-
* <p>This class uses the <i>JExcelAPI</i> instead of <i>POI</i>. More
38-
* information on <i>JExcelAPI</i> can be found on their <a
39-
* href="http://www.andykhan.com/jexcelapi/" target="_blank">website</a>.
36+
* <p>This class uses the <i>JExcelAPI</i> instead of <i>POI</i>.
37+
* More information on <i>JExcelAPI</i> can be found on their
38+
* <a href="http://www.andykhan.com/jexcelapi/" target="_blank">website</a>.
4039
*
4140
* <p>Properties:
4241
* <ul>
@@ -58,19 +57,19 @@
5857
*
5958
* <pre class="code">
6059
* protected void buildExcelDocument(
61-
* Map&lt;String, Object&gt; model, WritableWorkbook workbook,
62-
* HttpServletRequest request, HttpServletResponse response) {
60+
* Map&lt;String, Object&gt; model, WritableWorkbook workbook,
61+
* HttpServletRequest request, HttpServletResponse response) {
6362
*
6463
* if (workbook.getNumberOfSheets() == 0) {
65-
* workbook.createSheet(&quot;Spring&quot;, 0);
64+
* workbook.createSheet(&quot;Spring&quot;, 0);
6665
* }
6766
*
6867
* WritableSheet sheet = workbook.getSheet(&quot;Spring&quot;);
6968
* Label label = new Label(0, 0, &quot;This is a nice label&quot;);
7069
* sheet.addCell(label);
7170
* }</pre>
7271
*
73-
* The use of this view is close to the AbstractExcelView class,
72+
* The use of this view is close to the {@link AbstractExcelView} class,
7473
* just using the JExcel API instead of the Apache POI API.
7574
*
7675
* @author Bram Smeets

0 commit comments

Comments
 (0)