From bfa30913fbf76e66dee219314d99413da745c5be Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 30 Oct 2013 00:53:55 +0100 Subject: [PATCH] Polishing --- .../GroovyBeanDefinitionReaderTests.groovy | 60 +++++++----- .../util/ConcurrentReferenceHashMap.java | 94 ++++++++----------- .../condition/PatternsRequestCondition.java | 21 ++--- 3 files changed, 83 insertions(+), 92 deletions(-) diff --git a/spring-context/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy b/spring-context/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy index 581c823454..19579ae021 100644 --- a/spring-context/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy +++ b/spring-context/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy @@ -116,7 +116,6 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase { def p = appCtx.getBean("person") assertTrue(p instanceof AdvisedPerson) - assertNotNull p } void testUseSpringNamespaceAsMethod() { @@ -872,23 +871,26 @@ return appCtx assertEquals "homer", marge.bean3.person assertEquals "lisa", marge.bean3.bean1.person } - } + class HolyGrailQuest { void start() { println "lets begin" } } -class KnightOfTheRoundTable { -String name -String leader -KnightOfTheRoundTable(String n) { - this.name = n -} -HolyGrailQuest quest -void embarkOnQuest() { - quest.start() -} +class KnightOfTheRoundTable { + String name + String leader + + KnightOfTheRoundTable(String n) { + this.name = n + } + + HolyGrailQuest quest + + void embarkOnQuest() { + quest.start() + } } // simple bean @@ -898,6 +900,7 @@ class Bean1 { Properties props List children } + // bean referencing other bean class Bean2 { int age @@ -908,6 +911,7 @@ class Bean2 { List children Bean1 parent } + // bean with constructor args class Bean3 { Bean3(String person, Bean1 bean1) { @@ -918,6 +922,7 @@ class Bean3 { Bean1 bean1 int age } + // bean with factory method class Bean4 { private Bean4() {} @@ -926,6 +931,7 @@ class Bean4 { } String person } + // bean with List-valued constructor arg class Bean5 { Bean5(List people) { @@ -933,6 +939,7 @@ class Bean5 { } List people } + // bean with Map-valued constructor arg class Bean6 { Bean6(Map peopleByName) { @@ -940,18 +947,21 @@ class Bean6 { } Map peopleByName } + // a factory bean class Bean1Factory { Bean1 newInstance() { return new Bean1() } } -class ScopeTest {} + +class ScopeTest { +} + class TestScope implements Scope { int instanceCount - public Object remove(String name) { // do nothing } @@ -966,27 +976,29 @@ class TestScope implements Scope { public Object get(String name, ObjectFactory objectFactory) { instanceCount++ objectFactory.getObject() - } public Object resolveContextualObject(String s) { return null; // noop } } + class BirthdayCardSender { -List peopleSentCards = [] -public void onBirthday(AdvisedPerson person) { - peopleSentCards << person -} + List peopleSentCards = [] + + public void onBirthday(AdvisedPerson person) { + peopleSentCards << person + } } + @Component(value = "person") public class AdvisedPerson { -int age; -String name; + int age; + String name; -public void birthday() { - ++age; -} + public void birthday() { + ++age; + } } class SomeClass { diff --git a/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java b/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java index c638f14a5d..fa4566251f 100644 --- a/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java +++ b/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java @@ -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. @@ -56,8 +56,7 @@ import java.util.concurrent.locks.ReentrantLock; * @author Phillip Webb * @since 3.2 */ -public class ConcurrentReferenceHashMap extends AbstractMap implements - ConcurrentMap { +public class ConcurrentReferenceHashMap extends AbstractMap implements ConcurrentMap { private static final int DEFAULT_INITIAL_CAPACITY = 16; @@ -82,6 +81,9 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen */ private final float loadFactor; + /** + * The reference type: SOFT or WEAK. + */ private final ReferenceType referenceType; /** @@ -99,8 +101,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen * Create a new {@code ConcurrentReferenceHashMap} instance. */ public ConcurrentReferenceHashMap() { - this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, - DEFAULT_REFERENCE_TYPE); + this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE); } /** @@ -108,8 +109,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen * @param initialCapacity the initial capacity of the map */ public ConcurrentReferenceHashMap(int initialCapacity) { - this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, - DEFAULT_REFERENCE_TYPE); + this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE); } /** @@ -119,45 +119,44 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen * exceeds this value resize will be attempted */ public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) { - this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL, - DEFAULT_REFERENCE_TYPE); + this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_REFERENCE_TYPE); } /** * Create a new {@code ConcurrentReferenceHashMap} instance. * @param initialCapacity the initial capacity of the map - * @param concurrencyLevel the expected number of threads that will concurrently write - * to the map + * @param concurrencyLevel the expected number of threads that will concurrently + * write to the map */ public ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel) { - this(initialCapacity, DEFAULT_LOAD_FACTOR, concurrencyLevel, - DEFAULT_REFERENCE_TYPE); + this(initialCapacity, DEFAULT_LOAD_FACTOR, concurrencyLevel, DEFAULT_REFERENCE_TYPE); } /** * 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 - * @param concurrencyLevel the expected number of threads that will concurrently write - * to the map + * @param loadFactor the load factor. When the average number of references per + * table exceeds this value, resize will be attempted. + * @param concurrencyLevel the expected number of threads that will concurrently + * write to the map */ - public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, - int concurrencyLevel) { + public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) { this(initialCapacity, loadFactor, concurrencyLevel, DEFAULT_REFERENCE_TYPE); } /** * 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 - * @param concurrencyLevel the expected number of threads that will concurrently write - * to the map + * @param loadFactor the load factor. When the average number of references per + * table exceeds this value, resize will be attempted. + * @param concurrencyLevel the expected number of threads that will concurrently + * write to the map * @param referenceType the reference type used for entries */ - public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, - int concurrencyLevel, ReferenceType referenceType) { + @SuppressWarnings("unchecked") + 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"); @@ -167,17 +166,12 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen int size = 1 << this.shift; this.referenceType = referenceType; int roundedUpSegmentCapactity = (int) ((initialCapacity + size - 1L) / size); - this.segments = createSegmentsArray(size); + this.segments = (Segment[]) Array.newInstance(Segment.class, size); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(roundedUpSegmentCapactity); } } - @SuppressWarnings("unchecked") - private Segment[] createSegmentsArray(int size) { - return (Segment[]) Array.newInstance(Segment.class, size); - } - protected final float getLoadFactor() { return this.loadFactor; @@ -222,7 +216,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen public V get(Object key) { Reference reference = getReference(key, Restructure.WHEN_NECESSARY); Entry entry = (reference == null ? null : reference.get()); - return (entry == null ? null : entry.getValue()); + return (entry != null ? entry.getValue() : null); } @Override @@ -392,7 +386,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen /** * Use {@link WeakReference}s. */ - WEAK; + WEAK } @@ -425,7 +419,6 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen */ private int resizeThreshold; - public Segment(int initialCapacity) { this.referenceManager = createReferenceManager(); this.initialSize = 1 << calculateShift(initialCapacity, MAXIMUM_SEGMENT_SIZE); @@ -456,17 +449,13 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen * @return the result of the operation */ public T doTask(final int hash, final Object key, final Task task) { - boolean resize = task.hasOption(TaskOption.RESIZE); - if (task.hasOption(TaskOption.RESTRUCTURE_BEFORE)) { restructureIfNecessary(resize); } - if (task.hasOption(TaskOption.SKIP_IF_EMPTY) && (this.count == 0)) { return task.execute(null, null, null); } - lock(); try { final int index = getIndex(hash, this.references); @@ -484,7 +473,8 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen } }; return task.execute(reference, entry, entries); - } finally { + } + finally { unlock(); if (task.hasOption(TaskOption.RESTRUCTURE_AFTER)) { restructureIfNecessary(resize); @@ -573,8 +563,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen } } - private Reference findInChain(Reference reference, Object key, - int hash) { + private Reference findInChain(Reference reference, Object key, int hash) { while (reference != null) { if (reference.getHash() == hash) { Entry entry = reference.get(); @@ -759,6 +748,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen * Various options supported by a {@link Task}. */ private static enum TaskOption { + RESTRUCTURE_BEFORE, RESTRUCTURE_AFTER, SKIP_IF_EMPTY, RESIZE } @@ -790,8 +780,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen public boolean contains(Object o) { if (o != null && o instanceof Map.Entry) { Map.Entry entry = (java.util.Map.Entry) o; - Reference reference = ConcurrentReferenceHashMap.this.getReference( - entry.getKey(), Restructure.NEVER); + Reference reference = ConcurrentReferenceHashMap.this.getReference(entry.getKey(), Restructure.NEVER); Entry other = (reference == null ? null : reference.get()); if (other != null) { return ObjectUtils.nullSafeEquals(entry.getValue(), other.getValue()); @@ -804,8 +793,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen public boolean remove(Object o) { if (o instanceof Map.Entry) { Map.Entry entry = (Map.Entry) o; - return ConcurrentReferenceHashMap.this.remove(entry.getKey(), - entry.getValue()); + return ConcurrentReferenceHashMap.this.remove(entry.getKey(), entry.getValue()); } return false; } @@ -926,8 +914,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen * @param next the next reference in the chain or {@code null} * @return a new {@link Reference} */ - public Reference createReference(Entry entry, int hash, - Reference next) { + public Reference createReference(Entry entry, int hash, Reference next) { if (ConcurrentReferenceHashMap.this.referenceType == ReferenceType.WEAK) { return new WeakEntryReference(entry, hash, next, this.queue); } @@ -951,15 +938,13 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen /** * Internal {@link Reference} implementation for {@link SoftReference}s. */ - private static final class SoftEntryReference extends - SoftReference> implements Reference { + private static final class SoftEntryReference extends SoftReference> implements Reference { private final int hash; private final Reference nextReference; - public SoftEntryReference(Entry entry, int hash, Reference next, - ReferenceQueue> queue) { + public SoftEntryReference(Entry entry, int hash, Reference next, ReferenceQueue> queue) { super(entry, queue); this.hash = hash; this.nextReference = next; @@ -986,15 +971,13 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen /** * Internal {@link Reference} implementation for {@link WeakReference}s. */ - private static final class WeakEntryReference extends - WeakReference> implements Reference { + private static final class WeakEntryReference extends WeakReference> implements Reference { private final int hash; private final Reference nextReference; - public WeakEntryReference(Entry entry, int hash, Reference next, - ReferenceQueue> queue) { + public WeakEntryReference(Entry entry, int hash, Reference next, ReferenceQueue> queue) { super(entry, queue); this.hash = hash; this.nextReference = next; @@ -1016,4 +999,5 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen clear(); } } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java index a9e29e57bb..d68b231ba5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java @@ -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. @@ -25,7 +25,6 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; - import javax.servlet.http.HttpServletRequest; import org.springframework.util.AntPathMatcher; @@ -54,6 +53,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition fileExtensions = new ArrayList(); + /** * Creates a new instance with the given URL patterns. * Each pattern that is not empty and does not start with "/" is prepended with "/". @@ -66,7 +66,6 @@ public final class PatternsRequestCondition extends AbstractRequestCondition asList(String... patterns) { - return patterns != null ? Arrays.asList(patterns) : Collections.emptyList(); + return (patterns != null ? Arrays.asList(patterns) : Collections.emptyList()); } private static Set prependLeadingSlash(Collection patterns) { @@ -187,7 +187,6 @@ public final class PatternsRequestCondition extends AbstractRequestConditionA matching pattern is obtained by making checks in the following order: *
    *
  • Direct match @@ -195,12 +194,10 @@ public final class PatternsRequestCondition extends AbstractRequestConditionPattern match *
  • Pattern match with "/" appended if the pattern doesn't already end in "/" *
- * * @param request the current request - * * @return the same instance if the condition contains no patterns; - * or a new condition with sorted matching patterns; - * or {@code null} if no patterns match. + * or a new condition with sorted matching patterns; + * or {@code null} if no patterns match. */ @Override public PatternsRequestCondition getMatchingCondition(HttpServletRequest request) { @@ -209,9 +206,8 @@ public final class PatternsRequestCondition extends AbstractRequestCondition matches = new ArrayList(); - for (String pattern : patterns) { + for (String pattern : this.patterns) { String match = getMatchingPattern(pattern, lookupPath); if (match != null) { matches.add(match); @@ -259,7 +255,6 @@ public final class PatternsRequestCondition extends AbstractRequestConditionIt is assumed that both instances have been obtained via * {@link #getMatchingCondition(HttpServletRequest)} to ensure they * contain only patterns that match the request and are sorted with @@ -270,7 +265,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition patternComparator = this.pathMatcher.getPatternComparator(lookupPath); - Iterator iterator = patterns.iterator(); + Iterator iterator = this.patterns.iterator(); Iterator iteratorOther = other.patterns.iterator(); while (iterator.hasNext() && iteratorOther.hasNext()) { int result = patternComparator.compare(iterator.next(), iteratorOther.next());