001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util; 017 018public class Counter { 019 020 int count; 021 022 public Counter() { 023 this(0); 024 } 025 026 public Counter(int startValue) { 027 this.count = startValue; 028 } 029 030 public synchronized int increment() { 031 return increment(1); 032 } 033 034 public synchronized int increment(int amount) { 035 Assert.isTrue(amount > 0, "amount must be > 0"); 036 Assert.isTrue(count <= Integer.MAX_VALUE - amount, "max value exceeded"); 037 return count += amount; 038 } 039 040 public synchronized int decrement() { 041 return decrement(1); 042 } 043 044 public synchronized int decrement(int amount) { 045 Assert.isTrue(amount > 0, "amount must be > 0"); 046 Assert.isTrue(count >= Integer.MIN_VALUE + amount, "min value exceeded"); 047 return count -= amount; 048 } 049 050 public synchronized int getValue() { 051 return count; 052 } 053}