Skip to content

Commit

Permalink
[ISSUE apache#8298] Optimize some code format or style (apache#8299)
Browse files Browse the repository at this point in the history
Co-authored-by: supervate <vate@MacBook-Air-4.local>
  • Loading branch information
supervate and supervate authored Jul 4, 2024
1 parent 9173def commit 77d6633
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AclUtils {

public static byte[] combineRequestContent(RemotingCommand request, SortedMap<String, String> fieldsMap) {
try {
StringBuilder sb = new StringBuilder("");
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : fieldsMap.entrySet()) {
if (!SessionCredentials.SIGNATURE.equals(entry.getKey())) {
sb.append(entry.getValue());
Expand Down Expand Up @@ -71,12 +71,12 @@ public static void IPv6AddressCheck(String netAddress) {
if (isAsterisk(netAddress) || isMinus(netAddress)) {
int asterisk = netAddress.indexOf("*");
int minus = netAddress.indexOf("-");
// '*' must be the end of netAddress if it exists
// '*' must be the end of netAddress if it exists
if (asterisk > -1 && asterisk != netAddress.length() - 1) {
throw new AclException(String.format("NetAddress examine scope Exception netAddress is %s", netAddress));
}

// format like "2::ac5:78:1-200:*" or "2::ac5:78:1-200" is legal
// format like "2::ac5:78:1-200:*" or "2::ac5:78:1-200" is legal
if (minus > -1) {
if (asterisk == -1) {
if (minus <= netAddress.lastIndexOf(":")) {
Expand Down Expand Up @@ -128,7 +128,7 @@ public static String[] getAddresses(String netAddress, String partialAddress) {
}

public static boolean isScope(String netAddress, int index) {
// IPv6 Address
// IPv6 Address
if (isColon(netAddress)) {
netAddress = expandIP(netAddress, 8);
String[] strArray = StringUtils.split(netAddress, ":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<String> getAllAclFiles(String path) {
List<String> allAclFileFullPath = new ArrayList<>();
File file = new File(path);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
for (int i = 0; files != null && i < files.length; i++) {
String fileName = files[i].getAbsolutePath();
File f = new File(fileName);
if (fileName.equals(fileHome + MixAll.ACL_CONF_TOOLS_FILE)) {
Expand Down Expand Up @@ -126,10 +126,9 @@ public void load() {
fileList.add(defaultAclFile);
}

for (int i = 0; i < fileList.size(); i++) {
final String currentFile = MixAll.dealFilePath(fileList.get(i));
PlainAccessData plainAclConfData = AclUtils.getYamlDataObject(currentFile,
PlainAccessData.class);
for (String path : fileList) {
final String currentFile = MixAll.dealFilePath(path);
PlainAccessData plainAclConfData = AclUtils.getYamlDataObject(currentFile, PlainAccessData.class);
if (plainAclConfData == null) {
log.warn("No data in file {}", currentFile);
continue;
Expand All @@ -139,12 +138,11 @@ public void load() {
List<RemoteAddressStrategy> globalWhiteRemoteAddressStrategyList = new ArrayList<>();
List<String> globalWhiteRemoteAddressesList = plainAclConfData.getGlobalWhiteRemoteAddresses();
if (globalWhiteRemoteAddressesList != null && !globalWhiteRemoteAddressesList.isEmpty()) {
for (int j = 0; j < globalWhiteRemoteAddressesList.size(); j++) {
globalWhiteRemoteAddressStrategyList.add(remoteAddressStrategyFactory.
getRemoteAddressStrategy(globalWhiteRemoteAddressesList.get(j)));
for (String address : globalWhiteRemoteAddressesList) {
globalWhiteRemoteAddressStrategyList.add(remoteAddressStrategyFactory.getRemoteAddressStrategy(address));
}
}
if (globalWhiteRemoteAddressStrategyList.size() > 0) {
if (!globalWhiteRemoteAddressStrategyList.isEmpty()) {
globalWhiteRemoteAddressStrategyMap.put(currentFile, globalWhiteRemoteAddressStrategyList);
globalWhiteRemoteAddressStrategy.addAll(globalWhiteRemoteAddressStrategyList);
}
Expand All @@ -163,7 +161,7 @@ public void load() {
}
}
}
if (plainAccessResourceMap.size() > 0) {
if (!plainAccessResourceMap.isEmpty()) {
aclPlainAccessResourceMap.put(currentFile, plainAccessResourceMap);
}

Expand Down Expand Up @@ -219,17 +217,16 @@ public void load(String aclFilePath) {
log.info("Broker plain acl conf data is : {}", plainAclConfData.toString());
List<String> globalWhiteRemoteAddressesList = plainAclConfData.getGlobalWhiteRemoteAddresses();
if (globalWhiteRemoteAddressesList != null && !globalWhiteRemoteAddressesList.isEmpty()) {
for (int i = 0; i < globalWhiteRemoteAddressesList.size(); i++) {
globalWhiteRemoteAddressStrategy.add(remoteAddressStrategyFactory.
getRemoteAddressStrategy(globalWhiteRemoteAddressesList.get(i)));
for (String address : globalWhiteRemoteAddressesList) {
globalWhiteRemoteAddressStrategy.add(remoteAddressStrategyFactory.getRemoteAddressStrategy(address));
}
}

this.globalWhiteRemoteAddressStrategy.addAll(globalWhiteRemoteAddressStrategy);
if (this.globalWhiteRemoteAddressStrategyMap.get(aclFilePath) != null) {
List<RemoteAddressStrategy> remoteAddressStrategyList = this.globalWhiteRemoteAddressStrategyMap.get(aclFilePath);
for (int i = 0; i < remoteAddressStrategyList.size(); i++) {
this.globalWhiteRemoteAddressStrategy.remove(remoteAddressStrategyList.get(i));
for (RemoteAddressStrategy remoteAddressStrategy : remoteAddressStrategyList) {
this.globalWhiteRemoteAddressStrategy.remove(remoteAddressStrategy);
}
this.globalWhiteRemoteAddressStrategyMap.put(aclFilePath, globalWhiteRemoteAddressStrategy);
}
Expand Down Expand Up @@ -279,11 +276,9 @@ public PlainAccessData updateAclConfigFileVersion(String aclFileName, PlainAcces

List<PlainAccessData.DataVersion> dataVersions = updateAclConfigMap.getDataVersion();
DataVersion dataVersion = new DataVersion();
if (dataVersions != null) {
if (dataVersions.size() > 0) {
dataVersion.setTimestamp(dataVersions.get(0).getTimestamp());
dataVersion.setCounter(new AtomicLong(dataVersions.get(0).getCounter()));
}
if (dataVersions != null && !dataVersions.isEmpty()) {
dataVersion.setTimestamp(dataVersions.get(0).getTimestamp());
dataVersion.setCounter(new AtomicLong(dataVersions.get(0).getCounter()));
}
dataVersion.nextVersion();
List<PlainAccessData.DataVersion> versionElement = new ArrayList<>();
Expand Down Expand Up @@ -336,7 +331,7 @@ public boolean updateAccessConfig(PlainAccessConfig plainAccessConfig) {
if (accountMap == null) {
accountMap = new HashMap<>(1);
accountMap.put(plainAccessConfig.getAccessKey(), buildPlainAccessResource(plainAccessConfig));
} else if (accountMap.size() == 0) {
} else if (accountMap.isEmpty()) {
accountMap.put(plainAccessConfig.getAccessKey(), buildPlainAccessResource(plainAccessConfig));
} else {
for (Map.Entry<String, PlainAccessResource> entry : accountMap.entrySet()) {
Expand Down Expand Up @@ -469,7 +464,7 @@ public boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList) {
}

public boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList, String fileName) {
if (fileName == null || fileName.equals("")) {
if (fileName == null || fileName.isEmpty()) {
fileName = this.defaultAclFile;
}

Expand Down Expand Up @@ -511,10 +506,8 @@ public AclConfig getAllAclConfig() {
List<String> whiteAddrs = new ArrayList<>();
Set<String> accessKeySets = new HashSet<>();

for (int i = 0; i < fileList.size(); i++) {
String path = fileList.get(i);
PlainAccessData plainAclConfData = AclUtils.getYamlDataObject(path,
PlainAccessData.class);
for (String path : fileList) {
PlainAccessData plainAclConfData = AclUtils.getYamlDataObject(path, PlainAccessData.class);
if (plainAclConfData == null) {
continue;
}
Expand All @@ -525,18 +518,18 @@ public AclConfig getAllAclConfig() {

List<PlainAccessConfig> plainAccessConfigs = plainAclConfData.getAccounts();
if (plainAccessConfigs != null && !plainAccessConfigs.isEmpty()) {
for (int j = 0; j < plainAccessConfigs.size(); j++) {
if (!accessKeySets.contains(plainAccessConfigs.get(j).getAccessKey())) {
accessKeySets.add(plainAccessConfigs.get(j).getAccessKey());
for (PlainAccessConfig accessConfig : plainAccessConfigs) {
if (!accessKeySets.contains(accessConfig.getAccessKey())) {
accessKeySets.add(accessConfig.getAccessKey());
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
plainAccessConfig.setGroupPerms(plainAccessConfigs.get(j).getGroupPerms());
plainAccessConfig.setDefaultTopicPerm(plainAccessConfigs.get(j).getDefaultTopicPerm());
plainAccessConfig.setDefaultGroupPerm(plainAccessConfigs.get(j).getDefaultGroupPerm());
plainAccessConfig.setAccessKey(plainAccessConfigs.get(j).getAccessKey());
plainAccessConfig.setSecretKey(plainAccessConfigs.get(j).getSecretKey());
plainAccessConfig.setAdmin(plainAccessConfigs.get(j).isAdmin());
plainAccessConfig.setTopicPerms(plainAccessConfigs.get(j).getTopicPerms());
plainAccessConfig.setWhiteRemoteAddress(plainAccessConfigs.get(j).getWhiteRemoteAddress());
plainAccessConfig.setGroupPerms(accessConfig.getGroupPerms());
plainAccessConfig.setDefaultTopicPerm(accessConfig.getDefaultTopicPerm());
plainAccessConfig.setDefaultGroupPerm(accessConfig.getDefaultGroupPerm());
plainAccessConfig.setAccessKey(accessConfig.getAccessKey());
plainAccessConfig.setSecretKey(accessConfig.getSecretKey());
plainAccessConfig.setAdmin(accessConfig.isAdmin());
plainAccessConfig.setTopicPerms(accessConfig.getTopicPerms());
plainAccessConfig.setWhiteRemoteAddress(accessConfig.getWhiteRemoteAddress());
configs.add(plainAccessConfig);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void sendHeartbeatViaDataVersion(
final DataVersion dataVersion,
final boolean isInBrokerContainer) {
List<String> nameServerAddressList = this.remotingClient.getAvailableNameSrvList();
if (nameServerAddressList != null && nameServerAddressList.size() > 0) {
if (nameServerAddressList != null && !nameServerAddressList.isEmpty()) {
final QueryDataVersionRequestHeader requestHeader = new QueryDataVersionRequestHeader();
requestHeader.setBrokerAddr(brokerAddr);
requestHeader.setBrokerName(brokerName);
Expand Down Expand Up @@ -405,7 +405,7 @@ public BrokerSyncInfo retrieveBrokerHaInfo(String masterBrokerAddr)
assert response != null;
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
ExchangeHAInfoResponseHeader responseHeader = (ExchangeHAInfoResponseHeader) response.decodeCommandCustomHeader(ExchangeHAInfoResponseHeader.class);
ExchangeHAInfoResponseHeader responseHeader = response.decodeCommandCustomHeader(ExchangeHAInfoResponseHeader.class);
return new BrokerSyncInfo(responseHeader.getMasterHaAddress(), responseHeader.getMasterFlushOffset(), responseHeader.getMasterAddress());
}
default:
Expand Down Expand Up @@ -574,8 +574,7 @@ private RegisterBrokerResult registerBroker(
assert response != null;
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
RegisterBrokerResponseHeader responseHeader =
(RegisterBrokerResponseHeader) response.decodeCommandCustomHeader(RegisterBrokerResponseHeader.class);
RegisterBrokerResponseHeader responseHeader = response.decodeCommandCustomHeader(RegisterBrokerResponseHeader.class);
RegisterBrokerResult result = new RegisterBrokerResult();
result.setMasterAddr(responseHeader.getMasterAddr());
result.setHaServerAddr(responseHeader.getHaServerAddr());
Expand Down Expand Up @@ -725,7 +724,7 @@ public void run0() {
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
QueryDataVersionResponseHeader queryDataVersionResponseHeader =
(QueryDataVersionResponseHeader) response.decodeCommandCustomHeader(QueryDataVersionResponseHeader.class);
response.decodeCommandCustomHeader(QueryDataVersionResponseHeader.class);
changed = queryDataVersionResponseHeader.getChanged();
byte[] body = response.getBody();
if (body != null) {
Expand Down Expand Up @@ -887,7 +886,7 @@ public long getMaxOffset(final String addr, final String topic, final int queueI
assert response != null;
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
GetMaxOffsetResponseHeader responseHeader = (GetMaxOffsetResponseHeader) response.decodeCommandCustomHeader(GetMaxOffsetResponseHeader.class);
GetMaxOffsetResponseHeader responseHeader = response.decodeCommandCustomHeader(GetMaxOffsetResponseHeader.class);

return responseHeader.getOffset();
}
Expand All @@ -909,7 +908,7 @@ public long getMinOffset(final String addr, final String topic, final int queueI
assert response != null;
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
GetMinOffsetResponseHeader responseHeader = (GetMinOffsetResponseHeader) response.decodeCommandCustomHeader(GetMinOffsetResponseHeader.class);
GetMinOffsetResponseHeader responseHeader = response.decodeCommandCustomHeader(GetMinOffsetResponseHeader.class);

return responseHeader.getOffset();
}
Expand Down Expand Up @@ -1096,8 +1095,7 @@ private SendResult processSendResponse(
break;
}
if (sendStatus != null) {
SendMessageResponseHeader responseHeader =
(SendMessageResponseHeader) response.decodeCommandCustomHeader(SendMessageResponseHeader.class);
SendMessageResponseHeader responseHeader = response.decodeCommandCustomHeader(SendMessageResponseHeader.class);

//If namespace not null , reset Topic without namespace.
String topic = msg.getTopic();
Expand Down Expand Up @@ -1270,7 +1268,7 @@ public Pair<ElectMasterResponseHeader, Set<Long>> brokerElect(String controllerA
// Only record success response.
case CONTROLLER_MASTER_STILL_EXIST:
case SUCCESS:
final ElectMasterResponseHeader responseHeader = (ElectMasterResponseHeader) response.decodeCommandCustomHeader(ElectMasterResponseHeader.class);
final ElectMasterResponseHeader responseHeader = response.decodeCommandCustomHeader(ElectMasterResponseHeader.class);
final ElectMasterResponseBody responseBody = RemotingSerializable.decode(response.getBody(), ElectMasterResponseBody.class);
return new Pair<>(responseHeader, responseBody.getSyncStateSet());
}
Expand All @@ -1285,7 +1283,7 @@ public GetNextBrokerIdResponseHeader getNextBrokerId(final String clusterName, f
final RemotingCommand response = this.remotingClient.invokeSync(controllerAddress, request, 3000);
assert response != null;
if (response.getCode() == SUCCESS) {
return (GetNextBrokerIdResponseHeader) response.decodeCommandCustomHeader(GetNextBrokerIdResponseHeader.class);
return response.decodeCommandCustomHeader(GetNextBrokerIdResponseHeader.class);
}
throw new MQBrokerException(response.getCode(), response.getRemark());
}
Expand All @@ -1297,7 +1295,7 @@ public ApplyBrokerIdResponseHeader applyBrokerId(final String clusterName, final
final RemotingCommand response = this.remotingClient.invokeSync(controllerAddress, request, 3000);
assert response != null;
if (response.getCode() == SUCCESS) {
return (ApplyBrokerIdResponseHeader) response.decodeCommandCustomHeader(ApplyBrokerIdResponseHeader.class);
return response.decodeCommandCustomHeader(ApplyBrokerIdResponseHeader.class);
}
throw new MQBrokerException(response.getCode(), response.getRemark());
}
Expand All @@ -1310,7 +1308,7 @@ public Pair<RegisterBrokerToControllerResponseHeader, Set<Long>> registerBrokerT
final RemotingCommand response = this.remotingClient.invokeSync(controllerAddress, request, 3000);
assert response != null;
if (response.getCode() == SUCCESS) {
RegisterBrokerToControllerResponseHeader responseHeader = (RegisterBrokerToControllerResponseHeader) response.decodeCommandCustomHeader(RegisterBrokerToControllerResponseHeader.class);
RegisterBrokerToControllerResponseHeader responseHeader = response.decodeCommandCustomHeader(RegisterBrokerToControllerResponseHeader.class);
Set<Long> syncStateSet = RemotingSerializable.decode(response.getBody(), SyncStateSet.class).getSyncStateSet();
return new Pair<>(responseHeader, syncStateSet);
}
Expand All @@ -1328,7 +1326,7 @@ public Pair<GetReplicaInfoResponseHeader, SyncStateSet> getReplicaInfo(final Str
assert response != null;
switch (response.getCode()) {
case SUCCESS: {
final GetReplicaInfoResponseHeader header = (GetReplicaInfoResponseHeader) response.decodeCommandCustomHeader(GetReplicaInfoResponseHeader.class);
final GetReplicaInfoResponseHeader header = response.decodeCommandCustomHeader(GetReplicaInfoResponseHeader.class);
assert response.getBody() != null;
final SyncStateSet stateSet = RemotingSerializable.decode(response.getBody(), SyncStateSet.class);
return new Pair<>(header, stateSet);
Expand Down Expand Up @@ -1447,8 +1445,7 @@ private PullResultExt processPullResponse(
throw new MQBrokerException(response.getCode(), response.getRemark(), addr);
}

PullMessageResponseHeader responseHeader =
(PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);
PullMessageResponseHeader responseHeader = response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody(), responseHeader.getOffsetDelta());
Expand Down
Loading

0 comments on commit 77d6633

Please sign in to comment.