Skip to content

Commit

Permalink
Code review (#3094)
Browse files Browse the repository at this point in the history
* movde compareTo into Configurator as a default method

* adjust javado

* optimize diamond

* polish the code
  • Loading branch information
beiwei30 authored and zonghaishang committed Dec 30, 2018
1 parent 383a37e commit 46073c7
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,36 @@
public interface Configurator extends Comparable<Configurator> {

/**
* get the configurator url.
* Get the configurator url.
*
* @return configurator url.
*/
URL getUrl();

/**
* Configure the provider url.
* O
*
* @param url - old rovider url.
* @param url - old provider url.
* @return new provider url.
*/
URL configure(URL url);


/**
* Convert override urls to map for use when re-refer.
* Send all rules every time, the urls will be reassembled and calculated
* Convert override urls to map for use when re-refer. Send all rules every time, the urls will be reassembled and
* calculated
*
* @param urls Contract:
* </br>1.override://0.0.0.0/...( or override://ip:port...?anyhost=true)&para1=value1... means global rules (all of the providers take effect)
* </br>2.override://ip:port...?anyhost=false Special rules (only for a certain provider)
* </br>3.override:// rule is not supported... ,needs to be calculated by registry itself.
* </br>4.override://0.0.0.0/ without parameters means clearing the override
* @return
* URL contract:
* <ol>
* <li>override://0.0.0.0/...( or override://ip:port...?anyhost=true)&para1=value1... means global rules
* (all of the providers take effect)</li>
* <li>override://ip:port...?anyhost=false Special rules (only for a certain provider)</li>
* <li>override:// rule is not supported... ,needs to be calculated by registry itself</li>
* <li>override://0.0.0.0/ without parameters means clearing the override</li>
* </ol>
*
* @param urls URL list to convert
* @return converted configurator list
*/
static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
if (CollectionUtils.isEmpty(urls)) {
Expand All @@ -70,13 +74,13 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
ConfiguratorFactory configuratorFactory = ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
.getAdaptiveExtension();

List<Configurator> configurators = new ArrayList<Configurator>(urls.size());
List<Configurator> configurators = new ArrayList<>(urls.size());
for (URL url : urls) {
if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
configurators.clear();
break;
}
Map<String, String> override = new HashMap<String, String>(url.getParameters());
Map<String, String> override = new HashMap<>(url.getParameters());
//The anyhost parameter of override may be added automatically, it can't change the judgement of changing url
override.remove(Constants.ANYHOST_KEY);
if (override.size() == 0) {
Expand All @@ -88,4 +92,25 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
Collections.sort(configurators);
return Optional.of(configurators);
}

/**
* Sort by host, then by priority
* 1. the url with a specific host ip should have higher priority than 0.0.0.0
* 2. if two url has the same host, compare by priority value;
*/
default int compareTo(Configurator o) {
if (o == null) {
return -1;
}

int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
// host is the same, sort by priority
if (ipCompare == 0) {
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0);
int j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
return Integer.compare(i, j);
} else {
return ipCompare;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,6 @@ private URL configureIfMatch(String host, URL url) {
return url;
}

/**
* Sort by host, priority
* 1. the url with a specific host ip should have higher priority than 0.0.0.0
* 2. if two url has the same host, compare by priority value;
*
* @param o
* @return
*/
@Override
public int compareTo(Configurator o) {
if (o == null) {
return -1;
}

int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
if (ipCompare == 0) {//host is the same, sort by priority
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0),
j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
return Integer.compare(i, j);
} else {
return ipCompare;
}


}

protected abstract URL doConfigure(URL currentUrl, URL configUrl);

}
Loading

0 comments on commit 46073c7

Please sign in to comment.