Skip to content

Commit

Permalink
[ISSUE #6145] Modify Collections.sort usage (#6146)
Browse files Browse the repository at this point in the history
* modify Collections.sort usage

* modify Collections.sort usage
  • Loading branch information
hardyfish authored Feb 28, 2023
1 parent 086f9d1 commit 33371e5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,16 @@ public String toString() {
return sb.toString();
}

public static final Comparator<PopRequest> COMPARATOR = new Comparator<PopRequest>() {
@Override
public int compare(PopRequest o1, PopRequest o2) {
int ret = (int) (o1.getExpired() - o2.getExpired());
public static final Comparator<PopRequest> COMPARATOR = (o1, o2) -> {
int ret = (int) (o1.getExpired() - o2.getExpired());

if (ret != 0) {
return ret;
}
ret = (int) (o1.op - o2.op);
if (ret != 0) {
return ret;
}
return -1;
if (ret != 0) {
return ret;
}
ret = (int) (o1.op - o2.op);
if (ret != 0) {
return ret;
}
return -1;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -63,18 +62,15 @@ private void freshState() {
//reduce the remapping
if (brokerNumMapBeforeRemapping != null
&& !brokerNumMapBeforeRemapping.isEmpty()) {
Collections.sort(leastBrokers, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int i1 = 0, i2 = 0;
if (brokerNumMapBeforeRemapping.containsKey(o1)) {
i1 = brokerNumMapBeforeRemapping.get(o1);
}
if (brokerNumMapBeforeRemapping.containsKey(o2)) {
i2 = brokerNumMapBeforeRemapping.get(o2);
}
return i1 - i2;
leastBrokers.sort((o1, o2) -> {
int i1 = 0, i2 = 0;
if (brokerNumMapBeforeRemapping.containsKey(o1)) {
i1 = brokerNumMapBeforeRemapping.get(o1);
}
if (brokerNumMapBeforeRemapping.containsKey(o2)) {
i2 = brokerNumMapBeforeRemapping.get(o2);
}
return i1 - i2;
});
} else {
//reduce the imbalance
Expand Down Expand Up @@ -342,12 +338,7 @@ public static void checkPhysicalQueueConsistence(Map<String, TopicConfigAndQueu


public static Map<Integer, TopicQueueMappingOne> checkAndBuildMappingItems(List<TopicQueueMappingDetail> mappingDetailList, boolean replace, boolean checkConsistence) {
Collections.sort(mappingDetailList, new Comparator<TopicQueueMappingDetail>() {
@Override
public int compare(TopicQueueMappingDetail o1, TopicQueueMappingDetail o2) {
return (int)(o2.getEpoch() - o1.getEpoch());
}
});
mappingDetailList.sort((o1, o2) -> (int) (o2.getEpoch() - o1.getEpoch()));

int maxNum = 0;
Map<Integer, TopicQueueMappingOne> globalIdMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.apache.rocketmq.remoting.rpc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -121,12 +119,7 @@ public static ConcurrentMap<MessageQueue, String> topicRouteData2EndpointsForSta
Map<String, TopicQueueMappingInfo> topicQueueMappingInfoMap = mapEntry.getValue();
ConcurrentMap<MessageQueue, TopicQueueMappingInfo> mqEndPoints = new ConcurrentHashMap<>();
List<Map.Entry<String, TopicQueueMappingInfo>> mappingInfos = new ArrayList<>(topicQueueMappingInfoMap.entrySet());
Collections.sort(mappingInfos, new Comparator<Map.Entry<String, TopicQueueMappingInfo>>() {
@Override
public int compare(Map.Entry<String, TopicQueueMappingInfo> o1, Map.Entry<String, TopicQueueMappingInfo> o2) {
return (int) (o2.getValue().getEpoch() - o1.getValue().getEpoch());
}
});
mappingInfos.sort((o1, o2) -> (int) (o2.getValue().getEpoch() - o1.getValue().getEpoch()));
int maxTotalNums = 0;
long maxTotalNumOfEpoch = -1;
for (Map.Entry<String, TopicQueueMappingInfo> entry : mappingInfos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,9 @@ public static TreeMap<String, String> buildSchemaOfMethods(Class apiClass) throw
continue;
}
Class<?>[] parameterTypes = method.getParameterTypes();
Arrays.sort(parameterTypes, new Comparator<Class<?>>() {
@Override
public int compare(Class<?> o1, Class<?> o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(parameterTypes, Comparator.comparing(Class::getName));
Class<?>[] exceptionTypes = method.getExceptionTypes();
Arrays.sort(exceptionTypes, new Comparator<Class<?>>() {
@Override
public int compare(Class<?> o1, Class<?> o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(exceptionTypes, Comparator.comparing(Class::getName));
String key = String.format("Method %s(%s)", method.getName(), Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.joining(",")));
String value = String.format("%s throws (%s): %s",
isPublicOrPrivate(method.getModifiers()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -107,11 +105,7 @@ public static void waitForInput(String keyWord, String info) {

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
list.sort(Map.Entry.comparingByValue());

Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -196,12 +194,7 @@ private Map<Integer, List<MessageExt>> computeMessageByQueue(Collection<Object>
messagesByQueue.get(messageExt.getQueueId()).add(messageExt);
}
for (List<MessageExt> msgEachQueue : messagesByQueue.values()) {
Collections.sort(msgEachQueue, new Comparator<MessageExt>() {
@Override
public int compare(MessageExt o1, MessageExt o2) {
return (int) (o1.getQueueOffset() - o2.getQueueOffset());
}
});
msgEachQueue.sort((o1, o2) -> (int) (o1.getQueueOffset() - o2.getQueueOffset()));
}
return messagesByQueue;
}
Expand Down

0 comments on commit 33371e5

Please sign in to comment.