Wednesday, October 21, 2015

Apache Commons Source Code



org.apache.commons.lang3.StringUtils.replaceChars(String, String, String)
     * StringUtils.replaceChars("abcba", "bc", "yz")  = "ayzya"
     * StringUtils.replaceChars("abcba", "bc", "y")   = "ayya"
     * StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
public static String replaceChars(final String str, final String searchChars, String replaceChars) {
    if (isEmpty(str) || isEmpty(searchChars)) {
        return str;
    }
    if (replaceChars == null) {
        replaceChars = EMPTY;
    }
    boolean modified = false;
    final int replaceCharsLength = replaceChars.length();
    final int strLength = str.length();
    final StringBuilder buf = new StringBuilder(strLength);
    for (int i = 0; i < strLength; i++) {
        final char ch = str.charAt(i);
        final int index = searchChars.indexOf(ch);
        if (index >= 0) {
            modified = true;
            if (index < replaceCharsLength) {
                buf.append(replaceChars.charAt(index));
            }
        } else {
            buf.append(ch);
        }
    }
    if (modified) {
        return buf.toString();
    }
    return str;
}
比较两个List是否相等(相同元素)
两个数组相等,就是意味着他们有相同的长度,相同的元素,以及相同的顺序
1. 检查是否指向同一个地址(不同引用);
2. 非空检查;
3. 长度检查;
4. 顺序和对应的元素相等检查。(依次比较,只要一个不等就返回false)
  1. public static boolean equals(int[] a, int[] a2) {  
  2.     if (a==a2) return true;  
  3.     if (a==null || a2==nullreturn false;  
  4.     int length = a.length;  
  5.     if (a2.length != length) return false;  
  6.     for (int i=0; i<length; i++)  
  7.         if (a[i] != a2[i]) return false;  
  8.     return true;  
  9. }
java.util.AbstractCollection.containsAll(Collection<?>)
  1. public boolean containsAll(Collection<?> c) {  
  2.     for (Object e : c)  
  3.         if (!contains(e))  
  4.             return false;  
  5.     return true;  
  6. }  
org.apache.commons.collections4.CollectionUtils
    public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) {
        if(a.size() != b.size()) {
            return false;
        }
        final CardinalityHelper<Object> helper = new CardinalityHelper<Object>(a, b);
        if(helper.cardinalityA.size() != helper.cardinalityB.size()) {
            return false;
        }
        for( final Object obj : helper.cardinalityA.keySet()) {
            if(helper.freqA(obj) != helper.freqB(obj)) {
                return false;
            }
        }
        return true;

    }
Old impl - org.apache.commons.collections.CollectionUtils.isEqualCollection() 

    private static Integer INTEGER_ONE = new Integer(1); // reuse same object
  1. private static final Integer INTEGER_ONE = 1;  
  2. public static boolean isEqualCollection(Collection a, Collection b){  
  3.     if (a.size() !=b.size()) {  // size是最简单的相等条件  
  4.        return false;  
  5.     }  
  6.     Map mapa = getCardinalityMap(a);   
  7.     Map mapb = getCardinalityMap(b);  
  8.      
  9.     // 转换map后,能去掉重复的,这时候size就是非重复项,也是先决条件  
  10.     if (mapa.size() !=mapb.size()) {    
  11.        return false;  
  12.     }  
  13.     Iterator it =mapa.keySet().iterator();  
  14.     while (it.hasNext()) {  
  15.        Object obj = it.next();  
  16.        // 查询同一个obj,首先两边都要有,而且还要校验重复个数,就是map.value  
  17.        if (getFreq(obj,mapa) != getFreq(obj, mapb)) {  
  18.            return false;  
  19.        }  
  20.     }  
  21.     return true;  
  22. }  
  23. /** 
  24.  * 以obj为key,可以防止重复,如果重复就value++ 
  25.  * 这样实际上记录了元素以及出现的次数 
  26.  */  
  27. public static Map getCardinalityMap(Collection coll) {  
  28.     Map count = new HashMap();  // change 
  29.     for (Iterator it =coll.iterator(); it.hasNext();) {  
  30.        Object obj =it.next();  
  31.        Integer c =(Integer) count.get(obj);  
  32.        if (c == null)     
  33.            count.put(obj, INTEGER_ONE);  
  34.        else {  
  35.            count.put(obj, newInteger(c.intValue() + 1));  
  36.        }  
  37.     }  
  38.     return count;  
  39. }  
  40. private static final int getFreq(Objectobj, Map freqMap) {  
  41.     Integer count =(Integer) freqMap.get(obj);  
  42.     if (count != null) {  
  43.        return count.intValue();  
  44.     }  
  45.     return 0;  
采用获取网络当前时间来代替获取系统当前时间
apache的commons-net包下面有ntp的实现. 
  1.         NTPUDPClient timeClient = new NTPUDPClient();  
  2.         String timeServerUrl = "time-a.nist.gov";  
  3.         InetAddress timeServerAddress = InetAddress.getByName(timeServerUrl);  
  4.         TimeInfo timeInfo = timeClient.getTime(timeServerAddress);  
  5.         TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();  
  6.         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  7.         System.out.println(dateFormat.format(timeStamp.getDate()));  
org.apache.commons.lang3.ObjectUtils.compare(T, T, boolean)
    public static <T extends Comparable<? super T>> int compare(final T c1, final T c2, final boolean nullGreater) {
        if (c1 == c2) {
            return 0;
        } else if (c1 == null) {
            return nullGreater ? 1 : -1;
        } else if (c2 == null) {
            return nullGreater ? -1 : 1;
        }
        return c1.compareTo(c2);

    }

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts