001package gu.sql2java;
002
003import java.util.Map;
004import java.util.concurrent.ConcurrentMap;
005import java.util.concurrent.TimeUnit;
006
007/**
008 * constant declare
009 * @author guyadong
010 */
011public interface Constant {    
012    public static final int STATE_BIT_NUM = 32;
013    public static final int STATE_BIT_SHIFT = 5;
014    public static final int STATE_BIT_MASK = 0x1f;
015
016    public static final String SQL_LIKE_WILDCARD = "%";
017    /** set =QUERY for loadUsingTemplate */
018    public static final int SEARCH_EXACT = 0;
019    /** set %QUERY% for loadLikeTemplate */
020    public static final int SEARCH_LIKE = 1;
021    /** set %QUERY for loadLikeTemplate */
022    public static final int SEARCH_STARTING_LIKE = 2;
023    /** set QUERY% for loadLikeTemplate */
024    public static final int SEARCH_ENDING_LIKE = 3;
025
026    /** JDBC property name definition */
027    public static enum JdbcProperty{
028        /** debug status */DEBUG("isDebug"),
029        /** JDBC driver class name */JDBC_DRIVER("jdbc.driver"),
030        /** JDBC connection url */JDBC_URL("jdbc.url"),
031        /** JDBC user name */JDBC_USERNAME("jdbc.username"),
032        /** JDBC password */JDBC_PASSWORD("jdbc.password"),
033        /** Retrieval type of auto generated key:auto,before,after */GENERATEDKEY_RETRIEVE("generatedkey.retrieve"),
034        /** Retrieval statement of auto generated key */GENERATEDKEY_STATEMENT("generatedkey.statement"),
035        /** data source type, c3p0 supported only now */DATASOURCE("datasource"),
036        /** c3p0 property */C3P0_MINPOOLSIZE("c3p0.minPoolSize"),
037        /** c3p0 property */C3P0_MAXPOOLSIZE("c3p0.maxPoolSize"),
038        /** c3p0 property */C3P0_MAXIDLETIME("c3p0.maxIdleTime"),
039        /** c3p0 property */C3P0_IDLECONNECTIONTESTPERIOD("c3p0.idleConnectionTestPeriod");
040        /** JDBC property name */
041        public final String key;
042        
043        JdbcProperty(String key){
044            this.key = key;
045        }
046        /** return {@link #key} with {@code prefix} */
047        public String withPrefix(String prefix){
048            return new StringBuffer().append(prefix).append(key).toString();
049        }
050        /** 
051         * cast key to {@link JdbcProperty} instance if {@link #key} field equal the argument {@code key},
052         * otherwise return {@code null} 
053         */
054        public static final JdbcProperty fromKey(String key){
055            for(JdbcProperty p: values()){
056                if(p.key.equals(key)){
057                    return p;
058                }
059            }
060            return null;
061        }
062
063    }
064
065        /** 
066         * Update strategy for cache
067         */
068        enum UpdateStrategy{        
069            /** update no matter whether key exists */
070            always,
071            /** update only if key exists */
072            replace,
073            /** remove key  */
074            remove,
075            /** reload data if key exists, need {@code entry } implement the reload method */
076            refresh;
077            /**
078             * update {@code entry} to {@code map},if {code getValue()} return {@code null},remove key.
079             * @param map
080             * @param entry
081             * @return value of entry
082             */
083            <K,V> V update(ConcurrentMap<K,V> map,Map.Entry<K,V>entry){
084                if(null == map || null == entry ){
085                    return null;
086                }
087                K key = entry.getKey();
088                if( null == key){
089                    return null;
090                }
091                V value = entry.getValue();
092                if(null == value){
093                    map.remove(key);
094                    return null;
095                }
096                switch(this){
097                case always:
098                    map.put(key, value);
099                    break;
100                case replace:
101                    map.replace(key, value);
102                    break;
103                case remove:
104                    map.remove(key);
105                    break;
106                case refresh:
107                    map.put(key, value);
108                default:
109                    break;
110                }
111                return value;
112            }
113        }
114
115        UpdateStrategy DEFAULT_STRATEGY = UpdateStrategy.always;
116        long DEFAULT_CACHE_MAXIMUMSIZE = 10000;
117        long DEFAULT_DURATION = 10;
118        TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MINUTES;
119        public static final String RETRIEVE_SUFFIX="generatedkey.retrieve";
120        public static final String STATEMENT_SUFFIX="generatedkey.statement";
121}