001 /**
002 * Copyright (c) 2010, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.base;
029
030
031 import java.io.Serializable;
032
033
034 /**
035 * {@code CounterImpl} is a simple counter and a better alternative than {@code
036 * Integer} as a new instance is created each time you want to set the value.
037 *
038 * @author nikitin
039 *
040 * @version 1.0
041 *
042 */
043 public final class CounterImpl implements Counter, Resetable, ExplicitlyCloneable,
044 Serializable {
045
046 private static final long serialVersionUID = 7831797543622859629L;
047
048 private int count;
049
050 private CounterImpl(int start) {
051 count = start;
052 }
053
054 /**
055 * A basic static factory method.
056 *
057 * @return a new instance of {@code CounterImpl} initialized to 0.
058 */
059 public static CounterImpl newInstance() {
060 return new CounterImpl(0);
061 }
062
063 /**
064 * A type-conversion static factory method.
065 *
066 * @return a new instance of {@code CounterImpl} with the given value.
067 */
068 public static CounterImpl valueOf(int start) {
069 return new CounterImpl(start);
070 }
071
072 /** @see Resetable#reset() */
073 public void reset() {
074 count = 0;
075 }
076
077 /**
078 * Creates and returns a copy of this {@code CounterImpl}.
079 *
080 * @see Object#clone()
081 */
082 public CounterImpl clone() {
083 CounterImpl clone = null;
084 try {
085 clone = (CounterImpl) super.clone();
086 clone.count = count;
087 } catch (CloneNotSupportedException e) {
088 throw new AssertionError(); // cannot happen
089 }
090 return clone;
091 }
092
093 public boolean equals(Object o) {
094 if (o instanceof CounterImpl) {
095 if (((CounterImpl) o).count == count) {
096 return true;
097 }
098 }
099 return false;
100 }
101
102 public int hashCode() {
103 return count;
104 }
105
106 /**
107 * Increment counter by 1.
108 */
109 public void increment() {
110 count++;
111 }
112
113 /**
114 * Increment counter by {@code inc}.
115 */
116 public void increment(int inc) {
117 count += inc;
118 }
119
120 /**
121 * Decrement counter by 1.
122 */
123 public void decrement() {
124 count--;
125 }
126
127 /**
128 * Decrement counter by {@code dec}.
129 */
130 public void decrement(int dec) {
131 count -= dec;
132 }
133
134 /**
135 * @return counter value.
136 */
137 public int getCount() {
138 return count;
139 }
140
141 public String toString() {
142 return "x" + count;
143 }
144 }