org.opencastproject.util.data.functions.Strings Maven / Gradle / Ivy
/**
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community 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://opensource.org/licenses/ecl2.txt
*
* 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.opencastproject.util.data.functions;
import static org.opencastproject.util.data.Collections.list;
import static org.opencastproject.util.data.Collections.nil;
import static org.opencastproject.util.data.Option.none;
import static org.opencastproject.util.data.Option.some;
import org.opencastproject.util.data.Arrays;
import org.opencastproject.util.data.Function;
import org.opencastproject.util.data.Function2;
import org.opencastproject.util.data.Option;
import org.opencastproject.util.data.Predicate;
import org.apache.commons.lang3.StringUtils;
import java.text.Format;
import java.util.List;
import java.util.regex.Pattern;
/** Functions for strings. */
public final class Strings {
private Strings() {
}
private static final List NIL = nil();
private static final Option NONE = none();
/**
* Trim a string and return either some
or none
if it's empty. The string may be null.
*/
public static final Function> trimToNone = new Function>() {
@Override
public Option apply(String a) {
return trimToNone(a);
}
};
/**
* Trim a string and return either some
or none
if it's empty. The string may be null.
*/
public static Option trimToNone(String a) {
if (a != null) {
final String trimmed = a.trim();
return trimmed.length() > 0 ? some(trimmed) : NONE;
} else {
return none();
}
}
/** Return a.toString()
wrapped in a some if a != null
, none otherwise. */
public static Option asString(Object a) {
return a != null ? some(a.toString()) : NONE;
}
/** Return a.toString()
wrapped in a some if a != null
, none otherwise. */
public static Function> asString() {
return new Function>() {
@Override
public Option apply(A a) {
return asString(a);
}
};
}
/** Return a.toString()
or <null>
if argument is null. */
public static Function asStringNull() {
return new Function() {
@Override
public String apply(A a) {
return a != null ? a.toString() : "";
}
};
}
/** Convert a string into a long if possible. */
public static final Function> toLong = new Function>() {
@Override
public Option apply(String s) {
try {
return some(Long.parseLong(s));
} catch (NumberFormatException e) {
return none();
}
}
};
/** Convert a string into a long if possible. */
public static final Function> toDouble = new Function>() {
@Override
public Option apply(String s) {
try {
return some(Double.parseDouble(s));
} catch (NumberFormatException e) {
return none();
}
}
};
/** Convert a string into an integer if possible. */
public static final Function> toInt = new Function>() {
@Override
public Option apply(String s) {
try {
return some(Integer.parseInt(s));
} catch (NumberFormatException e) {
return none();
}
}
};
/** Convert a string into an integer if possible. */
public static final Function> toIntL = new Function>() {
@Override
public List apply(String s) {
try {
return list(Integer.parseInt(s));
} catch (NumberFormatException e) {
return nil();
}
}
};
/**
* Convert a string into a boolean.
*
* @see Boolean#valueOf(String)
*/
public static final Function toBool = new Function() {
@Override
public Boolean apply(String s) {
return Boolean.valueOf(s);
}
};
/**
* Return a string formatting function.
*
* @see String#format(String, Object...)
*/
public static Function2 format() {
return new Function2() {
@Override
public String apply(String s, A[] p) {
return String.format(s, p);
}
};
}
public static Function format(final Format f) {
return new Function() {
@Override
public String apply(A a) {
return f.format(a);
}
};
}
public static final Predicate notBlank = new Predicate() {
@Override
public Boolean apply(String a) {
return StringUtils.isNotBlank(a);
}
};
public static final Function> trimToNil = new Function>() {
@Override
public List apply(String a) {
if (a != null) {
final String trimmed = a.trim();
return trimmed.length() > 0 ? list(trimmed) : NIL;
} else {
return NIL;
}
}
};
public static Predicate eqIgnoreCase(final String other) {
return new Predicate() {
@Override
public Boolean apply(String s) {
return s.equalsIgnoreCase(other);
}
};
}
/**
* Return a function that replaces all occurrences of regex
in the argument with replacement
* .
*
* @see String#replaceAll(String, String)
*/
public static Function replaceAll(final String regex, final String replacement) {
return new Function() {
@Override
public String apply(String s) {
return s.replaceAll(regex, replacement);
}
};
}
/** Create a {@linkplain Pattern#split(CharSequence) split} function from a regex pattern. */
public static Function split(final Pattern splitter) {
return new Function() {
@Override
public String[] apply(String s) {
return splitter.split(s);
}
};
}
/**
* Split function to split comma separated values. Regex = \s*,\s*
.
*/
public static final Function csvSplit = split(Pattern.compile("\\s*,\\s*"));
/**
* Split function to split comma separated values. Regex = \s*,\s*
.
*/
public static final Function> csvSplitList = Arrays. toList().o(csvSplit);
/** A function to prepend the argument string with a prefix. */
public static Function prepend(final String prefix) {
return new Function() {
@Override
public String apply(String s) {
return prefix + s;
}
};
}
/** A function to append a suffix to the argument string. */
public static Function append(final String suffix) {
return new Function() {
@Override
public String apply(String s) {
return s + suffix;
}
};
}
/**
* A function to convert all of the characters in this String to lower case using the rules of the default locale.
*/
public static final Function lowerCase = new Function() {
@Override
public String apply(String s) {
return s.toLowerCase();
}
};
/**
* A predicate function to match a regular expression.
*/
public static Predicate matches(final String pattern) {
return new Predicate() {
@Override
public Boolean apply(String s) {
return s.matches(pattern);
}
};
}
/**
* A predicate function to check if a string contains a specified sequence.
*/
public static Predicate contains(final String seq) {
return new Predicate() {
@Override
public Boolean apply(String s) {
return s.contains(seq);
}
};
}
/**
* Return a string concatenation function.
*/
public static Function2 concat(final String sep) {
return new Function2() {
@Override
public String apply(String a, String b) {
return a + sep + b;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy