localeconverter.java
来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 47 行
JAVA
47 行
package com.thoughtworks.xstream.converters.extended;import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter;import java.util.Locale;/** * Converts a java.util.Locale to a string. * * @author Jose A. Illescas * @author Joe Walnes */public class LocaleConverter extends AbstractBasicConverter { public boolean canConvert(Class type) { return type.equals(Locale.class); } protected Object fromString(String str) { int[] underscorePositions = underscorePositions(str); String language, country, variant; if (underscorePositions[0] == -1) { // "language" language = str; country = ""; variant = ""; } else if (underscorePositions[1] == -1) { // "language_country" language = str.substring(0, underscorePositions[0]); country = str.substring(underscorePositions[0] + 1); variant = ""; } else { // "language_country_variant" language = str.substring(0, underscorePositions[0]); country = str.substring(underscorePositions[0] + 1, underscorePositions[1]); variant = str.substring(underscorePositions[1] + 1); } return new Locale(language, country, variant); } private int[] underscorePositions(String in) { int[] result = new int[2]; for (int i = 0; i < result.length; i++) { int last = i == 0 ? 0 : result[i - 1]; result[i] = in.indexOf('_', last + 1); } return result; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?