1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import java.util.concurrent.TimeUnit;
20
21 import org.openjdk.jmh.annotations.Benchmark;
22 import org.openjdk.jmh.annotations.BenchmarkMode;
23 import org.openjdk.jmh.annotations.Mode;
24 import org.openjdk.jmh.annotations.OutputTimeUnit;
25 import org.openjdk.jmh.annotations.Scope;
26 import org.openjdk.jmh.annotations.State;
27 import org.openjdk.jmh.annotations.Warmup;
28
29
30
31
32 @BenchmarkMode(Mode.AverageTime)
33 @OutputTimeUnit(TimeUnit.NANOSECONDS)
34 @State(Scope.Thread)
35 @Warmup(iterations = 5, time = 10)
36 public class StringUtilsIsMixedCaseTest {
37
38 public static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
39 public static final String END_MATCH = "at the enD";
40 public static final String Middle_MATCH = "at tHe Mid";
41 public static final String EARLY_MATCH = "At tHe beginning";
42
43 public static boolean oldIsMixedCase(final CharSequence cs) {
44 if (StringUtils.isEmpty(cs) || cs.length() == 1) {
45 return false;
46 }
47 boolean containsUppercase = false;
48 boolean containsLowercase = false;
49 final int sz = cs.length();
50 for (int i = 0; i < sz; i++) {
51 if (containsUppercase && containsLowercase) {
52 return true;
53 }
54 if (Character.isUpperCase(cs.charAt(i))) {
55 containsUppercase = true;
56 } else if (Character.isLowerCase(cs.charAt(i))) {
57 containsLowercase = true;
58 }
59 }
60 return containsUppercase && containsLowercase;
61 }
62
63 @Benchmark
64 public boolean newIsMixedCaseBeginningMatch() {
65 return StringUtils.isMixedCase(EARLY_MATCH);
66 }
67
68 @Benchmark
69 public boolean newIsMixedCaseEndMatch() {
70 return StringUtils.isMixedCase(END_MATCH);
71 }
72
73 @Benchmark
74 public boolean newIsMixedCaseMiddleMatch() {
75 return StringUtils.isMixedCase(Middle_MATCH);
76 }
77
78 @Benchmark
79 public boolean newIsMixedCaseNoneMatch() {
80 return StringUtils.isMixedCase(LOWER_CASE_LETTERS);
81 }
82
83 @Benchmark
84 public boolean oldIsMixedCaseBeginningMatch() {
85 return oldIsMixedCase(EARLY_MATCH);
86 }
87
88 @Benchmark
89 public boolean oldIsMixedCaseEndMatch() {
90 return oldIsMixedCase(END_MATCH);
91 }
92
93 @Benchmark
94 public boolean oldIsMixedCaseMiddleMatch() {
95 return oldIsMixedCase(Middle_MATCH);
96 }
97
98 @Benchmark
99 public boolean oldIsMixedCaseNoneMatch() {
100 return oldIsMixedCase(LOWER_CASE_LETTERS);
101 }
102 }