[WIP]Load balancing abstract#3054
[WIP]Load balancing abstract#3054CalvinKirs wants to merge 5 commits intoapache:devfrom CalvinKirs:kris
Conversation
|
Kudos, SonarCloud Quality Gate passed!
|
| public abstract class AbstractSelector<T> implements Selector<T>{ | ||
| @Override | ||
| public T select(Collection<T> source) { | ||
|
|
||
| if (CollectionUtils.isEmpty(source)) { | ||
| throw new IllegalArgumentException("Empty source."); | ||
| } | ||
|
|
||
| /** | ||
| * if only one , return directly | ||
| */ | ||
| if (source.size() == 1) { | ||
| return (T)source.toArray()[0]; | ||
| } | ||
| return doSelect(source); | ||
| } | ||
|
|
||
| protected abstract T doSelect(Collection<T> source); | ||
|
|
||
| } |
There was a problem hiding this comment.
What is the purpose of this new AbstractSelector<T> class?
In my opinion, interface default method will be better than abstract method if there is no clear inheritance relation. So is it better o implement select method in Selector<T> as a default method?
是否有必要新建一个 AbstractSelector<T> 类呢?
个人理解,如果后续没有明显的继承关系的话,实现接口的default方法会比抽象类的方法有更多的好处。将 select 方法直接在 Selector<T> 接口中实现会不会更好一些?
There was a problem hiding this comment.
Thank you very much for your review, but I personally are not very inclined to implement the default method of the interface. The original intention of the default method is to add new functions to the smooth evolution of the existing interface. It is not very appropriate to put it here. what do you think?
| /** | ||
| * if only one , return directly | ||
| */ | ||
| if (source.size() == 1) { | ||
| return (T)source.toArray()[0]; | ||
| } | ||
| return doSelect(source); | ||
| } | ||
|
|
||
| protected abstract T doSelect(Collection<T> source); |
There was a problem hiding this comment.
| /** | |
| * if only one , return directly | |
| */ | |
| if (source.size() == 1) { | |
| return (T)source.toArray()[0]; | |
| } | |
| return doSelect(source); | |
| } | |
| protected abstract T doSelect(Collection<T> source); | |
| /** | |
| * if only one , return directly | |
| */ | |
| if (1 == source.size()) { | |
| return (T)source.toArray()[0]; | |
| } | |
| return doSelect(source); | |
| } | |
| protected abstract T doSelect(Collection<T> source); |
There was a problem hiding this comment.
Hello, why do you need to modify this?
There was a problem hiding this comment.
Why did it change like this?
Thx.
In my opinion, it's not necessary to change it to this way, but the advantages of doing so are prevent us from coding like this if (source.size() = 1). Just to improve the robustness of the code.
There was a problem hiding this comment.
Thank you for your suggestion, I will finish it later.
What is the purpose of the pull request
abstract loadbalance select
Brief change log
This pull request is code cleanup without any test coverage.