From 77d6633e622200ed52feef6fb2adeb12fba8e2c8 Mon Sep 17 00:00:00 2001 From: vate <806019582@qq.com> Date: Thu, 4 Jul 2024 18:36:55 +0800 Subject: [PATCH] [ISSUE #8298] Optimize some code format or style (#8299) Co-authored-by: supervate --- .../apache/rocketmq/acl/common/AclUtils.java | 8 +-- .../acl/plain/PlainPermissionManager.java | 67 +++++++++---------- .../rocketmq/broker/out/BrokerOuterAPI.java | 29 ++++---- .../org/apache/rocketmq/common/MixAll.java | 22 +++--- .../common/config/AbstractRocksDBStorage.java | 13 ++-- .../service/route/ProxyTopicRouteData.java | 18 +++-- 6 files changed, 70 insertions(+), 87 deletions(-) diff --git a/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java b/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java index 65f04f54339..937619beee4 100644 --- a/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java +++ b/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java @@ -40,7 +40,7 @@ public class AclUtils { public static byte[] combineRequestContent(RemotingCommand request, SortedMap fieldsMap) { try { - StringBuilder sb = new StringBuilder(""); + StringBuilder sb = new StringBuilder(); for (Map.Entry entry : fieldsMap.entrySet()) { if (!SessionCredentials.SIGNATURE.equals(entry.getKey())) { sb.append(entry.getValue()); @@ -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(":")) { @@ -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, ":"); diff --git a/acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionManager.java b/acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionManager.java index 345aed06c5a..b075e5364ee 100644 --- a/acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionManager.java +++ b/acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionManager.java @@ -94,7 +94,7 @@ public List getAllAclFiles(String path) { List 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)) { @@ -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; @@ -139,12 +138,11 @@ public void load() { List globalWhiteRemoteAddressStrategyList = new ArrayList<>(); List 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); } @@ -163,7 +161,7 @@ public void load() { } } } - if (plainAccessResourceMap.size() > 0) { + if (!plainAccessResourceMap.isEmpty()) { aclPlainAccessResourceMap.put(currentFile, plainAccessResourceMap); } @@ -219,17 +217,16 @@ public void load(String aclFilePath) { log.info("Broker plain acl conf data is : {}", plainAclConfData.toString()); List 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 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); } @@ -279,11 +276,9 @@ public PlainAccessData updateAclConfigFileVersion(String aclFileName, PlainAcces List 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 versionElement = new ArrayList<>(); @@ -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 entry : accountMap.entrySet()) { @@ -469,7 +464,7 @@ public boolean updateGlobalWhiteAddrsConfig(List globalWhiteAddrsList) { } public boolean updateGlobalWhiteAddrsConfig(List globalWhiteAddrsList, String fileName) { - if (fileName == null || fileName.equals("")) { + if (fileName == null || fileName.isEmpty()) { fileName = this.defaultAclFile; } @@ -511,10 +506,8 @@ public AclConfig getAllAclConfig() { List whiteAddrs = new ArrayList<>(); Set 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; } @@ -525,18 +518,18 @@ public AclConfig getAllAclConfig() { List 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); } } diff --git a/broker/src/main/java/org/apache/rocketmq/broker/out/BrokerOuterAPI.java b/broker/src/main/java/org/apache/rocketmq/broker/out/BrokerOuterAPI.java index d1cdb297fed..d5c80ce2ec3 100644 --- a/broker/src/main/java/org/apache/rocketmq/broker/out/BrokerOuterAPI.java +++ b/broker/src/main/java/org/apache/rocketmq/broker/out/BrokerOuterAPI.java @@ -336,7 +336,7 @@ public void sendHeartbeatViaDataVersion( final DataVersion dataVersion, final boolean isInBrokerContainer) { List 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); @@ -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: @@ -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()); @@ -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) { @@ -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(); } @@ -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(); } @@ -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(); @@ -1270,7 +1268,7 @@ public Pair> 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()); } @@ -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()); } @@ -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()); } @@ -1310,7 +1308,7 @@ public Pair> 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 syncStateSet = RemotingSerializable.decode(response.getBody(), SyncStateSet.class).getSyncStateSet(); return new Pair<>(responseHeader, syncStateSet); } @@ -1328,7 +1326,7 @@ public Pair 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); @@ -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()); diff --git a/common/src/main/java/org/apache/rocketmq/common/MixAll.java b/common/src/main/java/org/apache/rocketmq/common/MixAll.java index 47b4aac34a4..efb115509ac 100644 --- a/common/src/main/java/org/apache/rocketmq/common/MixAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/MixAll.java @@ -120,21 +120,21 @@ public class MixAll { private static final String OS = System.getProperty("os.name").toLowerCase(); public static boolean isWindows() { - return OS.indexOf("win") >= 0; + return OS.contains("win"); } public static boolean isMac() { - return OS.indexOf("mac") >= 0; + return OS.contains("mac"); } public static boolean isUnix() { - return OS.indexOf("nix") >= 0 - || OS.indexOf("nux") >= 0 - || OS.indexOf("aix") > 0; + return OS.contains("nix") + || OS.contains("nux") + || OS.contains("aix"); } public static boolean isSolaris() { - return OS.indexOf("sunos") >= 0; + return OS.contains("sunos"); } public static String getWSAddr() { @@ -205,7 +205,7 @@ public static void string2FileNotSafe(final String str, final String fileName) t if (fileParent != null) { fileParent.mkdirs(); } - IOTinyUtils.writeStringToFile(file, str, "UTF-8"); + IOTinyUtils.writeStringToFile(file, str, DEFAULT_CHARSET); } public static String file2String(final String fileName) throws IOException { @@ -224,7 +224,7 @@ public static String file2String(final File file) throws IOException { } if (result) { - return new String(data, "UTF-8"); + return new String(data, DEFAULT_CHARSET); } } return null; @@ -364,9 +364,9 @@ public static void properties2Object(final Properties p, final Object object) { String property = p.getProperty(key); if (property != null) { Class[] pt = method.getParameterTypes(); - if (pt != null && pt.length > 0) { + if (pt.length > 0) { String cn = pt[0].getSimpleName(); - Object arg = null; + Object arg; if (cn.equals("int") || cn.equals("Integer")) { arg = Integer.parseInt(property); } else if (cn.equals("long") || cn.equals("Long")) { @@ -464,7 +464,7 @@ public static String getLocalhostByNetworkInterface() throws SocketException { return candidatesHost.get(0); } - // Fallback to loopback + // Fallback to loopback return localhost(); } diff --git a/common/src/main/java/org/apache/rocketmq/common/config/AbstractRocksDBStorage.java b/common/src/main/java/org/apache/rocketmq/common/config/AbstractRocksDBStorage.java index 20319abba3d..ed3a12dc245 100644 --- a/common/src/main/java/org/apache/rocketmq/common/config/AbstractRocksDBStorage.java +++ b/common/src/main/java/org/apache/rocketmq/common/config/AbstractRocksDBStorage.java @@ -478,11 +478,7 @@ public void statRocksdb(Logger logger) { } Map map = Maps.newHashMap(); for (LiveFileMetaData metaData : liveFileMetaDataList) { - StringBuilder sb = map.get(metaData.level()); - if (sb == null) { - sb = new StringBuilder(256); - map.put(metaData.level(), sb); - } + StringBuilder sb = map.computeIfAbsent(metaData.level(), k -> new StringBuilder(256)); sb.append(new String(metaData.columnFamilyName(), DataConverter.CHARSET_UTF8)).append(SPACE). append(metaData.fileName()).append(SPACE). append("s: ").append(metaData.size()).append(SPACE). @@ -491,9 +487,8 @@ public void statRocksdb(Logger logger) { append("d: ").append(metaData.numDeletions()).append(SPACE). append(metaData.beingCompacted()).append("\n"); } - for (Map.Entry entry : map.entrySet()) { - logger.info("level: {}\n{}", entry.getKey(), entry.getValue().toString()); - } + + map.forEach((key, value) -> logger.info("level: {}\n{}", key, value.toString())); String blockCacheMemUsage = this.db.getProperty("rocksdb.block-cache-usage"); String indexesAndFilterBlockMemUsage = this.db.getProperty("rocksdb.estimate-table-readers-mem"); @@ -504,4 +499,4 @@ public void statRocksdb(Logger logger) { } catch (Exception ignored) { } } -} \ No newline at end of file +} diff --git a/proxy/src/main/java/org/apache/rocketmq/proxy/service/route/ProxyTopicRouteData.java b/proxy/src/main/java/org/apache/rocketmq/proxy/service/route/ProxyTopicRouteData.java index 63651f6fe81..b5e65818ac5 100644 --- a/proxy/src/main/java/org/apache/rocketmq/proxy/service/route/ProxyTopicRouteData.java +++ b/proxy/src/main/java/org/apache/rocketmq/proxy/service/route/ProxyTopicRouteData.java @@ -40,12 +40,11 @@ public ProxyTopicRouteData(TopicRouteData topicRouteData) { ProxyTopicRouteData.ProxyBrokerData proxyBrokerData = new ProxyTopicRouteData.ProxyBrokerData(); proxyBrokerData.setCluster(brokerData.getCluster()); proxyBrokerData.setBrokerName(brokerData.getBrokerName()); - for (Long brokerId : brokerData.getBrokerAddrs().keySet()) { - String brokerAddr = brokerData.getBrokerAddrs().get(brokerId); - HostAndPort hostAndPort = HostAndPort.fromString(brokerAddr); + brokerData.getBrokerAddrs().forEach((brokerId, brokerAddr) -> { + HostAndPort brokerHostAndPort = HostAndPort.fromString(brokerAddr); - proxyBrokerData.getBrokerAddrs().put(brokerId, Lists.newArrayList(new Address(Address.AddressScheme.IPv4, hostAndPort))); - } + proxyBrokerData.getBrokerAddrs().put(brokerId, Lists.newArrayList(new Address(Address.AddressScheme.IPv4, brokerHostAndPort))); + }); this.brokerDatas.add(proxyBrokerData); } } @@ -58,13 +57,12 @@ public ProxyTopicRouteData(TopicRouteData topicRouteData, int port) { ProxyTopicRouteData.ProxyBrokerData proxyBrokerData = new ProxyTopicRouteData.ProxyBrokerData(); proxyBrokerData.setCluster(brokerData.getCluster()); proxyBrokerData.setBrokerName(brokerData.getBrokerName()); - for (Long brokerId : brokerData.getBrokerAddrs().keySet()) { - String brokerAddr = brokerData.getBrokerAddrs().get(brokerId); + brokerData.getBrokerAddrs().forEach((brokerId, brokerAddr) -> { HostAndPort brokerHostAndPort = HostAndPort.fromString(brokerAddr); - HostAndPort hostAndPort = HostAndPort.fromParts(brokerHostAndPort.getHost(), port); + HostAndPort proxyHostAndPort = HostAndPort.fromParts(brokerHostAndPort.getHost(), port); - proxyBrokerData.getBrokerAddrs().put(brokerId, Lists.newArrayList(new Address(Address.AddressScheme.IPv4, hostAndPort))); - } + proxyBrokerData.getBrokerAddrs().put(brokerId, Lists.newArrayList(new Address(Address.AddressScheme.IPv4, proxyHostAndPort))); + }); this.brokerDatas.add(proxyBrokerData); } }