View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test to show the better performance of the new implementation of isMixedCase
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 }