/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.spring.jdbc.datasource;

/**
 * 以 {@link ThreadLocal} 形式,维护数据源的key.
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see MultipleGroupReadWriteDataSource
 * @see java.lang.ThreadLocal
 * @see "org.springframework.web.context.request.RequestContextHolder"
 * @since 1.1.1
 * @since jdk 1.5
 */
public class MultipleGroupReadWriteStatusHolder{

    /** 为每一个线程维护变量的副本 . */
    private static final ThreadLocal<String> MULTIPLE_DATASOURCE_GROUP_NAME_HOLDER = new ThreadLocal<String>();

    /** Don't let anyone instantiate this class. */
    private MultipleGroupReadWriteStatusHolder(){
        //AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
        //see 《Effective Java》 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * 设置当前线程的线程局部变量的值.
     *
     * @param dataSourceName
     *            the multiple data source group name
     * @see java.lang.ThreadLocal#set(Object)
     */
    public static void setMultipleDataSourceGroupName(String dataSourceName){
        MULTIPLE_DATASOURCE_GROUP_NAME_HOLDER.set(dataSourceName);
    }

    /**
     * 返回当前线程所对应的线程局部变量.
     *
     * @return the multiple data source group name
     * @see java.lang.ThreadLocal#get()
     */
    public static String getMultipleDataSourceGroupName(){
        return MULTIPLE_DATASOURCE_GROUP_NAME_HOLDER.get();
    }

    /**
     * 将当前线程局部变量的值删除,目的是为了减少内存的占用,该方法是JDK 5.0新增的方法.<br>
     * 需要指出的是,当线程结束后,对应该线程的局部变量将自动被垃圾回收,<br>
     * 所以显式调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度
     *
     * @see java.lang.ThreadLocal#remove()
     * @since jdk1.5
     */
    public static void clearMultipleDataSourceGroupName(){
        MULTIPLE_DATASOURCE_GROUP_NAME_HOLDER.remove();
    }

}

@@ -1106,22 +1106,16 @@
 adLocal%3C
-String
 %3E();%0A%0A  

update bad code Replace the type specification in this constructor call with the diamond operator ("<>")