001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.increment;
016    
017    /**
018     * @author Shuyang Zhou
019     */
020    public abstract class OverrideIncrement<T extends Comparable<T>>
021            implements Increment<T> {
022    
023            public OverrideIncrement(T value) {
024                    this.value = value;
025            }
026    
027            @Override
028            public void decrease(T delta) {
029                    if (value.compareTo(delta) > 0) {
030                            value = delta;
031                    }
032            }
033    
034            @Override
035            public OverrideIncrement<T> decreaseForNew(T delta) {
036                    if (value.compareTo(delta) < 0) {
037                            delta = value;
038                    }
039    
040                    return createOverrideIncrement(delta);
041            }
042    
043            @Override
044            public T getValue() {
045                    return value;
046            }
047    
048            @Override
049            public void increase(T delta) {
050                    if (value.compareTo(delta) < 0) {
051                            value = delta;
052                    }
053            }
054    
055            @Override
056            public OverrideIncrement<T> increaseForNew(T delta) {
057                    if (value.compareTo(delta) > 0) {
058                            delta = value;
059                    }
060    
061                    return createOverrideIncrement(delta);
062            }
063    
064            @Override
065            public void setValue(T value) {
066                    this.value = value;
067            }
068    
069            protected abstract OverrideIncrement<T> createOverrideIncrement(T value);
070    
071            protected T value;
072    
073    }