001: /*
002: * $Id: CounterFile.java,v 1.4 2004/08/12 01:24:50 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.util;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.ObjectInputStream;
048: import java.io.ObjectOutputStream;
049:
050: /**
051: * A persistent counter.
052: *
053: * @version $Revision: 1.4 $ $Date: 2004/08/12 01:24:50 $
054: * @author Rodney Waldhoff
055: */
056: class CounterFile {
057: public CounterFile() {
058: }
059:
060: private CounterFile(int value) {
061: setValue(value);
062: }
063:
064: /** Return the current counter value, then increment it. */
065: public int increment() {
066: return _value++;
067: }
068:
069: /**
070: * Returns the current counter value.
071: *
072: * @return current counter value
073: */
074: public int current() {
075: return _value;
076: }
077:
078: public void save(File file) throws IOException {
079: ObjectOutputStream out = null;
080: try {
081: out = new ObjectOutputStream(new FileOutputStream(file));
082: out.writeInt(_value);
083: } finally {
084: try {
085: out.close();
086: } catch (Exception e) {
087: }
088: }
089: }
090:
091: public static CounterFile load(File file) throws IOException {
092: ObjectInputStream in = null;
093: try {
094: in = new ObjectInputStream(new FileInputStream(file));
095: int value = in.readInt();
096: return new CounterFile(value);
097: } finally {
098: try {
099: in.close();
100: } catch (Exception e) {
101: }
102: }
103: }
104:
105: private void setValue(int value) {
106: _value = value;
107: }
108:
109: private int _value = 0;
110: }
|