001: /*
002: * jGnash, a personal finance application
003: * Copyright (C) 2001-2008 Craig Cavanaugh
004: *
005: * This program is free software: you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation, either version 3 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program. If not, see <http://www.gnu.org/licenses/>.
017: */
018: package jgnash.util;
019:
020: import java.math.BigDecimal;
021:
022: /**
023: * A list class to cache BigDecimals at specified indexes. BigDecimalCache
024: * operates under the assumption that it may need to expand the size of the list
025: * if an index larger than the current capacity is being set. A returned value
026: * of null on a get(index) operation means that the value at that index has not
027: * been set.
028: *
029: * @author Craig Cavanaugh
030: *
031: * $Id: BigDecimalCache.java 197 2008-01-01 08:13:35Z ccavanaugh $
032: */
033: public class BigDecimalCache {
034: private BigDecimal cache[] = new BigDecimal[0];
035:
036: public BigDecimalCache(int capacity) {
037: ensureCapacity(capacity);
038: }
039:
040: /**
041: * Trims the capacity of this <tt>BigDecimalCache</tt> instance to the
042: * specified size.
043: *
044: * @param size the new capacity of the cache
045: */
046: public void trimToSize(int size) {
047: int oldCapacity = cache.length;
048: if (size < oldCapacity) {
049: BigDecimal oldData[] = cache;
050: cache = new BigDecimal[size];
051: System.arraycopy(oldData, 0, cache, 0, size);
052: }
053: }
054:
055: /**
056: * Increases the capacity of this <tt>BigDecimalCache</tt> instance, if
057: * necessary, to ensure that it can hold at least the number of BigDecimals
058: * specified by the minimum capacity argument.
059: *
060: * @param minCapacity the desired minimum capacity.
061: */
062: public void ensureCapacity(int minCapacity) {
063: if (minCapacity > cache.length) {
064: int oldCapacity = cache.length;
065: BigDecimal oldCache[] = cache;
066: int newCapacity = oldCapacity + 5;
067: if (newCapacity < minCapacity) {
068: newCapacity = minCapacity;
069: }
070: cache = new BigDecimal[newCapacity];
071: System.arraycopy(oldCache, 0, cache, 0, oldCapacity);
072: }
073: }
074:
075: /**
076: * Returns the capacity of the cache
077: *
078: * @return the capacity of the cache.
079: */
080: public int capacity() {
081: return cache.length;
082: }
083:
084: /**
085: * Returns the <tt>BigDecimal</tt> at the specified position in the cache
086: *
087: * @param index index of the <tt>BigDecimal</tt> to return.
088: * @return the <tt>BigDecimal</tt> at the specified position in this list.
089: */
090: public final BigDecimal get(int index) {
091: return cache[index];
092: }
093:
094: /**
095: * Sets the <tt>BigDecimal</tt> at the specified position in this cache.
096: *
097: * @param index index of the <tt>BigDecimal</tt> to replace.
098: * @param element <tt>BigDecimal</tt> to be stored at the specified position.
099: */
100: public void set(int index, BigDecimal element) {
101: ensureCapacity(index + 1); // capacity is one more than index
102: cache[index] = element;
103: }
104:
105: /**
106: * Clear all of the BigDecimals in the cache.
107: */
108: public void clear() {
109: cache = new BigDecimal[cache.length];
110: }
111:
112: /**
113: * Clears the cache all of <tt>BigDecimals</tt> starting at fromIndex.
114: *
115: * @param fromIndex index of first BigDecimal to be cleared.
116: */
117: public void clear(int fromIndex) {
118: if (fromIndex >= 0 && fromIndex < cache.length - 1) {
119: int toIndex = cache.length;
120: for (int i = fromIndex; i < toIndex; i++) {
121: cache[i] = null;
122: }
123: } else {
124: ensureCapacity(fromIndex + 1);
125: }
126: }
127: }
|