1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| import com.google.common.collect.Lists; import java.util.concurrent.Callable; import lombok.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.StopWatch; import org.junit.jupiter.api.Test;
import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
@Slf4j class SortTest {
private static final List<Bean> list = Lists.newArrayList( new Bean("1", "abc"), new Bean("3", "zzz"), new Bean("2", "ddd"), new Bean("2", "aaa"), new Bean("8", "ccc"), new Bean("6", "bbc"));
@Test void sort1() { Collections.sort(list, new Comparator<>() { @Override public int compare(Bean a, Bean b) { return a.getId().compareTo(b.getId()); } }); for (Bean bean : list) { log.debug("id={}", bean.getId()); } assertTrue(true); }
@Test void sort2() { list.sort(Comparator.comparing(Bean::getId)); list.forEach(bean -> log.debug("id={}", bean.getId())); assertTrue(true); }
@Test void sort2_1() { list.sort(Comparator.comparing(Bean::getId).reversed()); list.forEach(bean -> log.debug("id={}", bean.getId())); assertTrue(true); }
@Test void sort3() { list.sort(Bean::compareById); list.forEach(bean -> log.debug("id={}", bean.getId())); assertTrue(true); }
@Test void sort3_1() { Collections.sort(list, Bean::compareById); list.forEach(bean -> log.debug("id={},name={}", bean.getId(), bean.getName())); assertTrue(true); }
@Test void sort4() { list.sort(Bean.getInstance()::compare); list.forEach(bean -> log.debug("id={}", bean.getId())); assertTrue(true); }
@Test void sort5() { list.sort(Comparator.comparing(Bean::getId).thenComparing(Bean::getName)); list.forEach(bean -> log.debug("id={},name={}", bean.getId(), bean.getName())); assertTrue(true); }
@Test void sort6() { List<Bean> list1 = list.stream().sorted(Comparator.comparing(Bean::getId)).toList(); list1.forEach(bean -> log.debug("id={},name={}", bean.getId(), bean.getName())); assertTrue(true); }
@Test void sort7() { List<Bean> list1 = list.parallelStream().sorted(Comparator.comparing(Bean::getId)).toList(); list1.forEach(bean -> log.debug("id={},name={}", bean.getId(), bean.getName())); assertTrue(true); }
@Test void stopWatch() throws InterruptedException { StopWatch sw1 = new StopWatch(); sw1.start(); StopWatch sw2 = StopWatch.create(); sw2.start(); StopWatch sw3 = StopWatch.createStarted();
TimeUnit.SECONDS.sleep(1L); await().atMost(2L, TimeUnit.SECONDS).until(didTheThing()); log.info("time={}", sw3.getTime()); await().atMost(400L, TimeUnit.MILLISECONDS).until(didTheThing()); log.info("time={}", sw3.getTime()); sw3.suspend(); await().atMost(500L, TimeUnit.MILLISECONDS).until(didTheThing()); log.info("time={}", sw3.getTime()); sw3.resume(); await().atMost(500L, TimeUnit.MILLISECONDS).until(didTheThing()); log.info("time={}", sw3.getTime()); assertTrue(true); }
@Test void stopWatchSpring() throws InterruptedException { org.springframework.util.StopWatch sw1 = new org.springframework.util.StopWatch("test task times");
sw1.start("task a"); await().atMost(2L, TimeUnit.SECONDS).until(didTheThing()); sw1.stop(); log.info("time={}", sw1.getLastTaskTimeMillis());
sw1.start("task b"); await().atMost(500L, TimeUnit.MILLISECONDS).until(didTheThing()); sw1.stop(); log.info("time={}", sw1.getLastTaskTimeMillis());
log.info("count={}, total time={}", sw1.getTaskCount(), sw1.getTotalTimeMillis()); log.debug(sw1.prettyPrint()); assertTrue(true); }
private Callable<Boolean> didTheThing() { return () -> { return true; }; }
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @ToString static class Bean {
private static final Bean BEAN = new Bean(); private String id; private String name;
public static Bean getInstance() { return BEAN; }
public static int compareById(Bean a, Bean b) { return a.getId().compareTo(b.getId()); }
public int compare(Bean a, Bean b) { return a.getId().compareTo(b.getId()); } } }
|