001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.platform.lang;
005    
006    import graphlab.platform.attribute.AtomAttribute;
007    
008    /**
009     * represents a bounded integer: it's value only can be in the (max ,min) bound.
010     *
011     * @author azin azadi
012     */
013    //todo: bounded number
014    public class BoundedInteger implements Validator<Integer>, AtomAttribute<Integer> {
015        private int max;
016        private int min;
017    
018        /**
019         * sets the max and min to integer.maxvalue , minvalue
020         *
021         * @param value
022         */
023        public BoundedInteger(int value) {
024            max = Integer.MAX_VALUE;
025            min = Integer.MIN_VALUE;
026            this.value = value;
027        }
028    
029        public BoundedInteger(int value, int max, int min) {
030            setMax(max);
031            setMin(min);
032            this.value = value;
033        }
034    
035        public int getMax() {
036            return max;
037        }
038    
039        public void setMax(int max) {
040            if (max < min)
041                throw new RuntimeException("max value should be smaller than min value");
042            this.max = max;
043        }
044    
045        public int getMin() {
046            return min;
047        }
048    
049        public void setMin(int min) {
050            if (max < min)
051                throw new RuntimeException("max value should be smaller than min value");
052            this.min = min;
053        }
054    
055        public String toString() {
056            return "BInt" + value;
057        }
058    
059        /**
060         * returns true if value is in the bounds of this bounded integer and set the current value
061         */
062        public boolean setValue(Integer value) {
063            if (isValid(value)) {
064                this.value = value;
065                return true;
066            }
067            return false;
068        }
069    
070        public Integer getValue() {
071            return value;
072        }
073    
074        private int value;
075    
076    
077        /**
078         * is xx in bounds?
079         */
080        public boolean isValid(Integer xx) {
081            return xx < max && xx > min;
082        }
083    }