Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/test/java/org/apache/commons/lang3/StringUtilCenterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.lang3; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.apache.commons.lang3.StringUtils.leftPad; | ||
import static org.apache.commons.lang3.StringUtils.repeat; | ||
import static org.apache.commons.lang3.StringUtils.rightPad; | ||
|
||
/** | ||
* Test to show whether using BitSet for removeAll() methods is faster than using HashSet. | ||
*/ | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@State(Scope.Thread) | ||
public class StringUtilCenterTest { | ||
|
||
@Benchmark | ||
public void test0Old() { | ||
centerOld("a", 20, 'a'); | ||
} | ||
|
||
@Benchmark | ||
public void test0New() { | ||
centerNew("a", 20, 'a'); | ||
} | ||
|
||
@Benchmark | ||
public void test1Old() { | ||
centerOld("a", 10000000, ' '); | ||
} | ||
|
||
@Benchmark | ||
public void test1New() { | ||
centerNew("a", 10000000, ' '); | ||
} | ||
|
||
/** | ||
* <p>Centers a String in a larger String of size {@code size}. | ||
* Uses a supplied character as the value to pad the String with.</p> | ||
* | ||
* <p>If the size is less than the String length, the String is returned. | ||
* A {@code null} String returns {@code null}. | ||
* A negative size is treated as zero.</p> | ||
* | ||
* <pre> | ||
* StringUtils.center(null, *, *) = null | ||
* StringUtils.center("", 4, ' ') = " " | ||
* StringUtils.center("ab", -1, ' ') = "ab" | ||
* StringUtils.center("ab", 4, ' ') = " ab " | ||
* StringUtils.center("abcd", 2, ' ') = "abcd" | ||
* StringUtils.center("a", 4, ' ') = " a " | ||
* StringUtils.center("a", 4, 'y') = "yayy" | ||
* </pre> | ||
* | ||
* @param str the String to center, may be null | ||
* @param size the int size of new String, negative treated as zero | ||
* @param padChar the character to pad the new String with | ||
* @return centered String, {@code null} if null String input | ||
* @since 2.0 | ||
*/ | ||
public static String centerNew(String str, final int size, final char padChar) { | ||
if (str == null || size <= 0) { | ||
return str; | ||
} | ||
final int strLen = str.length(); | ||
final int pads = size - strLen; | ||
if (pads <= 0) { | ||
return str; | ||
} | ||
StringBuilder stringBuilder = new StringBuilder(size); | ||
repeat(stringBuilder, padChar, pads / 2); | ||
stringBuilder.append(str); | ||
repeat(stringBuilder, padChar, size - (strLen + pads / 2)); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
/** | ||
* <p>Centers a String in a larger String of size {@code size}. | ||
* Uses a supplied character as the value to pad the String with.</p> | ||
* | ||
* <p>If the size is less than the String length, the String is returned. | ||
* A {@code null} String returns {@code null}. | ||
* A negative size is treated as zero.</p> | ||
* | ||
* <pre> | ||
* StringUtils.center(null, *, *) = null | ||
* StringUtils.center("", 4, ' ') = " " | ||
* StringUtils.center("ab", -1, ' ') = "ab" | ||
* StringUtils.center("ab", 4, ' ') = " ab " | ||
* StringUtils.center("abcd", 2, ' ') = "abcd" | ||
* StringUtils.center("a", 4, ' ') = " a " | ||
* StringUtils.center("a", 4, 'y') = "yayy" | ||
* </pre> | ||
* | ||
* @param str the String to center, may be null | ||
* @param size the int size of new String, negative treated as zero | ||
* @param padChar the character to pad the new String with | ||
* @return centered String, {@code null} if null String input | ||
* @since 2.0 | ||
*/ | ||
public static String centerOld(String str, final int size, final char padChar) { | ||
if (str == null || size <= 0) { | ||
return str; | ||
} | ||
final int strLen = str.length(); | ||
final int pads = size - strLen; | ||
if (pads <= 0) { | ||
return str; | ||
} | ||
str = leftPad(str, strLen + pads / 2, padChar); | ||
str = rightPad(str, size, padChar); | ||
return str; | ||
} | ||
} |