001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.increment;
016    
017    import com.liferay.portal.kernel.concurrent.IncreasableEntry;
018    import com.liferay.portal.kernel.increment.Increment;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.util.Arrays;
023    
024    import org.aopalliance.intercept.MethodInvocation;
025    
026    /**
027     * @author Zsolt Berentey
028     */
029    public class BufferedIncreasableEntry<K, T>
030            extends IncreasableEntry<K, Increment<T>> {
031    
032            public BufferedIncreasableEntry(
033                    MethodInvocation methodInvocation, K key, Increment<T> value) {
034    
035                    super(key, value);
036    
037                    _methodInvocation = methodInvocation;
038            }
039    
040            @Override
041            public Increment<T> doIncrease(
042                    Increment<T> originalValue, Increment<T> deltaValue) {
043    
044                    return originalValue.increaseForNew(deltaValue.getValue());
045            }
046    
047            public void proceed() throws Throwable {
048                    Object[] arguments = _methodInvocation.getArguments();
049    
050                    arguments[arguments.length - 1] = getValue().getValue();
051    
052                    _methodInvocation.proceed();
053            }
054    
055            @Override
056            public String toString() {
057                    StringBundler sb = new StringBundler();
058    
059                    sb.append(_methodInvocation.toString());
060                    sb.append(StringPool.OPEN_PARENTHESIS);
061                    sb.append(Arrays.toString(_methodInvocation.getArguments()));
062                    sb.append(StringPool.CLOSE_PARENTHESIS);
063    
064                    return sb.toString();
065            }
066    
067            private MethodInvocation _methodInvocation;
068    
069    }