001    package net.sourceforge.mp3dings.utils;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: Checksum.java,v 1.2 2006/11/24 13:38:17 ringler Exp $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.text.DecimalFormat;
023    import java.text.NumberFormat;
024    
025    /** A simple counter for integers.
026     * @author Moritz Ringler
027     * @version $Revision: 1.2 $ ($Date: 2006/11/24 13:38:17 $)
028     */
029    public class Counter extends Bean {
030        private int value = 1;
031        private int increment = 1;
032        private NumberFormat format = new DecimalFormat("#00");
033    
034        public Counter(){
035            //default constructor
036        }
037    
038        public Counter(int start, int increment){
039            value = start;
040            this.increment = increment;
041        }
042    
043        /**
044         * Returns the value of value.
045         */
046        public int getValue()
047        {
048            return value;
049        }
050    
051        /**
052         * Returns the value of value.
053         */
054        public int getNextValue()
055        {
056            int oldval = value;
057            value += increment;
058            return oldval;
059        }
060    
061        /**
062         * Sets the value of value.
063         * @param value The value to assign value.
064         */
065        public void setValue(int value)
066        {
067            this.value = value;
068        }
069    
070        /**
071         * Returns the value of increment.
072         */
073        public int getIncrement()
074        {
075            return increment;
076        }
077    
078        /**
079         * Sets the value of increment.
080         * @param increment The value to assign increment.
081         */
082        public void setIncrement(int increment)
083        {
084            this.increment = increment;
085        }
086    
087        /**
088         * Returns the value of format.
089         */
090        public NumberFormat getFormat()
091        {
092            return format;
093        }
094    
095        /**
096         * Sets the value of format.
097         * @param format The value to assign format.
098         */
099        public void setFormat(NumberFormat format)
100        {
101            this.format = format;
102        }
103    
104        public String getNextFormattedValue(){
105            return format.format(getNextValue());
106        }
107    
108        /** @throws IllegalArgumentException if pattern is not a valid decimal format pattern.**/
109        public void setDecimalFormat(String pattern){
110            if(pattern != null){
111                DecimalFormat dformat = new DecimalFormat(pattern);
112                dformat.setGroupingUsed(false);
113                dformat.setDecimalSeparatorAlwaysShown(false);
114                format = dformat;
115            }
116        }
117    
118        public String getDecimalFormat(){
119            String result = null;
120            if(format instanceof DecimalFormat){
121                result = ((DecimalFormat) format).toPattern();
122            }
123            return result;
124        }
125    }