com.davidbracewell.string.StringFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango Show documentation
Show all versions of mango Show documentation
A set of utilities and tools to speed up and ease programming in Java.
/*
* (c) 2005 David B. Bracewell
*
* 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 com.davidbracewell.string;
import com.davidbracewell.function.SerializableFunction;
import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import java.text.Normalizer;
import java.util.regex.Pattern;
/**
* @author David B. Bracewell
*/
public enum StringFunctions implements SerializableFunction {
/**
* Transforms a string into upper case format
*/
UPPER_CASE {
@Override
public String apply(String input) {
return input == null ? null : input.toUpperCase();
}
},
/**
* Transforms a string into lower case format
*/
LOWER_CASE {
@Override
public String apply(String input) {
return input == null ? null : input.toLowerCase();
}
},
/**
* Transforms a string into title case format
*/
TITLE_CASE {
@Override
public String apply(String input) {
if (input == null) {
return null;
}
if (input.isEmpty()) {
return input;
}
char[] chars = input.toLowerCase().toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
for (int i = 1; i < input.length() - 1; i++) {
if (Character.isWhitespace(chars[i - 1])) {
chars[i] = Character.toUpperCase(chars[i]);
}
}
return new String(chars);
}
},
/**
* Reverses a string
*/
REVERSE {
@Override
public String apply(String input) {
return input == null ? null : new StringBuilder(input).reverse().toString();
}
},
/**
* Trims a function using unicode whitespace and invisible characters
*/
TRIM {
@Override
public String apply(String input) {
return input == null ? null : CharMatcher.INVISIBLE.trimFrom(CharMatcher.WHITESPACE.trimFrom(input));
}
},
/**
* Normalizes the string using {@link java.text.Normalizer.Form#NFKC}
*/
CANONICAL_NORMALIZATION {
@Override
public String apply(String input) {
return input == null ? null : Normalizer.normalize(input, Normalizer.Form.NFKC);
}
},
/**
* Normalizes the string using by removing diacritics
*/
DIACRITICS_NORMALIZATION {
@Override
public String apply(String input) {
return input == null ? null :
Normalizer.normalize(
Normalizer.normalize(input, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""),
Normalizer.Form.NFC);
}
},
LEFT_TRIM {
@Override
public String apply(String input) {
if (input == null) {
return null;
}
return CharMatcher.INVISIBLE.and(CharMatcher.BREAKING_WHITESPACE).and(CharMatcher.WHITESPACE).trimLeadingFrom(input);
}
},
RIGHT_TRIM {
@Override
public String apply(String input) {
if (input == null) {
return null;
}
return CharMatcher.INVISIBLE.and(CharMatcher.BREAKING_WHITESPACE).and(CharMatcher.WHITESPACE).trimTrailingFrom(input);
}
},
NULL_TO_EMPTY {
@Override
public String apply(String input) {
return input == null ? StringUtils.EMPTY : input;
}
};
/**
* Creates a function that performs a regular expression replacement on a
* string
*
* @param pattern The regex pattern
* @param replacement The replacement text
* @return The function
*/
public static SerializableFunction REGEX_REPLACE(String pattern, String replacement) {
return REGEX_REPLACE(
Pattern.compile(Preconditions.checkNotNull(pattern)),
Preconditions.checkNotNull(replacement));
}
/**
* Creates a function that performs a regular expression replacement on a
* string
*
* @param pattern The regex pattern
* @param replacement The replacement text
* @return The function
*/
public static SerializableFunction REGEX_REPLACE(final Pattern pattern, final String replacement) {
Preconditions.checkNotNull(pattern);
Preconditions.checkNotNull(replacement);
return arg0 -> arg0 == null ? null : pattern.matcher(arg0).replaceAll(replacement);
}
}//END OF StringFunctions
© 2015 - 2025 Weber Informatics LLC | Privacy Policy