For #10374, format code using Nacos Style (#11438)

This commit is contained in:
Dale Lee 2023-11-28 10:19:42 +08:00 committed by GitHub
parent 263e223d94
commit 7fdbc63390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 121 additions and 113 deletions

View File

@ -26,28 +26,28 @@ import java.util.List;
* @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.
*

View File

@ -27,4 +27,5 @@ import java.util.List;
* @author lideyou
*/
public interface NamingResult extends SelectResult<List<Instance>> {
}

View File

@ -24,4 +24,5 @@ import com.alibaba.nacos.api.selector.client.Selector;
* @author lideyou
*/
public interface NamingSelector extends Selector<NamingContext, NamingResult> {
}

View File

@ -23,6 +23,7 @@ package com.alibaba.nacos.api.selector.client;
* @author lideyou
*/
public interface SelectResult<T> {
/**
* Get select result.
*

View File

@ -24,6 +24,7 @@ package com.alibaba.nacos.api.selector.client;
* @author lideyou
*/
public interface Selector<C, E> {
/**
* select the target result.
*

View File

@ -29,80 +29,86 @@ import java.util.List;
* @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() {

View File

@ -25,15 +25,17 @@ import com.alibaba.nacos.api.naming.listener.Event;
* @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);

View File

@ -28,38 +28,40 @@ import java.util.List;
* @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();
}

View File

@ -32,23 +32,21 @@ import java.util.stream.Collectors;
* @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());
}
}

View File

@ -29,12 +29,13 @@ import java.util.Objects;
* @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) {
@ -43,21 +44,21 @@ public class NamingListenerInvoker implements ListenerInvoker<NamingEvent> {
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);

View File

@ -30,15 +30,16 @@ import java.util.Objects;
* @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.
*
@ -46,7 +47,7 @@ public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T ext
* @return true if the event can be callback
*/
protected abstract boolean isSelectable(T event);
/**
* Check whether the result can be callback.
*
@ -54,14 +55,15 @@ public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T ext
* @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.
*
@ -76,15 +78,15 @@ public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T ext
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) {
@ -96,7 +98,7 @@ public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T ext
AbstractSelectorWrapper<?, ?, ?> that = (AbstractSelectorWrapper<?, ?, ?>) o;
return Objects.equals(selector, that.selector) && Objects.equals(listener, that.listener);
}
@Override
public int hashCode() {
return Objects.hash(selector, listener);

View File

@ -23,6 +23,7 @@ package com.alibaba.nacos.client.selector;
* @author lideyou
*/
public interface ListenerInvoker<E> {
/**
* Invoke inner listener.
*

View File

@ -36,7 +36,8 @@ public class InstancesChangeEventTest {
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());

View File

@ -45,8 +45,7 @@ public class InstancesChangeNotifierTest {
InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(eventScope);
EventListener listener = Mockito.mock(EventListener.class);
NamingSelector selector = NamingSelectorFactory.newClusterSelector(clusters);
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector,
listener);
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector, listener);
instancesChangeNotifier.registerListener(group, name, wrapper);
List<ServiceInfo> subscribeServices = instancesChangeNotifier.getSubscribeServices();
Assert.assertEquals(1, subscribeServices.size());
@ -94,9 +93,8 @@ public class InstancesChangeNotifierTest {
EventListener listener = Mockito.mock(EventListener.class);
NamingSelector selector = NamingSelectorFactory.newClusterSelector(clusters);
Assert.assertFalse(instancesChangeNotifier.isSubscribed(group, name));
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector,
listener);
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector, listener);
instancesChangeNotifier.registerListener(group, name, wrapper);
Assert.assertTrue(instancesChangeNotifier.isSubscribed(group, name));
}
@ -111,9 +109,8 @@ public class InstancesChangeNotifierTest {
InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(eventScope);
NamingSelector selector = NamingSelectorFactory.newClusterSelector(clusters);
EventListener listener = Mockito.mock(EventListener.class);
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector,
listener);
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector, listener);
instancesChangeNotifier.registerListener(group, name, wrapper);
Instance instance = new Instance();
InstancesDiff diff = new InstancesDiff(null, Collections.singletonList(instance), null);

View File

@ -27,24 +27,19 @@ import java.util.List;
import java.util.Random;
public class InstancesDiffTest {
@Test
public void testGetDiff() {
String serviceName = "testService";
Instance addedIns = InstanceBuilder.newBuilder()
.setServiceName(serviceName)
.setClusterName("a").build();
Instance removedIns = InstanceBuilder.newBuilder()
.setServiceName(serviceName)
.setClusterName("b").build();
Instance modifiedIns = InstanceBuilder.newBuilder()
.setServiceName(serviceName)
.setClusterName("c").build();
Instance addedIns = InstanceBuilder.newBuilder().setServiceName(serviceName).setClusterName("a").build();
Instance removedIns = InstanceBuilder.newBuilder().setServiceName(serviceName).setClusterName("b").build();
Instance modifiedIns = InstanceBuilder.newBuilder().setServiceName(serviceName).setClusterName("c").build();
InstancesDiff instancesDiff = new InstancesDiff();
instancesDiff.setAddedInstances(Collections.singletonList(addedIns));
instancesDiff.setRemovedInstances(Collections.singletonList(removedIns));
instancesDiff.setModifiedInstances(Collections.singletonList(modifiedIns));
Assert.assertTrue(instancesDiff.hasDifferent());
Assert.assertTrue(instancesDiff.isAdded());
Assert.assertTrue(instancesDiff.isRemoved());
@ -53,19 +48,16 @@ public class InstancesDiffTest {
Assert.assertEquals(removedIns, instancesDiff.getRemovedInstances().get(0));
Assert.assertEquals(modifiedIns, instancesDiff.getModifiedInstances().get(0));
}
@Test
public void testWithFullConstructor() {
Random random = new Random();
int addedCount = random.nextInt(32) + 1;
int removedCount = random.nextInt(32) + 1;
int modifiedCount = random.nextInt(32) + 1;
InstancesDiff instancesDiff = new InstancesDiff(
getInstanceList(addedCount),
getInstanceList(removedCount),
getInstanceList(modifiedCount)
);
InstancesDiff instancesDiff = new InstancesDiff(getInstanceList(addedCount), getInstanceList(removedCount),
getInstanceList(modifiedCount));
Assert.assertTrue(instancesDiff.hasDifferent());
Assert.assertTrue(instancesDiff.isAdded());
Assert.assertTrue(instancesDiff.isRemoved());
@ -82,7 +74,7 @@ public class InstancesDiffTest {
Assert.assertFalse(instancesDiff.isRemoved());
Assert.assertFalse(instancesDiff.isModified());
}
@Test
public void testWithNoConstructor() {
Random random = new Random();
@ -93,7 +85,7 @@ public class InstancesDiffTest {
instancesDiff.setAddedInstances(getInstanceList(addedCount));
instancesDiff.setRemovedInstances(getInstanceList(removedCount));
instancesDiff.setModifiedInstances(getInstanceList(modifiedCount));
Assert.assertTrue(instancesDiff.hasDifferent());
Assert.assertEquals(addedCount, instancesDiff.getAddedInstances().size());
Assert.assertEquals(removedCount, instancesDiff.getRemovedInstances().size());
@ -106,7 +98,7 @@ public class InstancesDiffTest {
Assert.assertFalse(instancesDiff.isRemoved());
Assert.assertFalse(instancesDiff.isModified());
}
private static List<Instance> getInstanceList(int count) {
ArrayList<Instance> list = new ArrayList<>(count);
for (int i = 0; i < count; i++) {

View File

@ -30,11 +30,11 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class NamingChangeEventTest {
private MockNamingEventListener eventListener;
private InstancesDiff instancesDiff;
@Before
public void setUp() throws Exception {
eventListener = new MockNamingEventListener();
@ -43,7 +43,7 @@ public class NamingChangeEventTest {
instancesDiff.setRemovedInstances(Arrays.asList(new Instance(), new Instance()));
instancesDiff.setModifiedInstances(Arrays.asList(new Instance()));
}
@Test
public void testNamingChangeEventWithSimpleConstructor() {
NamingChangeEvent event = new NamingChangeEvent("serviceName", Collections.EMPTY_LIST, instancesDiff);
@ -69,10 +69,11 @@ public class NamingChangeEventTest {
assertFalse(event.isModified());
assertEquals(0, event.getRemovedInstances().size());
}
@Test
public void testNamingChangeEventWithFullConstructor() {
NamingChangeEvent event = new NamingChangeEvent("serviceName", "group", "clusters", Collections.EMPTY_LIST, instancesDiff);
NamingChangeEvent event = new NamingChangeEvent("serviceName", "group", "clusters", Collections.EMPTY_LIST,
instancesDiff);
assertEquals("serviceName", event.getServiceName());
assertEquals("group", event.getGroupName());
assertEquals("clusters", event.getClusters());
@ -95,7 +96,7 @@ public class NamingChangeEventTest {
assertFalse(event.isModified());
assertEquals(0, event.getRemovedInstances().size());
}
@Test
public void testGetChanges() {
NamingChangeEvent event = new NamingChangeEvent("serviceName", Collections.EMPTY_LIST, instancesDiff);
@ -104,22 +105,22 @@ public class NamingChangeEventTest {
event.getAddedInstances().clear();
assertFalse(event.isAdded());
assertEquals(0, event.getAddedInstances().size());
assertTrue(event.isRemoved());
assertEquals(2, event.getRemovedInstances().size());
event.getRemovedInstances().clear();
assertFalse(event.isRemoved());
assertEquals(0, event.getRemovedInstances().size());
assertTrue(event.isModified());
assertEquals(1, event.getModifiedInstances().size());
event.getModifiedInstances().clear();
assertFalse(event.isModified());
assertEquals(0, event.getRemovedInstances().size());
}
private static class MockNamingEventListener extends AbstractNamingChangeListener {
@Override
public void onChange(NamingChangeEvent event) {
assertNull(getExecutor());

View File

@ -31,27 +31,27 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class DefaultNamingSelectorTest {
@Test
public void testSelect() {
DefaultNamingSelector namingSelector = new DefaultNamingSelector(Instance::isHealthy);
Random random = new Random();
int total = random.nextInt(32) + 1;
int health = random.nextInt(total);
NamingContext namingContext = getMockNamingContext(total, health);
NamingResult result = namingSelector.select(namingContext);
assertEquals(health, result.getResult().size());
result.getResult().forEach(ins -> assertTrue(ins.isHealthy()));
}
private NamingContext getMockNamingContext(int total, int health) {
NamingContext namingContext = mock(NamingContext.class);
when(namingContext.getInstances()).thenReturn(getInstance(total, health));
return namingContext;
}
private List<Instance> getInstance(int total, int health) {
List<Instance> list = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
@ -59,11 +59,11 @@ public class DefaultNamingSelectorTest {
instance.setHealthy(false);
list.add(instance);
}
for (int i = 0; i < health; i++) {
list.get(i).setHealthy(true);
}
return list;
}
}

View File

@ -33,6 +33,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
public class NamingListenerInvokerTest {
@Test
public void testEventListener() {
EventListener listener = mock(EventListener.class);
@ -41,7 +42,7 @@ public class NamingListenerInvokerTest {
listenerInvoker.invoke(event);
verify(listener).onEvent(event);
}
@Test
public void testAbstractEventListener() {
AbstractEventListener listener = mock(AbstractEventListener.class);
@ -50,7 +51,7 @@ public class NamingListenerInvokerTest {
listenerInvoker.invoke(event);
verify(listener).getExecutor();
}
@Test
public void testAbstractNamingChaneEventListener() {
AbstractNamingChangeListener listener = spy(AbstractNamingChangeListener.class);
@ -59,7 +60,7 @@ public class NamingListenerInvokerTest {
listenerInvoker.invoke(event);
verify(listener).onChange(event);
}
@Test
public void testEquals() {
EventListener listener1 = mock(EventListener.class);

View File

@ -30,7 +30,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
public class SelectorManagerTest {
@Test
public void testCurd() {
SelectorManager<NamingSelectorWrapper> selectorManager = new SelectorManager<>();
@ -41,7 +41,7 @@ public class SelectorManagerTest {
selectorManager.removeSelectorWrapper(subId, sw);
assertNull(selectorManager.getSelectorWrappers(subId));
}
@Test
public void testSubInfo() {
SelectorManager<NamingSelectorWrapper> selectorManager = new SelectorManager<>();
@ -49,36 +49,36 @@ public class SelectorManagerTest {
for (int i = 0; i < 64; i++) {
list.add(generateRandomString(2, 32));
}
for (String subId : list) {
selectorManager.addSelectorWrapper(subId, mock(NamingSelectorWrapper.class));
assertTrue(selectorManager.isSubscribed(subId));
}
Set<String> subsSet = selectorManager.getSubscriptions();
for (String subId : subsSet) {
assertTrue(list.contains(subId));
}
for (String subId : list) {
selectorManager.removeSubscription(subId);
assertFalse(selectorManager.isSubscribed(subId));
}
}
private static String generateRandomString(int minLength, int maxLength) {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
int length = random.nextInt(maxLength - minLength + 1) + minLength;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
char randomChar = characters.charAt(index);
sb.append(randomChar);
}
return sb.toString();
}
}