Skip to content

Commit

Permalink
add merge config source #13
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Feb 18, 2013
1 parent e63d661 commit 3c7276c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
42 changes: 40 additions & 2 deletions src/main/java/com/metaframe/unicorn/Unicorn.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,51 @@

package com.metaframe.unicorn;

import com.metaframe.unicorn.support.AbstractConfigSource;

import java.util.List;

/**
* @author Jerry Lee(oldratlee AT gmail DOT com)
*/
public class Unicorn {
public static ConfigSource merge(List<ConfigSource> sources) {
return null;

/**
* 把多个{@link ConfigSource}合并成一个。
*
* @param sources 被合并的多个
* @return
*/
public static ConfigSource merge(final List<ConfigSource> sources) {
if (sources == null) {
throw new IllegalArgumentException("argument sources == null");
}
if (sources.isEmpty()) {
return AbstractConfigSource.EMPTY_SOURCE;
}
if (sources.size() == 1) {
return sources.get(0);
}

return new AbstractConfigSource() {
@Override
public boolean contains(String key) {
for (ConfigSource source : sources) {
if (source.contains(key)) return true;
}
return false;
}

@Override
public String get(String key) {
for (ConfigSource source : sources) {
if (source.contains(key)) return source.get(key);
}
return null;
}
};
}

private Unicorn() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
* @author Jerry Lee(oldratlee AT gmail DOT com)
*/
public abstract class AbstractConfigSource implements ConfigSource {
public static final ConfigSource EMPTY_SOURCE = new AbstractConfigSource() {
@Override
public boolean contains(String key) {
return false;
}

@Override
public String get(String key) {
return null;
}
};

/**
* @since 0.1.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.metaframe.unicorn.support;

import com.metaframe.unicorn.ConfigSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.metaframe.unicorn.support;

import com.metaframe.unicorn.ConfigSource;

/**
* JVM系统属性配置数据来源。
* <p/>
Expand Down

0 comments on commit 3c7276c

Please sign in to comment.