📄 stringvalue.java
字号:
/* * 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.wicket.util.string;import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;import java.text.NumberFormat;import java.text.ParseException;import java.util.Locale;import org.apache.wicket.IClusterable;import org.apache.wicket.util.time.Duration;import org.apache.wicket.util.time.Time;/** * Holds an immutable String value and optionally a Locale, with methods to convert to various * types. Also provides some handy parsing methods and a variety of static factory methods. * <p> * Objects can be constructed directly from Strings or by using the valueOf() static factory * methods. The repeat() static factory methods provide a way of generating a String value that * repeats a given char or String a number of times. * <p> * Conversions to a wide variety of types can be found in the to*() methods. A generic conversion * can be achieved with to(Class). * <P> * The beforeFirst(), afterFirst(), beforeLast() and afterLast() methods are handy for parsing * things like paths and filenames. * * @author Jonathan Locke */public class StringValue implements IClusterable{ private static final long serialVersionUID = 1L; /** Locale to be used for formatting and parsing. */ private final Locale locale; /** The underlying string. */ private final String text; /** * @param times * Number of times to repeat character * @param c * Character to repeat * @return Repeated character string */ public static StringValue repeat(final int times, final char c) { final AppendingStringBuffer buffer = new AppendingStringBuffer(times); for (int i = 0; i < times; i++) { buffer.append(c); } return valueOf(buffer); } /** * @param times * Number of times to repeat string * @param s * String to repeat * @return Repeated character string */ public static StringValue repeat(final int times, final String s) { final AppendingStringBuffer buffer = new AppendingStringBuffer(times); for (int i = 0; i < times; i++) { buffer.append(s); } return valueOf(buffer); } /** * Converts the given input to an instance of StringValue. * * @param value * Double precision value * @return String value formatted with one place after decimal */ public static StringValue valueOf(final double value) { return valueOf(value, Locale.getDefault()); } /** * Converts the given input to an instance of StringValue. * * @param value * Double precision value * @param places * Number of places after decimal * @param locale * Locale to be used for formatting * @return String value formatted with the given number of places after decimal */ public static StringValue valueOf(final double value, final int places, final Locale locale) { if (Double.isNaN(value) || Double.isInfinite(value)) { return valueOf("N/A"); } else { final DecimalFormat format = new DecimalFormat("#." + repeat(places, '#'), new DecimalFormatSymbols(locale)); return valueOf(format.format(value)); } } /** * Converts the given input to an instance of StringValue. * * @param value * Double precision value * @param locale * Locale to be used for formatting * @return String value formatted with one place after decimal */ public static StringValue valueOf(final double value, final Locale locale) { return valueOf(value, 1, locale); } /** * Converts the given input to an instance of StringValue. * * @param object * An object * @return String value for object */ public static StringValue valueOf(final Object object) { return valueOf(Strings.toString(object)); } /** * Converts the given input to an instance of StringValue. * * @param object * An object * @param locale * Locale to be used for formatting * @return String value for object */ public static StringValue valueOf(final Object object, final Locale locale) { return valueOf(Strings.toString(object), locale); } /** * Converts the given input to an instance of StringValue. * * @param string * A string * @return String value for string */ public static StringValue valueOf(final String string) { return new StringValue(string); } /** * Converts the given input to an instance of StringValue. * * @param string * A string * @param locale * Locale to be used for formatting * @return String value for string */ public static StringValue valueOf(final String string, final Locale locale) { return new StringValue(string, locale); } /** * Converts the given input to an instance of StringValue. * * @param buffer * A string buffer * @return String value */ public static StringValue valueOf(final AppendingStringBuffer buffer) { return valueOf(buffer.toString()); } /** * Private constructor to force use of static factory methods. * * @param text * The text for this string value */ protected StringValue(final String text) { this.text = text; locale = Locale.getDefault(); } /** * Private constructor to force use of static factory methods. * * @param text * The text for this string value * @param locale * the locale for formatting and parsing */ protected StringValue(final String text, final Locale locale) { this.text = text; this.locale = locale; } /** * Gets the substring after the first occurrence given char. * * @param c * char to scan for * @return the substring */ public final String afterFirst(final char c) { return Strings.afterFirst(text, c); } /** * Gets the substring after the last occurrence given char. * * @param c * char to scan for * @return the substring */ public final String afterLast(final char c) { return Strings.afterLast(text, c); } /** * Gets the substring before the first occurrence given char. * * @param c * char to scan for * @return the substring */ public final String beforeFirst(final char c) { return Strings.beforeFirst(text, c); } /** * Gets the substring before the last occurrence given char. * * @param c * char to scan for * @return the substring */ public final String beforeLast(final char c) { return Strings.afterLast(text, c); } /** * Replaces on this text. * * @param searchFor * What to search for * @param replaceWith * What to replace with * @return This string value with searchFor replaces with replaceWith */ public final CharSequence replaceAll(final CharSequence searchFor, final CharSequence replaceWith) { return Strings.replaceAll(text, searchFor, replaceWith); } /** * Converts this StringValue to a given type. * * @param type * The type to convert to * @return The converted value * @throws StringValueConversionException */ public final Object to(final Class type) throws StringValueConversionException { if (type == String.class) { return toString(); } if (type == Integer.TYPE || type == Integer.class) { return toInteger(); } if (type == Long.TYPE || type == Long.class) { return toLongObject(); } if (type == Boolean.TYPE || type == Boolean.class) { return toBooleanObject(); } if (type == Double.TYPE || type == Double.class) { return toDoubleObject(); } if (type == Character.TYPE || type == Character.class) { return toCharacter(); } if (type == Time.class) { return toTime(); } if (type == Duration.class) { return toDuration(); } throw new StringValueConversionException("Cannot convert '" + toString() + "'to type " + type); } /** * Convert this text to a boolean. * * @return This string value as a boolean * @throws StringValueConversionException */ public final boolean toBoolean() throws StringValueConversionException { return Strings.isTrue(text); } /** * Convert to primitive types, returning default value if text is null. * * @param defaultValue
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -