Skip to content

Commit

Permalink
For alibaba#10374, format code using Nacos Style
Browse files Browse the repository at this point in the history
  • Loading branch information
ldyedu committed Nov 27, 2023
1 parent 263e223 commit cd751a5
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@
* @author lideyou
*/
public interface NamingContext {

/**
* Get service name.
*
* @return service name
*/
String getServiceName();

/**
* Get group name.
*
* @return group name
*/
String getGroupName();

/**
* Get clusters.
*
* @return clusters
*/
String getClusters();

/**
* Get current instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
* @author lideyou
*/
public interface NamingResult extends SelectResult<List<Instance>> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
* @author lideyou
*/
public interface NamingSelector extends Selector<NamingContext, NamingResult> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @author lideyou
*/
public interface SelectResult<T> {

/**
* Get select result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @author lideyou
*/
public interface Selector<C, E> {

/**
* select the target result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,80 +29,86 @@
* @author lideyou
*/
public class InstancesDiff {

private final List<Instance> addedInstances = new ArrayList<>();

private final List<Instance> removedInstances = new ArrayList<>();

private final List<Instance> modifiedInstances = new ArrayList<>();

public InstancesDiff() {
}

public InstancesDiff(List<Instance> addedInstances, List<Instance> removedInstances, List<Instance> modifiedInstances) {

public InstancesDiff(List<Instance> addedInstances, List<Instance> removedInstances,
List<Instance> modifiedInstances) {
setAddedInstances(addedInstances);
setRemovedInstances(removedInstances);
setModifiedInstances(modifiedInstances);
}

public List<Instance> getAddedInstances() {
return addedInstances;
}

public void setAddedInstances(Collection<Instance> addedInstances) {
this.addedInstances.clear();
if (CollectionUtils.isNotEmpty(addedInstances)) {
this.addedInstances.addAll(addedInstances);
}
}

public List<Instance> getRemovedInstances() {
return removedInstances;
}

public void setRemovedInstances(Collection<Instance> removedInstances) {
this.removedInstances.clear();
if (CollectionUtils.isNotEmpty(removedInstances)) {
this.removedInstances.addAll(removedInstances);
}
}

public List<Instance> getModifiedInstances() {
return modifiedInstances;
}

public void setModifiedInstances(Collection<Instance> modifiedInstances) {
this.modifiedInstances.clear();
if (CollectionUtils.isNotEmpty(modifiedInstances)) {
this.modifiedInstances.addAll(modifiedInstances);
}
}

/**
* Check if any instances have changed.
*
* @return true if there are instances that have changed
*/
public boolean hasDifferent() {
return isAdded() || isRemoved() || isModified();
}

/**
* Check if any instances have been added.
*
* @return true if there are instances that have been added.
*/
public boolean isAdded() {
return CollectionUtils.isNotEmpty(this.addedInstances);
}

/**
* Check if any instances have been added.
*
* @return true if there are instances that have been added.
*/
public boolean isRemoved() {
return CollectionUtils.isNotEmpty(this.removedInstances);
}

/**
* Check if any instances have been added.
*
* @return true if there are instances that have been added.
*/
public boolean isModified() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
* @author lideyou
*/
public abstract class AbstractNamingChangeListener extends AbstractEventListener {

@Override
public final void onEvent(Event event) {
if (event instanceof NamingChangeEvent) {
onChange((NamingChangeEvent) event);
}
}

/**
* Callback when instances have changed.
*
* @param event NamingChangeEvent
*/
public abstract void onChange(NamingChangeEvent event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,40 @@
* @author lideyou
*/
public class NamingChangeEvent extends NamingEvent {

private final InstancesDiff instancesDiff;

public NamingChangeEvent(String serviceName, List<Instance> instances, InstancesDiff instancesDiff) {
super(serviceName, instances);
this.instancesDiff = instancesDiff;
}

public NamingChangeEvent(String serviceName, String groupName, String clusters, List<Instance> instances, InstancesDiff instancesDiff) {

public NamingChangeEvent(String serviceName, String groupName, String clusters, List<Instance> instances,
InstancesDiff instancesDiff) {
super(serviceName, groupName, clusters, instances);
this.instancesDiff = instancesDiff;
}

public boolean isAdded() {
return this.instancesDiff.isAdded();
}

public boolean isRemoved() {
return this.instancesDiff.isRemoved();
}

public boolean isModified() {
return this.instancesDiff.isModified();
}

public List<Instance> getAddedInstances() {
return this.instancesDiff.getAddedInstances();
}

public List<Instance> getRemovedInstances() {
return this.instancesDiff.getRemovedInstances();
}

public List<Instance> getModifiedInstances() {
return this.instancesDiff.getModifiedInstances();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@
* @author lideyou
*/
public class DefaultNamingSelector implements NamingSelector {

private final Predicate<Instance> filter;

public DefaultNamingSelector(Predicate<Instance> filter) {
this.filter = filter;
}

@Override
public NamingResult select(NamingContext context) {
List<Instance> instances = doFilter(context.getInstances());
return () -> instances;
}

private List<Instance> doFilter(List<Instance> instances) {
return instances == null ? Collections.emptyList() :
instances
.stream()
.filter(filter)
.collect(Collectors.toList());
return instances == null ? Collections.emptyList()
: instances.stream().filter(filter).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
* @author lideyou
*/
public class NamingListenerInvoker implements ListenerInvoker<NamingEvent> {

private final EventListener listener;

public NamingListenerInvoker(EventListener listener) {
this.listener = listener;
}

@Override
public void invoke(NamingEvent event) {
if (listener instanceof AbstractEventListener && ((AbstractEventListener) listener).getExecutor() != null) {
Expand All @@ -43,21 +44,21 @@ public void invoke(NamingEvent event) {
listener.onEvent(event);
}
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}

if (this == o) {
return true;
}

NamingListenerInvoker that = (NamingListenerInvoker) o;
return Objects.equals(listener, that.listener);
}

@Override
public int hashCode() {
return Objects.hashCode(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,40 @@
* @author lideyou
*/
public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T extends Event> {

private final S selector;

private final ListenerInvoker<E> listener;

public AbstractSelectorWrapper(S selector, ListenerInvoker<E> listener) {
this.selector = selector;
this.listener = listener;
}

/**
* Check whether the event can be callback.
*
* @param event original event
* @return true if the event can be callback
*/
protected abstract boolean isSelectable(T event);

/**
* Check whether the result can be callback.
*
* @param event select result
* @return true if the result can be callback
*/
protected abstract boolean isCallable(E event);

/**
* Build an event received by the listener.
*
* @param event original event
* @return listener event
*/
protected abstract E buildListenerEvent(T event);

/**
* Notify listener.
*
Expand All @@ -76,15 +78,15 @@ public void notifyListener(T event) {
listener.invoke(newEvent);
}
}

public ListenerInvoker<E> getListener() {
return this.listener;
}

public S getSelector() {
return this.selector;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -96,7 +98,7 @@ public boolean equals(Object o) {
AbstractSelectorWrapper<?, ?, ?> that = (AbstractSelectorWrapper<?, ?, ?>) o;
return Objects.equals(selector, that.selector) && Objects.equals(listener, that.listener);
}

@Override
public int hashCode() {
return Objects.hash(selector, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @author lideyou
*/
public interface ListenerInvoker<E> {

/**
* Invoke inner listener.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void testGetServiceName() {
hosts.add(ins);
InstancesDiff diff = new InstancesDiff();
diff.setAddedInstances(hosts);
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, serviceName, groupName, clusters, hosts, diff);
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, serviceName, groupName, clusters, hosts,
diff);
Assert.assertEquals(eventScope, event.scope());
Assert.assertEquals(serviceName, event.getServiceName());
Assert.assertEquals(clusters, event.getClusters());
Expand Down
Loading

0 comments on commit cd751a5

Please sign in to comment.