001/**   
002* @Title: Assert.java 
003* @Package net.gdface.utils 
004* @Description: TODO 
005* @author guyadong   
006* @date 2015年4月23日 上午9:48:17 
007* @version V1.0   
008*/
009package net.gdface.utils;
010
011import java.nio.Buffer;
012import java.util.Collection;
013import java.util.Map;
014
015/**断言类
016 * @author guyadong
017 *
018 */
019public class Assert {
020        private static final String getLocation(){
021                StackTraceElement stack = Thread.currentThread() .getStackTrace()[3];
022                return String.format("[%s.%s:%d]\n",stack.getClassName(),stack.getMethodName(),stack.getLineNumber());
023        }
024        public static final <T>void isTrue(boolean t,String expression){
025                if (!t)
026                        throw new IllegalArgumentException(String.format("%s:experssion '%s' must be true",getLocation(),expression));
027        }
028        public static final <T>void isTrue(boolean t,String expression,String msg){
029                if (!t)
030                        throw new IllegalArgumentException(String.format("%s:experssion '%s' must be true,%s",getLocation(),expression,msg));
031        }
032        public static final <T>void notNull(T t,String arg){
033                if (null == t)
034                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null",getLocation(),arg));             
035        }
036        public static final <T>void notNull(T t,String arg,String msg){
037                if (null == t)
038                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null,%s",getLocation(),arg,msg));              
039        }
040        public static final <T>void notEmpty(T[] t,String arg){
041                if (null == t||0==t.length)
042                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty",getLocation(),arg));            
043        }
044        public static final <T>void notNullElement(T[] t,String arg){
045                notEmpty(t,arg);
046                for(int i=0;i<t.length;i++){                    
047                        if (null == t[i]) {
048                                throw new IllegalArgumentException(String.format("%s:%s[%d] is null ",getLocation(),arg,i));
049                        }
050                }
051        }
052        
053        public static final void notEmptyElement(String[] t,String arg){
054                notEmpty(t,arg);
055                for(int i=0;i<t.length;i++){                    
056                        if (null == t[i] || 0 == t[i].length()) {
057                                throw new IllegalArgumentException(String.format("%s:%s[%d] is null or empty",getLocation(),arg,i));
058                        }
059                }
060        }
061        public static final void notEmptyElement(Buffer[] t,String arg){
062                notEmpty(t,arg);
063                for(int i=0;i<t.length;i++){                    
064                        if (null == t[i] || !t[i].hasRemaining()) {
065                                throw new IllegalArgumentException(String.format("%s:%s[%d] is null or empty",getLocation(),arg,i));
066                        }
067                }
068        }
069        public static final void notEmptyElement(Collection<Buffer> t,String arg){
070                notEmpty(t,arg);
071                for(Buffer buffer:t){                   
072                        if (null == buffer || !buffer.hasRemaining()) {
073                                throw new IllegalArgumentException(String.format("%s:%s have null or empty element",getLocation(),arg));
074                        }
075                }
076        }
077        public static final <T extends Collection<?>>void notEmpty(T t,String arg){
078                if (null == t || t.isEmpty())
079                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty",getLocation(),arg));            
080        }
081        public static final <T extends Map<?,?>>void notEmpty(T t,String arg){
082                if (null == t|| t.isEmpty())
083                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty",getLocation(),arg));            
084        }
085        public static final void notEmpty(byte[] t,String arg){
086                if (null == t||0==t.length)
087                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty",getLocation(),arg));            
088        }
089        public static final void notEmpty(String t,String arg){
090                if (null == t||0==t.length())
091                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty",getLocation(),arg));            
092        }
093
094        public static final void notEmpty(String t,String arg,String msg){
095                if (null == t||0==t.length())
096                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty,%s",getLocation(),arg,msg));             
097        }
098        public static final void notEmpty(Buffer t,String arg){
099                if (null == t || !t.hasRemaining())
100                        throw new IllegalArgumentException(String.format("%s:the argument %s must not be null or empty",getLocation(),arg));
101        }
102
103        public static final void assertValidCode(byte[] code1, byte[] code2){
104                notEmpty(code1, "code1");
105                notEmpty(code2, "code2");
106                if(code1.length!=code2.length){
107                        throw new IllegalArgumentException(String.format("%s:INVALID CODE code1(%dbytes),code2(%dbytes)",getLocation(),code1.length,code2.length));
108                }
109        }
110
111}