📄 utils.java
字号:
package mome.ext;import javax.microedition.lcdui.Font;/** * Collection of useful static utils for parsing strings. * * @author Sergio Morozov * @version 1.1.2 */public class Utils{ /** * Protects constructor. * * @since 1.0 */ protected Utils() { super(); } /** * Parses given string and returns <code>true</code> if string is "on" or * "true" <code>false</code> otherwise. Tests are case insensitive. * * @param arg * string to parse. * @return <code>true</code> if string is "on" or "true" <code>false</code> * otherwise. * @since 1.0 */ public static boolean parseBoolean(String arg) { return arg != null && (arg.equalsIgnoreCase("true") || arg.equalsIgnoreCase("on") || arg .equalsIgnoreCase("yes")); } /** * Parses string as hexadecimal integer representation and returns color as * integer or <code>-1</code> if string is <code>null</code> or can't be * parsed. * * @param arg * string to be parsed. * @return color as integer or <code>-1</code> if string is * <code>null</code> or can't be parsed * @since 1.0 */ public static int parseColor(String arg) { int res = -1; if (arg != null) try { res = Integer.parseInt(arg, 16); } catch (Throwable e) {} return res; } /** * Parses string and returns fonts face as integer. String must be of form * * <pre> * PROPRTIONAL | MONOSPACE | SYSTEM * </pre> * * to be parsed. Test are case insensitive. Spaces are not allowed. * * @param face * string to be parsed. * @return font's face as integer. * @throws NullPointerException * if face is <code>null</code>. * @throws IllegalArgumentException * if face is not of valid form. * @since 1.0 */ public static int parseFontFace(String face) { int res = 0; if (face == null) throw new NullPointerException("face"); if (face.equalsIgnoreCase("PROPORTIONAL")) res = Font.FACE_PROPORTIONAL; else if (face.equalsIgnoreCase("MONOSPACE")) res = Font.FACE_MONOSPACE; else if (face.equalsIgnoreCase("SYSTEM")) res = Font.FACE_SYSTEM; else throw new IllegalArgumentException("invalid font face:" + face); return res; } /** * Parses string and returns fonts style as integer. String must be of form * * <pre> * (PLAIN|BOLD|ITALIC|UNDERLINED)(;(PLAIN|BOLD|ITALIC|UNDERLINED))* * </pre> * * to be parsed. Test are case insensitive. Spaces are not allowed. * * @param style * string to be parsed. * @return font's style as integer. * @throws NullPointerException * if style is <code>null</code>. * @throws IllegalArgumentException * if style is not of valid form. * @since 1.0 */ public static int parseFontStyle(String style) { int res = 0; if (style == null) throw new NullPointerException("style"); int i = 0; for (int j = style.indexOf(';', i); j > 0; i++, j = style.indexOf(';', i)) { String s = style.substring(i, i = j); if (s.equalsIgnoreCase("BOLD")) res |= Font.STYLE_BOLD; else if (s.equalsIgnoreCase("ITALIC")) res |= Font.STYLE_ITALIC; else if (s.equalsIgnoreCase("PLAIN")) res |= Font.STYLE_PLAIN; else if (s.equalsIgnoreCase("UNDERLINED")) res |= Font.STYLE_UNDERLINED; else throw new IllegalArgumentException("invalid font style: " + style); } String s = style.substring(i); if (s.equalsIgnoreCase("BOLD")) res |= Font.STYLE_BOLD; else if (s.equalsIgnoreCase("ITALIC")) res |= Font.STYLE_ITALIC; else if (s.equalsIgnoreCase("PLAIN")) res |= Font.STYLE_PLAIN; else if (s.equalsIgnoreCase("UNDERLINED")) res |= Font.STYLE_UNDERLINED; else throw new IllegalArgumentException("invalid font style: " + style); return res; } /** * Parses string and returns fonts size as integer. String must be of form * * <pre> * LARGE | MEDIUM | SMALL * </pre> * * to be parsed. Test are case insensitive. Spaces are not allowed. * * @param size * string to be parsed. * @return font's size as integer. * @throws NullPointerException * if style is <code>null</code>; * @throws IllegalArgumentException * if style is not of valid form. * @since 1.0 */ public static int parseFontSize(String size) { int res = 0; if (size != null) { if (size.equalsIgnoreCase("LARGE")) res = Font.SIZE_LARGE; else if (size.equalsIgnoreCase("MEDIUM")) res = Font.SIZE_MEDIUM; else if (size.equalsIgnoreCase("SMALL")) res = Font.SIZE_SMALL; else throw new IllegalArgumentException("invalid font size: " + size); } return res; } /** * Parses string and returns font. String must be of form * * <pre> * <Face>,<Style>,<Size> * Face = PROPRTIONAL|MONOSPACE|SYSTEM * Style = (PLAIN|BOLD|ITALIC|UNDERLINED)(;(PLAIN|BOLD|ITALIC|UNDERLINED))* * Size = LARGE|MEDIUM|SMALL * </pre> * * to be parsed. Test are case insensitive. Spaces are not allowed. * * @param arg * string to be parsed. * @return font or <code>null</code> if string is <code>null</code> or not * of valid form. * @since 1.0 */ public static Font parseFont(String arg) { Font res = null; if (arg != null) { try { int i = 0; int face = parseFontFace(arg.substring(i, i = arg.indexOf(','))); int style = parseFontStyle(arg.substring(++i, i = arg.indexOf(',', i))); int size = parseFontSize(arg.substring(++i)); res = Font.getFont(face, style, size); } catch (Throwable e) {} } return res; } /** * Array of separators. * * @since 1.0 */ public static char[] testSeparators = " \t,:;".toCharArray(); /** * Checks if the given character is separator. * * @param c * character to be tested. * @return <code>true</code> if character is separator, <code>false</code> * otherwise. * @since 1.0 */ public static boolean isTestSeparator(char c) { int i; for (i = testSeparators.length - 1; i >= 0 && c != testSeparators[i]; i--); return i >= 0; } /** * <p> * Parses given string. Returns the time interval as number of milliseconds or * <code>-1</code> if parse error occurs. String must be of the form: * </p> * <code> <DoubleLiteral>[<UnitSuffix>]</code> * <p> * <code>DoubleLiteral</code> is double number string representation as * specified by {@link Double#valueOf(String)}. * </p> * <p> * <code>UnitSuffix</code> is an optional specification of unit of time. It * can be one of the following * <ul> * <li><code>h</code> - for hours;</li> * <li><code>m</code> - for minutes;</li> * <li><code>s</code> - for seconds;</li> * <li><code>ms</code> - for milliseconds.</li> * </ul> * If omitted milliseconds are implied. <code>UnitSuffix</code> is case * insensitive (<code>ms</code>, <code>MS</code>, <code>Ms</code>, * <code>mS</code> mean the same). * </p> * * @param timeInterval * string representing time interval. * @return the time interval in milliseconds or <code>-1</code> if string is * not parsable. * @since 1.1.1 */ public static long parseTimeInterval(String timeInterval) { long res = -1; if (timeInterval != null) { int m = 1; int lastCharPos = timeInterval.length() - 1; char lastChar = timeInterval.charAt(lastCharPos); switch (lastChar) { case 'S': case 's': m = 1000; if (lastCharPos > 0 && (timeInterval.charAt(lastCharPos - 1) == 'M' || timeInterval .charAt(lastCharPos - 1) == 'm')) { m = 1; lastCharPos--; } break; case 'M': case 'm': m = 60000; break; case 'H': case 'h': m = 3600000; break; default: lastCharPos++; break; } try { res = (int) (Double.parseDouble(timeInterval.substring(0, lastCharPos)) * m); } catch (Throwable e) {} } return res; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -