`

common-long包简介

 
阅读更多

http://www.open-open.com/bbs/view/1318864045468

http://www.oschina.net/code/snippet_2765_14434

这一组API的所有包名都以org.apache.commons.lang开头,共有如下8个包:

org.apache.commons.lang

org.apache.commons.lang.builder

org.apache.commons.lang.enum

org.apache.commons.lang.enums

org.apache.commons.lang.exception

org.apache.commons.lang.math

org.apache.commons.lang.mutable

org.apache.commons.lang.time

其中的lang.enum已不建议使用,替代它的是紧随其后的lang.enums包。 lang包主要是一些可以高度重用的Util类;lang.builder包包含了一组用于产生每个Java类中都常使用到的toString()、 hashCode()、equals()、compareTo()等等方法的构造器;lang.enums包顾名思义用于处理枚 举;lang.exception包用于处理Java标准API中的exception,为1.4之前版本提供Nested Exception功能;lang.math包用于处理数字;lang.mutable用于包装值型变量;lang.time包提供处理日期和时间的功 能。

由于Commons的包和类实在很多,不可能一个一个讲了,在接下来的专题文章中我就只分别过一下lang、lang.builder、lang.math和lang.time这几个包和常见的用法,其他的我们可以在用到时临时参考一下Javadoc。位置就在安装路径的

…\commons-lang-2.1\docs\api\index.html

我们首先来看org.apache.commons.lang包,这个包提供了一些有用的包含static方法的Util类。除了6个Exception类和2个已经deprecated的数字类之外,commons.lang包共包含了17个实用的类:

ArrayUtils – 用于对数组的操作,如添加、查找、删除、子数组、倒序、元素类型转换等;

BitField – 用于操作位元,提供了一些方便而安全的方法;

BooleanUtils – 用于操作和转换boolean或者Boolean及相应的数组;

CharEncoding – 包含了Java环境支持的字符编码,提供是否支持某种编码的判断;

CharRange – 用于设定字符范围并做相应检查;

CharSet – 用于设定一组字符作为范围并做相应检查;

CharSetUtils – 用于操作CharSet;

CharUtils – 用于操作char值和Character对象;

ClassUtils – 用于对Java类的操作,不使用反射;

ObjectUtils – 用于操作Java对象,提供null安全的访问和其他一些功能;

RandomStringUtils – 用于生成随机的字符串;

SerializationUtils – 用于处理对象序列化,提供比一般Java序列化更高级的处理能力;

StringEscapeUtils – 用于正确处理转义字符,产生正确的Java、JavaScript、HTML、XML和SQL代码;

StringUtils – 处理String的核心类,提供了相当多的功能;

SystemUtils – 在java.lang.System基础上提供更方便的访问,如用户路径、Java版本、时区、操作系统等判断;

Validate – 提供验证的操作,有点类似assert断言;

WordUtils – 用于处理单词大小写、换行等。

 

 

  1. public class TestLangDemo {  
  2.   
  3.     public void charSetDemo() {  
  4.         System.out.println("**CharSetDemo**");  
  5.         CharSet charSet = CharSet.getInstance("aeiou");  
  6.         String demoStr = "The quick brown fox jumps over the lazy dog.";  
  7.         int count = 0;  
  8.         for (int i = 0, len = demoStr.length(); i < len; i++) {  
  9.             if (charSet.contains(demoStr.charAt(i))) {  
  10.                 count++;  
  11.             }  
  12.         }  
  13.         System.out.println("count: " + count);  
  14.     }  
  15.   
  16.     public void charSetUtilsDemo() {  
  17.         System.out.println("**CharSetUtilsDemo**");  
  18.         System.out.println("计算字符串中包含某字符数.");  
  19.         System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.""aeiou"));  
  20.   
  21.         System.out.println("删除字符串中某字符.");  
  22.         System.out.println(CharSetUtils.delete("The quick brown fox jumps over the lazy dog.""aeiou"));  
  23.   
  24.         System.out.println("保留字符串中某字符.");  
  25.         System.out.println(CharSetUtils.keep("The quick brown fox jumps over the lazy dog.""aeiou"));  
  26.   
  27.         System.out.println("合并重复的字符.");  
  28.         System.out.println(CharSetUtils.squeeze("a  bbbbbb     c dd""b d"));  
  29.     }  
  30.   
  31.     public void objectUtilsDemo() {  
  32.         System.out.println("**ObjectUtilsDemo**");  
  33.         System.out.println("Object为null时,默认打印某字符.");  
  34.         Object obj = null;  
  35.         System.out.println(ObjectUtils.defaultIfNull(obj, "空"));  
  36.   
  37.         System.out.println("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");  
  38.         Object a = new Object();  
  39.         Object b = a;  
  40.         Object c = new Object();  
  41.         System.out.println(ObjectUtils.equals(a, b));  
  42.         System.out.println(ObjectUtils.equals(a, c));  
  43.   
  44.         System.out.println("用父类Object的toString()方法返回对象信息.");  
  45.         Date date = new Date();  
  46.         System.out.println(ObjectUtils.identityToString(date));  
  47.         System.out.println(date);  
  48.   
  49.         System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");  
  50.         System.out.println(ObjectUtils.toString(date));  
  51.         System.out.println(ObjectUtils.toString(null));  
  52.         System.out.println(date);  
  53.     }  
  54.   
  55.     public void serializationUtilsDemo() {  
  56.         System.out.println("*SerializationUtils**");  
  57.         Date date = new Date();  
  58.         byte[] bytes = SerializationUtils.serialize(date);  
  59.         System.out.println(ArrayUtils.toString(bytes));  
  60.         System.out.println(date);  
  61.   
  62.         Date reDate = (Date) SerializationUtils.deserialize(bytes);  
  63.         System.out.println(reDate);  
  64.         System.out.println(ObjectUtils.equals(date, reDate));  
  65.         System.out.println(date == reDate);  
  66.   
  67.         FileOutputStream fos = null;  
  68.         FileInputStream fis = null;  
  69.         try {  
  70.             fos = new FileOutputStream(new File("d:/test.txt"));  
  71.             fis = new FileInputStream(new File("d:/test.txt"));  
  72.             SerializationUtils.serialize(date, fos);  
  73.             Date reDate2 = (Date) SerializationUtils.deserialize(fis);  
  74.   
  75.             System.out.println(date.equals(reDate2));  
  76.   
  77.         } catch (FileNotFoundException e) {  
  78.             e.printStackTrace();  
  79.         } finally {  
  80.             try {  
  81.                 fos.close();  
  82.                 fis.close();  
  83.             } catch (IOException e) {  
  84.                 e.printStackTrace();  
  85.             }  
  86.         }  
  87.   
  88.     }  
  89.   
  90.     public void randomStringUtilsDemo() {  
  91.         System.out.println("**RandomStringUtilsDemo**");  
  92.         System.out.println("生成指定长度的随机字符串,好像没什么用.");  
  93.         System.out.println(RandomStringUtils.random(500));  
  94.   
  95.         System.out.println("在指定字符串中生成长度为n的随机字符串.");  
  96.         System.out.println(RandomStringUtils.random(5"abcdefghijk"));  
  97.   
  98.         System.out.println("指定从字符或数字中生成随机字符串.");  
  99.         System.out.println(RandomStringUtils.random(5truefalse));  
  100.         System.out.println(RandomStringUtils.random(5falsetrue));  
  101.   
  102.     }  
  103.   
  104.     public void stringUtilsDemo() {  
  105.         System.out.println("**StringUtilsDemo**");  
  106.         System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");  
  107.         String[] header = new String[3];  
  108.         header[0] = StringUtils.repeat("*"50);  
  109.         header[1] = StringUtils.center("  StringUtilsDemo  "50"^O^");  
  110.         header[2] = header[0];  
  111.         String head = StringUtils.join(header, "\n");  
  112.         System.out.println(head);  
  113.   
  114.         System.out.println("缩短到某长度,用...结尾.");  
  115.         System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog."10));  
  116.         System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog."1510));  
  117.   
  118.         System.out.println("返回两字符串不同处索引号.");  
  119.         System.out.println(StringUtils.indexOfDifference("aaabc""aaacc"));  
  120.   
  121.         System.out.println("返回两字符串不同处开始至结束.");  
  122.         System.out.println(StringUtils.difference("aaabcde""aaaccde"));  
  123.   
  124.         System.out.println("截去字符串为以指定字符串结尾的部分.");  
  125.         System.out.println(StringUtils.chomp("aaabcde""de"));  
  126.   
  127.         System.out.println("检查一字符串是否为另一字符串的子集.");  
  128.         System.out.println(StringUtils.containsOnly("aad""aadd"));  
  129.   
  130.         System.out.println("检查一字符串是否不是另一字符串的子集.");  
  131.         System.out.println(StringUtils.containsNone("defg""aadd"));  
  132.   
  133.         System.out.println("检查一字符串是否包含另一字符串.");  
  134.         System.out.println(StringUtils.contains("defg""ef"));  
  135.         System.out.println(StringUtils.containsOnly("ef""defg"));  
  136.   
  137.         System.out.println("返回可以处理null的toString().");  
  138.         System.out.println(StringUtils.defaultString("aaaa"));  
  139.         System.out.println("?" + StringUtils.defaultString(null) + "!");  
  140.   
  141.         System.out.println("去除字符中的空格.");  
  142.         System.out.println(StringUtils.deleteWhitespace("aa  bb  cc"));  
  143.   
  144.         System.out.println("分隔符处理成数组.");  
  145.         String[] strArray = StringUtils.split("a,b,,c,d,null,e"",");  
  146.         System.out.println(strArray.length);  
  147.         System.out.println(strArray.toString());  
  148.   
  149.         System.out.println("判断是否是某类字符.");  
  150.         System.out.println(StringUtils.isAlpha("ab"));  
  151.         System.out.println(StringUtils.isAlphanumeric("12"));  
  152.         System.out.println(StringUtils.isBlank(""));  
  153.         System.out.println(StringUtils.isNumeric("123"));  
  154.     }  
  155.   
  156.     public void systemUtilsDemo() {  
  157.         System.out.println(genHeader("SystemUtilsDemo"));  
  158.         System.out.println("获得系统文件分隔符.");  
  159.         System.out.println(SystemUtils.FILE_SEPARATOR);  
  160.   
  161.         System.out.println("获得源文件编码.");  
  162.         System.out.println(SystemUtils.FILE_ENCODING);  
  163.   
  164.         System.out.println("获得ext目录.");  
  165.         System.out.println(SystemUtils.JAVA_EXT_DIRS);  
  166.   
  167.         System.out.println("获得java版本.");  
  168.         System.out.println(SystemUtils.JAVA_VM_VERSION);  
  169.   
  170.         System.out.println("获得java厂商.");  
  171.         System.out.println(SystemUtils.JAVA_VENDOR);  
  172.     }  
  173.   
  174.     public void classUtilsDemo() {  
  175.         System.out.println(genHeader("ClassUtilsDemo"));  
  176.         System.out.println("获取类实现的所有接口.");  
  177.         System.out.println(ClassUtils.getAllInterfaces(Date.class));  
  178.   
  179.         System.out.println("获取类所有父类.");  
  180.         System.out.println(ClassUtils.getAllSuperclasses(Date.class));  
  181.   
  182.         System.out.println("获取简单类名.");  
  183.         System.out.println(ClassUtils.getShortClassName(Date.class));  
  184.   
  185.         System.out.println("获取包名.");  
  186.         System.out.println(ClassUtils.getPackageName(Date.class));  
  187.   
  188.         System.out.println("判断是否可以转型.");  
  189.         System.out.println(ClassUtils.isAssignable(Date.class, Object.class));  
  190.         System.out.println(ClassUtils.isAssignable(Object.class, Date.class));  
  191.     }  
  192.   
  193.     public void stringEscapeUtilsDemo() {  
  194.         System.out.println(genHeader("StringEcsapeUtils"));  
  195.         System.out.println("转换特殊字符.");  
  196.         System.out.println("html:" + StringEscapeUtils.escapeHtml3(" "));  
  197.         System.out.println("html:" + StringEscapeUtils.escapeHtml4(" "));  
  198.         System.out.println("html:" + StringEscapeUtils.unescapeHtml3("<p>"));  
  199.         System.out.println("html:" + StringEscapeUtils.unescapeHtml4("<p>"));  
  200.     }  
  201.   
  202.     private final class BuildDemo {  
  203.         String name;  
  204.         int age;  
  205.   
  206.         public BuildDemo(String name, int age) {  
  207.             this.name = name;  
  208.             this.age = age;  
  209.         }  
  210.   
  211.         public String toString() {  
  212.             ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);  
  213.             tsb.append("Name", name);  
  214.             tsb.append("Age", age);  
  215.             return tsb.toString();  
  216.         }  
  217.   
  218.         public int hashCode() {  
  219.             HashCodeBuilder hcb = new HashCodeBuilder();  
  220.             hcb.append(name);  
  221.             hcb.append(age);  
  222.             return hcb.hashCode();  
  223.         }  
  224.   
  225.         public boolean equals(Object obj) {  
  226.             if (!(obj instanceof BuildDemo)) {  
  227.                 return false;  
  228.             }  
  229.             BuildDemo bd = (BuildDemo) obj;  
  230.             EqualsBuilder eb = new EqualsBuilder();  
  231.             eb.append(name, bd.name);  
  232.             eb.append(age, bd.age);  
  233.             return eb.isEquals();  
  234.         }  
  235.     }  
  236.   
  237.     public void builderDemo() {  
  238.         System.out.println(genHeader("BuilderDemo"));  
  239.         BuildDemo obj1 = new BuildDemo("a"1);  
  240.         BuildDemo obj2 = new BuildDemo("b"2);  
  241.         BuildDemo obj3 = new BuildDemo("a"1);  
  242.   
  243.         System.out.println("toString()");  
  244.         System.out.println(obj1);  
  245.         System.out.println(obj2);  
  246.         System.out.println(obj3);  
  247.   
  248.         System.out.println("hashCode()");  
  249.         System.out.println(obj1.hashCode());  
  250.         System.out.println(obj2.hashCode());  
  251.         System.out.println(obj3.hashCode());  
  252.   
  253.         System.out.println("equals()");  
  254.         System.out.println(obj1.equals(obj2));  
  255.         System.out.println(obj1.equals(obj3));  
  256.     }  
  257.   
  258.     public void numberUtils() {  
  259.         System.out.println(genHeader("NumberUtils"));  
  260.         System.out.println("字符串转为数字(不知道有什么用).");  
  261.         System.out.println(NumberUtils.toInt("ba"33));  
  262.   
  263.         System.out.println("从数组中选出最大值.");  
  264.         System.out.println(NumberUtils.max(new int[] { 1234 }));  
  265.   
  266.         System.out.println("判断字符串是否全是整数.");  
  267.         System.out.println(NumberUtils.isDigits("123.1"));  
  268.   
  269.         System.out.println("判断字符串是否是有效数字.");  
  270.         System.out.println(NumberUtils.isNumber("0123.1"));  
  271.     }  
  272.   
  273.     public void dateFormatUtilsDemo() {  
  274.         System.out.println(genHeader("DateFormatUtilsDemo"));  
  275.         System.out.println("格式化日期输出.");  
  276.         System.out.println(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));  
  277.   
  278.         System.out.println("秒表.");  
  279.         StopWatch sw = new StopWatch();  
  280.         sw.start();  
  281.   
  282.         for (Iterator iterator = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {  
  283.             Calendar cal = (Calendar) iterator.next();  
  284.             System.out.println(DateFormatUtils.format(cal.getTime(), "yy-MM-dd HH:mm"));  
  285.         }  
  286.   
  287.         sw.stop();  
  288.         System.out.println("秒表计时:" + sw.getTime());  
  289.   
  290.     }  
  291.   
  292.     private String genHeader(String head) {  
  293.         String[] header = new String[3];  
  294.         header[0] = StringUtils.repeat("*"50);  
  295.         header[1] = StringUtils.center("  " + head + "  "50"^O^");  
  296.         header[2] = header[0];  
  297.         return StringUtils.join(header, "\n");  
  298.     }  
  299.   
  300.     private void validateDemo() {  
  301.         String[] strarray = { "a""b""c" };  
  302.         System.out.println("验证功能");  
  303.         System.out.println(Validate.notEmpty(strarray));  
  304.     }  
  305.   
  306.     private void wordUtilsDemo() {  
  307.         System.out.println("单词处理功能");  
  308.         String str1 = "wOrD";  
  309.         String str2 = "ghj\nui\tpo";  
  310.         System.out.println(WordUtils.capitalize(str1)); // 首字母大写  
  311.         System.out.println(WordUtils.capitalizeFully(str1)); // 首字母大写其它字母小写  
  312.         char[] ctrg = { '.' };  
  313.         System.out.println(WordUtils.capitalizeFully("i aM.fine", ctrg)); // 在规则地方转换  
  314.         System.out.println(WordUtils.initials(str1)); // 获取首字母  
  315.         System.out.println(WordUtils.initials("Ben John Lee"null)); // 取每个单词的首字母  
  316.         char[] ctr = { ' ''.' };  
  317.         System.out.println(WordUtils.initials("Ben J.Lee", ctr)); // 按指定规则获取首字母  
  318.         System.out.println(WordUtils.swapCase(str1)); // 大小写逆转  
  319.         System.out.println(WordUtils.wrap(str2, 1)); // 解析\n和\t等字符  
  320.     }  
  321.   
  322.     /** 
  323.      * @param args 
  324.      */  
  325.     public static void main(String[] args) {  
  326.         TestLangDemo langDemo = new TestLangDemo();  
  327.   
  328.         langDemo.charSetDemo();  
  329.         langDemo.charSetUtilsDemo();  
  330.         langDemo.objectUtilsDemo();  
  331.         langDemo.serializationUtilsDemo();  
  332.         langDemo.randomStringUtilsDemo();  
  333.         langDemo.stringUtilsDemo();  
  334.         langDemo.systemUtilsDemo();  
  335.         langDemo.classUtilsDemo();  
  336.         langDemo.stringEscapeUtilsDemo();  
  337.         langDemo.builderDemo();  
  338.         langDemo.numberUtils();  
  339.         langDemo.dateFormatUtilsDemo();  
  340.         langDemo.validateDemo();  
  341.         langDemo.wordUtilsDemo();  
  342.     }  
  343.   

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics