All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rholder.spring.transaction.TransactionalResourceHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2005-2010 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 */
package com.github.rholder.spring.transaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * Helper class that will look up or create transactional resources.
 * This shortcuts some of the "if not existing, then create" code.
 * 
 * @author Derek Hulley
 * @author Ray Holder, derived work modifications
 * @since 3.2
 */
public abstract class TransactionalResourceHelper
{
    /**
     * Support method to retrieve or create and bind a HashMap to the current transaction.
     * 
     * @param            the map key type
     * @param            the map value type
     * @param resourceKey   the key under which the resource will be stored
     * @return              Returns an previously-bound Map or else a newly-bound HashMap
     */
    public static final  Map getMap(Object resourceKey)
    {
        Map map = TransactionBindingSupport.>getResource(resourceKey);
        if (map == null)
        {
            map = new HashMap(29);
            TransactionBindingSupport.bindResource(resourceKey, map);
        }
        return map;
    }

    /**
     * Support method to retrieve or create and bind a HashSet to the current transaction.
     * 
     * @param            the set value type
     * @param resourceKey   the key under which the resource will be stored
     * @return              Returns an previously-bound Set or else a newly-bound HashSet
     */
    public static final  Set getSet(Object resourceKey)
    {
        Set set = TransactionBindingSupport.>getResource(resourceKey);
        if (set == null)
        {
            set = new HashSet(29);
            TransactionBindingSupport.bindResource(resourceKey, set);
        }
        return set;
    }

    /**
     * Support method to retrieve or create and bind a TreeSet to the current transaction.
     * 
     * @param            the set value type
     * @param resourceKey   the key under which the resource will be stored
     * @return              Returns an previously-bound TreeSet or else a newly-bound TreeSet
     */
    public static final  TreeSet getTreeSet(Object resourceKey)
    {
        TreeSet set = TransactionBindingSupport.>getResource(resourceKey);
        if (set == null)
        {
            set = new TreeSet();
            TransactionBindingSupport.bindResource(resourceKey, set);
        }
        return set;
    }

    /**
     * Support method to retrieve or create and bind a ArrayList to the current transaction.
     * 
     * @param            the list value type
     * @param resourceKey   the key under which the resource will be stored
     * @return              Returns an previously-bound List or else a newly-bound ArrayList
     */
    public static final  List getList(Object resourceKey)
    {
        List list = TransactionBindingSupport.>getResource(resourceKey);
        if (list == null)
        {
            list = new ArrayList(29);
            TransactionBindingSupport.bindResource(resourceKey, list);
        }
        return list;
    }
    
    /**
     * Support method to set a boolean (true) value in the current transaction.
     * @param resourceKey   the key under which the resource will be stored
     * @return true - the value of resourceKey, was set to true, false - the value was already true
     */
    public static final boolean setBoolean(Object resourceKey)
    {
        Boolean value = TransactionBindingSupport.getResource(resourceKey);
        if(value == null)
        {
            TransactionBindingSupport.bindResource(resourceKey, Boolean.TRUE);
            return true;
        }
       
        return false;
    }
    
    /**
     * Support method to reset (make false) a boolean value in the current transaction.
     * @param resourceKey   the key under which the resource is stored.
     */
    public static final void resetBoolean(Object resourceKey)
    {
        Boolean value = TransactionBindingSupport.getResource(resourceKey);
        if(value == null)
        {
            TransactionBindingSupport.unbindResource(resourceKey);
        }
    }
    
    /**
     * Is there a boolean value in the current transaction
     * @param resourceKey   the key under which the resource will be stored
     * @return true - thre is, false no.
     */
    public static final boolean testBoolean(Object resourceKey)
    {
        Boolean value = TransactionBindingSupport.getResource(resourceKey);
        if(value == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy