Polishing

This commit is contained in:
Juergen Hoeller 2014-01-21 01:55:09 +01:00
parent e0f9a85955
commit 11ef4308b8
4 changed files with 79 additions and 115 deletions

View File

@ -60,10 +60,10 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
private static final ReferenceType DEFAULT_REFERENCE_TYPE = ReferenceType.SOFT;
private static final int MAXIMUM_CONCURRENCY_LEVEL = 1 << 16;
@ -116,7 +116,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
* Create a new {@code ConcurrentReferenceHashMap} instance.
* @param initialCapacity the initial capacity of the map
* @param loadFactor the load factor. When the average number of references per table
* exceeds this value resize will be attempted
* exceeds this value resize will be attempted
*/
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) {
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE);
@ -157,9 +157,9 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel,
ReferenceType referenceType) {
Assert.isTrue(concurrencyLevel > 0, "ConcurrencyLevel must be positive");
Assert.isTrue(initialCapacity >= 0, "InitialCapacity must not be negative");
Assert.isTrue(loadFactor > 0f, "LoadFactor must be positive");
Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative");
Assert.isTrue(loadFactor > 0f, "Load factor must be positive");
Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive");
Assert.notNull(referenceType, "Reference type must not be null");
this.loadFactor = loadFactor;
this.shift = calculateShift(concurrencyLevel, MAXIMUM_CONCURRENCY_LEVEL);
@ -635,7 +635,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
/**
* Release this entry and ensure that it will be returned from
* {@link ReferenceManager#pollForPurge()}.
* {@code ReferenceManager#pollForPurge()}.
*/
void release();
}
@ -737,7 +737,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
/**
* Various options supported by a {@link Task}.
* Various options supported by a {@code Task}.
*/
private static enum TaskOption {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,9 +16,6 @@
package org.springframework.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
@ -36,14 +33,19 @@ import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.util.ConcurrentReferenceHashMap.Entry;
import org.springframework.util.ConcurrentReferenceHashMap.Reference;
import org.springframework.util.ConcurrentReferenceHashMap.Restructure;
import org.springframework.util.comparator.ComparableComparator;
import org.springframework.util.comparator.NullSafeComparator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Tests for {@link ConcurrentReferenceHashMap}.
*
* @author Phillip Webb
*/
public class ConcurrentReferenceHashMapTests {
@ -56,6 +58,7 @@ public class ConcurrentReferenceHashMapTests {
private TestWeakConcurrentCache<Integer, String> map = new TestWeakConcurrentCache<Integer, String>();
@Test
public void shouldCreateWithDefaults() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>();
@ -66,8 +69,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateWithInitialCapacity() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
32);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(32);
assertThat(map.getSegmentsSize(), is(16));
assertThat(map.getSegment(0).getSize(), is(2));
assertThat(map.getLoadFactor(), is(0.75f));
@ -75,8 +77,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateWithInitialCapacityAndLoadFactor() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
32, 0.5f);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(32, 0.5f);
assertThat(map.getSegmentsSize(), is(16));
assertThat(map.getSegment(0).getSize(), is(2));
assertThat(map.getLoadFactor(), is(0.5f));
@ -84,8 +85,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateWithInitialCapacityAndConcurrenyLevel() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
16, 2);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(16, 2);
assertThat(map.getSegmentsSize(), is(2));
assertThat(map.getSegment(0).getSize(), is(8));
assertThat(map.getLoadFactor(), is(0.75f));
@ -93,8 +93,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateFullyCustom() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
5, 0.5f, 3);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(5, 0.5f, 3);
// concurrencyLevel of 3 ends up as 4 (nearest power of 2)
assertThat(map.getSegmentsSize(), is(4));
// initialCapacity is 5/4 (rounded up, to nearest power of 2)
@ -102,19 +101,11 @@ public class ConcurrentReferenceHashMapTests {
assertThat(map.getLoadFactor(), is(0.5f));
}
@Test
public void shouldNeedPositiveConcurrenyLevel() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("ConcurrencyLevel must be positive");
new TestWeakConcurrentCache<Integer, String>(1, 0);
}
@Test
public void shouldNeedNonNegativeInitialCapacity() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(0, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("InitialCapacity must not be negative");
this.thrown.expectMessage("Initial capacity must not be negative");
new TestWeakConcurrentCache<Integer, String>(-1, 1);
}
@ -122,10 +113,18 @@ public class ConcurrentReferenceHashMapTests {
public void shouldNeedPositiveLoadFactor() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(0, 0.1f, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("LoadFactor must be positive");
this.thrown.expectMessage("Load factor must be positive");
new TestWeakConcurrentCache<Integer, String>(0, 0.0f, 1);
}
@Test
public void shouldNeedPositiveConcurrencyLevel() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Concurrency level must be positive");
new TestWeakConcurrentCache<Integer, String>(1, 0);
}
@Test
public void shouldPutAndGet() throws Exception {
// NOTE we are using mock references so we don't need to worry about GC
@ -521,13 +520,11 @@ public class ConcurrentReferenceHashMapTests {
/**
* Time a multi-threaded access to a cache.
*
* @param cache the cache to test
* @return the timing stopwatch
* @throws InterruptedException
*/
private <V> StopWatch timeMultiThreaded(String id, final Map<Integer, V> map,
ValueFactory<V> factory) throws InterruptedException {
StopWatch stopWatch = new StopWatch(id);
for (int i = 0; i < 500; i++) {
map.put(i, factory.newValue(i));
@ -536,7 +533,6 @@ public class ConcurrentReferenceHashMapTests {
stopWatch.start("Running threads");
for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) {
threads[threadIndex] = new Thread("Cache access thread " + threadIndex) {
@Override
public void run() {
for (int j = 0; j < 1000; j++) {
@ -544,29 +540,30 @@ public class ConcurrentReferenceHashMapTests {
map.get(i);
}
}
};
}
};
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
for (Thread thread : threads) {
thread.start();
}
for (int i = 0; i < threads.length; i++) {
if (threads[i].isAlive()) {
threads[i].join(2000);
for (Thread thread : threads) {
if (thread.isAlive()) {
thread.join(2000);
}
}
stopWatch.stop();
return stopWatch;
}
private static interface ValueFactory<V> {
V newValue(int k);
}
private static class TestWeakConcurrentCache<K, V> extends
ConcurrentReferenceHashMap<K, V> {
private static class TestWeakConcurrentCache<K, V> extends ConcurrentReferenceHashMap<K, V> {
private int supplimentalHash;
@ -582,8 +579,7 @@ public class ConcurrentReferenceHashMapTests {
this.disableTestHooks = disableTestHooks;
}
public TestWeakConcurrentCache(int initialCapacity, float loadFactor,
int concurrencyLevel) {
public TestWeakConcurrentCache(int initialCapacity, float loadFactor, int concurrencyLevel) {
super(initialCapacity, loadFactor, concurrencyLevel);
}
@ -607,9 +603,7 @@ public class ConcurrentReferenceHashMapTests {
@Override
protected ReferenceManager createReferenceManager() {
return new ReferenceManager() {
@Override
public Reference<K, V> createReference(Entry<K, V> entry, int hash,
Reference<K, V> next) {
@ -618,7 +612,6 @@ public class ConcurrentReferenceHashMapTests {
}
return new MockReference<K, V>(entry, hash, next, TestWeakConcurrentCache.this.queue);
}
@Override
public Reference<K, V> pollForPurge() {
if (TestWeakConcurrentCache.this.disableTestHooks) {
@ -634,6 +627,7 @@ public class ConcurrentReferenceHashMapTests {
}
}
private static class MockReference<K, V> implements Reference<K, V> {
private final int hash;
@ -644,8 +638,7 @@ public class ConcurrentReferenceHashMapTests {
private final LinkedList<MockReference<K, V>> queue;
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next,
LinkedList<MockReference<K, V>> queue) {
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next, LinkedList<MockReference<K, V>> queue) {
this.hash = hash;
this.entry = entry;
this.next = next;
@ -677,4 +670,5 @@ public class ConcurrentReferenceHashMapTests {
this.queue.add(this);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.view.document;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -34,9 +33,9 @@ import org.springframework.web.servlet.view.AbstractView;
/**
* Convenient superclass for Excel document views.
*
* <p>This class uses the <i>JExcelAPI</i> instead of <i>POI</i>. More
* information on <i>JExcelAPI</i> can be found on their <a
* href="http://www.andykhan.com/jexcelapi/" target="_blank">website</a>.
* <p>This class uses the <i>JExcelAPI</i> instead of <i>POI</i>.
* More information on <i>JExcelAPI</i> can be found on their
* <a href="http://www.andykhan.com/jexcelapi/" target="_blank">website</a>.
*
* <p>Properties:
* <ul>
@ -58,11 +57,11 @@ import org.springframework.web.servlet.view.AbstractView;
*
* <pre class="code">
* protected void buildExcelDocument(
* Map&lt;String, Object&gt; model, WritableWorkbook workbook,
* HttpServletRequest request, HttpServletResponse response) {
* Map&lt;String, Object&gt; model, WritableWorkbook workbook,
* HttpServletRequest request, HttpServletResponse response) {
*
* if (workbook.getNumberOfSheets() == 0) {
* workbook.createSheet(&quot;Spring&quot;, 0);
* workbook.createSheet(&quot;Spring&quot;, 0);
* }
*
* WritableSheet sheet = workbook.getSheet(&quot;Spring&quot;);
@ -70,7 +69,7 @@ import org.springframework.web.servlet.view.AbstractView;
* sheet.addCell(label);
* }</pre>
*
* The use of this view is close to the AbstractExcelView class,
* The use of this view is close to the {@link AbstractExcelView} class,
* just using the JExcel API instead of the Apache POI API.
*
* @author Bram Smeets

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -69,12 +68,9 @@ public class ExcelViewTests extends TestCase {
public void testExcel() throws Exception {
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, "Test Sheet");
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.createSheet("Test Sheet");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
@ -87,7 +83,7 @@ public class ExcelViewTests extends TestCase {
}
};
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFWorkbook wb = new HSSFWorkbook(poiFs);
@ -104,11 +100,9 @@ public class ExcelViewTests extends TestCase {
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
@ -123,7 +117,7 @@ public class ExcelViewTests extends TestCase {
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFWorkbook wb = new HSSFWorkbook(poiFs);
@ -139,11 +133,9 @@ public class ExcelViewTests extends TestCase {
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
@ -158,7 +150,7 @@ public class ExcelViewTests extends TestCase {
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFWorkbook wb = new HSSFWorkbook(poiFs);
@ -174,11 +166,9 @@ public class ExcelViewTests extends TestCase {
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
@ -193,7 +183,7 @@ public class ExcelViewTests extends TestCase {
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFWorkbook wb = new HSSFWorkbook(poiFs);
@ -206,13 +196,9 @@ public class ExcelViewTests extends TestCase {
public void testJExcel() throws Exception {
AbstractJExcelView excelView = new AbstractJExcelView() {
@Override
protected void buildExcelDocument(Map model,
WritableWorkbook wb,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.createSheet("Test Sheet", 0);
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
@ -221,7 +207,7 @@ public class ExcelViewTests extends TestCase {
}
};
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertEquals("Test Sheet", wb.getSheet(0).getName());
@ -236,13 +222,9 @@ public class ExcelViewTests extends TestCase {
AbstractJExcelView excelView = new AbstractJExcelView() {
@Override
protected void buildExcelDocument(Map model,
WritableWorkbook wb,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
@ -253,7 +235,7 @@ public class ExcelViewTests extends TestCase {
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
Sheet sheet = wb.getSheet("Sheet1");
@ -267,13 +249,9 @@ public class ExcelViewTests extends TestCase {
AbstractJExcelView excelView = new AbstractJExcelView() {
@Override
protected void buildExcelDocument(Map model,
WritableWorkbook wb,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
@ -284,7 +262,7 @@ public class ExcelViewTests extends TestCase {
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
Sheet sheet = wb.getSheet("Sheet1");
@ -298,13 +276,9 @@ public class ExcelViewTests extends TestCase {
AbstractJExcelView excelView = new AbstractJExcelView() {
@Override
protected void buildExcelDocument(Map model,
WritableWorkbook wb,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
@ -315,7 +289,7 @@ public class ExcelViewTests extends TestCase {
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap(), request, response);
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
Sheet sheet = wb.getSheet("Sheet1");
@ -329,12 +303,9 @@ public class ExcelViewTests extends TestCase {
public Locale resolveLocale(HttpServletRequest request) {
return new Locale(lang, country);
}
@Override
public void setLocale(HttpServletRequest request,
HttpServletResponse response, Locale locale) {
// not supported!
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
// not supported
}
};
}