論理およびトランザクション制御用のツールクラス

8091 ワード

import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.function.Supplier;

/**
 *             
 *
 */
public class TAssert {

    /**
     * @param expectedSize    
     * @param actualSize      
     * @return
     */
    public static boolean notEquals(int expectedSize, int actualSize) {
        return actualSize != expectedSize;
    }

    /**
     * @param expectedSize    
     * @param actualSize      
     * @param message             
     */
    public static void throwsIfNotEquals(int expectedSize, int actualSize, String message) {
        if (notEquals(expectedSize, actualSize))
            throw new DataRetrievalFailureException(message);
    }

    /**
     * @param expectedSize         
     * @param actualSize           
     * @param exceptionSupplier       
     * @param 
     */
    public static <T extends RuntimeException> void throwsIfNotEquals(int expectedSize, int actualSize, Supplier<T> exceptionSupplier) {
        if (notEquals(expectedSize, actualSize))
            throw exceptionSupplier.get();
    }


    /**
     * @param expectedSize    
     * @param actualSize      
     */
    public static void throwsIfNotEquals(int expectedSize, int actualSize) {
        if (notEquals(expectedSize, actualSize))
            throw new IncorrectResultSizeDataAccessException(expectedSize, expectedSize);
    }

    /**
     *             ,    ,   true,    false
     *
     * @param expectedSize    
     * @param actualSize      
     * @return
     */
    public static boolean rollBackIfNotEquals(int expectedSize, int actualSize) {
        if (notEquals(expectedSize, actualSize)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return true;
        }
        return false;
    }
}