001/*
002        $Id: IntegerParameterType.java 3207 2009-04-09 06:48:11Z gregory $
003
004        Copyright (C) 2006 Gregory Vincic, Olle Mansson
005        Copyright (C) 2007 Gregory Vincic
006
007        This file is part of Proteios.
008        Available at http://www.proteios.org/
009
010        Proteios is free software; you can redistribute it and/or modify it
011        under the terms of the GNU General Public License as published by
012        the Free Software Foundation; either version 2 of the License, or
013        (at your option) any later version.
014
015        Proteios is distributed in the hope that it will be useful, but
016        WITHOUT ANY WARRANTY; without even the implied warranty of
017        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018        General Public License for more details.
019
020        You should have received a copy of the GNU General Public License
021        along with this program; if not, write to the Free Software
022        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
023        02111-1307, USA.
024*/
025package org.proteios.core;
026
027import org.proteios.core.data.IntegerParameterValueData;
028import java.util.List;
029
030/**
031        This class represent a parameter type that is an integer.
032        This type defines a {@link #getLowerLimit() lower limit}
033        and an {@link #getUpperLimit upper limit} for the range of
034        allowed values.
035
036        @author Samuel, Nicklas
037        @version 2.0
038        @base.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $
039*/
040public class IntegerParameterType
041        extends ParameterType<Integer>
042{
043        /**
044                The lower limit of the parameter.
045        */
046        private final Integer lowerLimit;
047
048        /**
049                The upper limit of the parameter.
050        */
051        private final Integer upperLimit;
052
053
054        /**
055                Create a new integer parameter type, without any limits and allowing null
056                values.
057        */
058        public IntegerParameterType()
059        {
060                this(null, null, null, false);
061        }
062        
063        /**
064                Create a new integer parameter type.
065                @param lowerLimit The lowest allowed value, or null to have no limit
066                @param upperLimit The highest allowed value, or null to have no limit
067                @param notNull FALSE if nulls values are allowed, TRUE otherwise
068        */
069        public IntegerParameterType(Integer lowerLimit, Integer upperLimit, Integer defaultValue, boolean notNull)
070        {
071                super(Integer.class, defaultValue, notNull);
072                this.lowerLimit = lowerLimit;
073                this.upperLimit = upperLimit;
074        }
075
076        public IntegerParameterType(Integer lowerLimit, Integer upperLimit, Integer defaultValue, boolean notNull, 
077                int multiplicity, int width, int height, List<Integer> items)
078        {
079                super(Integer.class, defaultValue, notNull, multiplicity, width, height, items);
080                this.lowerLimit = lowerLimit;
081                this.upperLimit = upperLimit;
082        }
083        
084        /*
085                From the Object class
086                -------------------------------------------
087        */
088        @Override
089        public String toString()
090        {
091                return "IntegerParameterType: Lower " + lowerLimit + ", Upper " + upperLimit;
092        }
093        // -------------------------------------------
094        
095        /*
096                From the ParameterType class
097                -------------------------------------------
098        */
099        /**
100                Checks if the value is within the range given by the upper and lower 
101                limits if those are given.
102                @param value The value to test
103                @throws InvalidDataException If the value is outside the range of allowed values
104        */
105        @Override
106        void validateValue(String name, Integer value)
107                throws InvalidDataException
108        {
109                if (((lowerLimit != null) && (value.compareTo(lowerLimit) < 0 ))
110                        ||
111                        ((upperLimit != null) && (value.compareTo(upperLimit) > 0 )))
112                {
113                        if (lowerLimit != null && upperLimit != null)
114                        {
115                                throw new NumberOutOfRangeException(name, value, lowerLimit, upperLimit);
116                        }
117                        else if (lowerLimit != null)
118                        {
119                                throw new NumberOutOfRangeException(name, value, lowerLimit, false);
120                        }
121                        else if (upperLimit != null)
122                        {
123                                throw new NumberOutOfRangeException(name, value, upperLimit, true);
124                        }
125
126                }
127        }
128        /**
129                Create a new {@link IntegerParameterValueData} object.
130        */
131        @Override
132        IntegerParameterValueData newParameterValueData()
133        {
134                return new IntegerParameterValueData();
135        }
136        // -------------------------------------------
137
138        /**
139                Get the lowest valid value of the parameter.
140                @return Lowest valid value or null if none is set.
141        */
142        public Integer getLowerLimit()
143        {
144                return lowerLimit;
145        }
146        
147        /**
148                Get the highest valid value of the parameter.
149                @return Highest valid value or null if none is set.
150        */
151        public Integer getUpperLimit()
152        {
153                return upperLimit;
154        }
155}