Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[type:refactor] Optimize code for shenyu-registry #5876

Merged
merged 14 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.apache.shenyu.registry.api.config;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
Expand Down Expand Up @@ -127,7 +130,7 @@ public void setEnabled(final boolean enabled) {

@Override
public boolean equals(final Object obj) {
if (obj == null) {
if (Objects.isNull(obj)) {
return false;
}
RegisterConfig registerConfig = (RegisterConfig) obj;
Expand All @@ -137,18 +140,20 @@ public boolean equals(final Object obj) {
if (!this.getServerLists().equals(registerConfig.getServerLists())) {
return false;
}
if (this.getProps() == null && registerConfig.getProps() == null) {
Properties properties = this.getProps();
Properties registerConfigProps = registerConfig.getProps();
if (Objects.isNull(properties) && Objects.isNull(registerConfigProps)) {
return true;
}
if (this.getProps() == null || registerConfig.getProps() == null) {
if (Objects.isNull(properties) || Objects.isNull(registerConfigProps)) {
return false;
}
if (this.getProps().entrySet().size() != registerConfig.getProps().entrySet().size()) {
if (properties.entrySet().size() != registerConfigProps.entrySet().size()) {
return false;
}
for (Map.Entry<Object, Object> entry : this.getProps().entrySet()) {
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object newValue = entry.getValue();
Object oldValue = registerConfig.getProps().get(entry.getKey());
Object oldValue = registerConfigProps.get(entry.getKey());
if (!newValue.equals(oldValue)) {
return false;
}
Expand All @@ -158,13 +163,18 @@ public boolean equals(final Object obj) {

@Override
public int hashCode() {
int result = getRegisterType() != null ? getRegisterType().hashCode() : 0;
result = 31 * result + (getServerLists() != null ? getServerLists().hashCode() : 0);
String registerTypeStr = getRegisterType();
int result = StringUtils.isNotEmpty(registerTypeStr) ? registerTypeStr.hashCode() : 0;
String serverListsStr = getServerLists();
result = 31 * result + (StringUtils.isNotEmpty(serverListsStr) ? serverListsStr.hashCode() : 0);

if (getProps() != null) {
for (Map.Entry<Object, Object> entry : getProps().entrySet()) {
result = 31 * result + (entry.getKey() != null ? entry.getKey().hashCode() : 0);
result = 31 * result + (entry.getValue() != null ? entry.getValue().hashCode() : 0);
Properties properties = getProps();
if (Objects.nonNull(properties)) {
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object entryKey = entry.getKey();
result = 31 * result + (Objects.nonNull(entryKey) ? entryKey.hashCode() : 0);
Object entryValue = entry.getValue();
result = 31 * result + (Objects.nonNull(entryValue) ? entryValue.hashCode() : 0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public boolean equals(final Object o) {
return Boolean.TRUE;
}

if (o == null || getClass() != o.getClass()) {
if (Objects.isNull(o) || getClass() != o.getClass()) {
return Boolean.FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -46,10 +47,10 @@ public KubernetesClient(final KubernetesConfig kubernetesConfig) {
*/
public List<KubernetesInstance> selectInstances(final String serviceId) {
List<KubernetesInstance> response = Collections.emptyList();
KubernetesInstance[] responseBody = (KubernetesInstance[]) this.rest.getForEntity(this.kubernetesConfig.getDiscoveryServerUrl() + "/apps/" + serviceId,
KubernetesInstance[] responseBody = this.rest.getForEntity(this.kubernetesConfig.getDiscoveryServerUrl() + "/apps/" + serviceId,
KubernetesInstance[].class, new Object[0]).getBody();
if (responseBody != null && responseBody.length > 0) {
response = (List) Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
if (Objects.nonNull(responseBody) && responseBody.length > 0) {
response = Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
}

return response;
Expand Down
Loading