博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Strings类的简单使用-代码优雅之道Guava(一)
阅读量:6930 次
发布时间:2019-06-27

本文共 4937 字,大约阅读时间需要 16 分钟。

hot3.png

package com.google.common.base;import com.google.common.annotations.GwtCompatible;import com.google.common.annotations.VisibleForTesting;import javax.annotation.CheckReturnValue;import javax.annotation.Nullable;@CheckReturnValue@GwtCompatiblepublic final class Strings {    private Strings() {    }    public static String nullToEmpty(@Nullable String string) {        return string == null ? "" : string;    }    @Nullable    public static String emptyToNull(@Nullable String string) {        return isNullOrEmpty(string) ? null : string;    }    public static boolean isNullOrEmpty(@Nullable String string) {        return string == null || string.length() == 0;    }    public static String padStart(String string, int minLength, char padChar) {        Preconditions.checkNotNull(string);        if (string.length() >= minLength) {            return string;        } else {            StringBuilder sb = new StringBuilder(minLength);            for(int i = string.length(); i < minLength; ++i) {                sb.append(padChar);            }            sb.append(string);            return sb.toString();        }    }    public static String padEnd(String string, int minLength, char padChar) {        Preconditions.checkNotNull(string);        if (string.length() >= minLength) {            return string;        } else {            StringBuilder sb = new StringBuilder(minLength);            sb.append(string);            for(int i = string.length(); i < minLength; ++i) {                sb.append(padChar);            }            return sb.toString();        }    }    public static String repeat(String string, int count) {        Preconditions.checkNotNull(string);        if (count <= 1) {            Preconditions.checkArgument(count >= 0, "invalid count: %s", new Object[]{count});            return count == 0 ? "" : string;        } else {            int len = string.length();            long longSize = (long)len * (long)count;            int size = (int)longSize;            if ((long)size != longSize) {                throw new ArrayIndexOutOfBoundsException("Required array size too large: " + longSize);            } else {                char[] array = new char[size];                string.getChars(0, len, array, 0);                int n;                for(n = len; n < size - n; n <<= 1) {                    System.arraycopy(array, 0, array, n, n);                }                System.arraycopy(array, 0, array, n, size - n);                return new String(array);            }        }    }    public static String commonPrefix(CharSequence a, CharSequence b) {        Preconditions.checkNotNull(a);        Preconditions.checkNotNull(b);        int maxPrefixLength = Math.min(a.length(), b.length());        int p;        for(p = 0; p < maxPrefixLength && a.charAt(p) == b.charAt(p); ++p) {            ;        }        if (validSurrogatePairAt(a, p - 1) || validSurrogatePairAt(b, p - 1)) {            --p;        }        return a.subSequence(0, p).toString();    }    public static String commonSuffix(CharSequence a, CharSequence b) {        Preconditions.checkNotNull(a);        Preconditions.checkNotNull(b);        int maxSuffixLength = Math.min(a.length(), b.length());        int s;        for(s = 0; s < maxSuffixLength && a.charAt(a.length() - s - 1) == b.charAt(b.length() - s - 1); ++s) {            ;        }        if (validSurrogatePairAt(a, a.length() - s - 1) || validSurrogatePairAt(b, b.length() - s - 1)) {            --s;        }        return a.subSequence(a.length() - s, a.length()).toString();    }    @VisibleForTesting    static boolean validSurrogatePairAt(CharSequence string, int index) {        return index >= 0 && index <= string.length() - 2 && Character.isHighSurrogate(string.charAt(index)) && Character.isLowSurrogate(string.charAt(index + 1));    }}

Strings类常用功能和使用

  1. /** 
  2.  * Guava Strings工具类的使用,null和empty的判断与转化 
  3.  *  chenleixing 
  4.  */  
  5. public void testStrings(){  
  6.     Strings.isNullOrEmpty("");//返回true  
  7.     Strings.nullToEmpty(null);//""  
  8.     Strings.nullToEmpty("chen");//返回"chen"  
  9.     Strings.emptyToNull("");//返回null  
  10.     Strings.emptyToNull("chen");//返回"chen"  
  11.       
  12.     Strings.commonPrefix("aaab""aac");//"aa"否则返回""  
  13.     Strings.commonSuffix("aaac""aac");//"aac"否则返回""  
  14. }  

个人实际开发中遇到的实例:

/** * 查询商品记录 * * @param page           第几页开始 * @param pageSize       页面条数 * @param itemCategoryId 商品种类id * @return */public PageInfo showItemData(Integer page, Integer pageSize, String itemCategoryId) {    PageHelper.startPage(page, pageSize);    PageInfo pageInfo = new PageInfo(itemMapper.showAugeItem(Strings.emptyToNull(itemCategoryId)));    return pageInfo;}

由于没有在方法的传参中判断,导致前台jsp页面一直取不到值。

在option中value为""空,showAugeItem拿到的就是这个"",程序运行到底层就是

SELECT *FROMauge_item aINNER JOIN auge_item_classification b ON a.item_cid = b.c_id        AND c_id = '""'ORDER BY a.item_print_date DESC

所以一直取不到值。所以使用Guava的Strings进行emptyToNull转换。

转载于:https://my.oschina.net/inchlifc/blog/1563834

你可能感兴趣的文章