PropertySource 类说明
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.env;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
public abstract class PropertySource<T> {
protected final Log logger = LogFactory.getLog(getClass());
protected final String name;
protected final T source;
public PropertySource(String name, T source) {
Assert.hasText(name, "Property source name must contain at least one character");
Assert.notNull(source, "Property source must not be null");
this.name = name;
this.source = source;
}
@SuppressWarnings("unchecked")
public PropertySource(String name) {
this(name, (T) new Object());
}
public String getName() {
return this.name;
}
public T getSource() {
return this.source;
}
public boolean containsProperty(String name) {
return (getProperty(name) != null);
}
public abstract Object getProperty(String name);
@Override
public boolean equals(Object obj) {
return (this == obj || (obj instanceof PropertySource &&
ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) obj).name)));
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.name);
}
@Override
public String toString() {
if (logger.isDebugEnabled()) {
return String.format("%s@%s [name='%s', properties=%s]",
getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source);
}
else {
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name);
}
}
public static PropertySource<?> named(String name) {
return new ComparisonPropertySource(name);
}
public static class StubPropertySource extends PropertySource<Object> {
public StubPropertySource(String name) {
super(name, new Object());
}
@Override
public String getProperty(String name) {
return null;
}
}
static class ComparisonPropertySource extends StubPropertySource {
private static final String USAGE_ERROR =
"ComparisonPropertySource instances are for use with collection comparison only";
public ComparisonPropertySource(String name) {
super(name);
}
@Override
public Object getSource() {
throw new UnsupportedOperationException(USAGE_ERROR);
}
@Override
public boolean containsProperty(String name) {
throw new UnsupportedOperationException(USAGE_ERROR);
}
@Override
public String getProperty(String name) {
throw new UnsupportedOperationException(USAGE_ERROR);
}
@Override
public String toString() {
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name);
}
}
}
子类
说明 :所有子类都没有实现equals
hashCode
方法,即所有子类比较都是比较的name属性
@Override
public boolean equals(Object obj) {
return (this == obj || (obj instanceof PropertySource &&
ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) obj).name)));
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.name);
}
PropertySource.png
MutablePropertySources
- addBefore
- addAfter
- addFirst
- addLast
- remove
- replace
MutablePropertySourcesTests 测试用例
@Test
public void test() {
MutablePropertySources sources = new MutablePropertySources();
sources.addLast(new MockPropertySource("b").withProperty("p1", "bValue"));
sources.addLast(new MockPropertySource("d").withProperty("p1", "dValue"));
sources.addLast(new MockPropertySource("f").withProperty("p1", "fValue"));
assertThat(sources.size(), equalTo(3));
assertThat(sources.contains("a"), is(false));
assertThat(sources.contains("b"), is(true));
assertThat(sources.contains("c"), is(false));
assertThat(sources.contains("d"), is(true));
assertThat(sources.contains("e"), is(false));
assertThat(sources.contains("f"), is(true));
assertThat(sources.contains("g"), is(false));
assertThat(sources.get("b"), not(nullValue()));
assertThat(sources.get("b").getProperty("p1"), equalTo((Object)"bValue"));
assertThat(sources.get("d"), not(nullValue()));
assertThat(sources.get("d").getProperty("p1"), equalTo((Object)"dValue"));
sources.addBefore("b", new MockPropertySource("a"));
sources.addAfter("b", new MockPropertySource("c"));
assertThat(sources.size(), equalTo(5));
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
sources.addBefore("f", new MockPropertySource("e"));
sources.addAfter("f", new MockPropertySource("g"));
assertThat(sources.size(), equalTo(7));
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
sources.addLast(new MockPropertySource("a"));
assertThat(sources.size(), equalTo(7));
assertThat(sources.precedenceOf(PropertySource.named("b")), is(0));
assertThat(sources.precedenceOf(PropertySource.named("c")), is(1));
assertThat(sources.precedenceOf(PropertySource.named("d")), is(2));
assertThat(sources.precedenceOf(PropertySource.named("e")), is(3));
assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
assertThat(sources.precedenceOf(PropertySource.named("g")), is(5));
assertThat(sources.precedenceOf(PropertySource.named("a")), is(6));
sources.addFirst(new MockPropertySource("a"));
assertThat(sources.size(), equalTo(7));
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
assertEquals(sources.remove("a"), PropertySource.named("a"));
assertThat(sources.size(), equalTo(6));
assertThat(sources.contains("a"), is(false));
assertEquals(sources.remove("a"), null);
assertThat(sources.size(), equalTo(6));
String bogusPS = "bogus";
try {
sources.addAfter(bogusPS, new MockPropertySource("h"));
fail("expected non-existent PropertySource exception");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(),
equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
}
sources.addFirst(new MockPropertySource("a"));
assertThat(sources.size(), equalTo(7));
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
sources.replace("a", new MockPropertySource("a-replaced"));
assertThat(sources.size(), equalTo(7));
assertThat(sources.precedenceOf(PropertySource.named("a-replaced")), is(0));
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
sources.replace("a-replaced", new MockPropertySource("a"));
try {
sources.replace(bogusPS, new MockPropertySource("bogus-replaced"));
fail("expected non-existent PropertySource exception");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(),
equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
}
try {
sources.addBefore("b", new MockPropertySource("b"));
fail("expected exception");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(),
equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
}
try {
sources.addAfter("b", new MockPropertySource("b"));
fail("expected exception");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(),
equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
}
}
网友评论