001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.item.file.transform;
018
019import org.springframework.util.Assert;
020
021/**
022 * A class to represent ranges. A Range can have minimum/maximum values from
023 * interval <1,Integer.MAX_VALUE-1> A Range can be unbounded at maximum
024 * side. This can be specified by passing {@link Range#UPPER_BORDER_NOT_DEFINED}} as max
025 * value or using constructor {@link #Range(int)}.
026 * 
027 * @author peter.zozom
028 */
029public class Range {
030
031        public final static int UPPER_BORDER_NOT_DEFINED = Integer.MAX_VALUE;
032        
033        final private int min;  
034        final private int max;
035        
036        public Range(int min) {
037                this(min,UPPER_BORDER_NOT_DEFINED);             
038        }
039        
040        public Range(int min, int max) {
041                checkMinMaxValues(min, max);
042                this.min = min;
043                this.max = max;
044        }
045
046        public int getMax() {           
047                return max;
048        }
049
050        public int getMin() {
051                return min;
052        }
053
054        public boolean hasMaxValue() {
055                return max != UPPER_BORDER_NOT_DEFINED;
056        }
057        
058    @Override
059        public String toString() {
060                return hasMaxValue() ? min + "-" + max : String.valueOf(min);
061        }
062        
063        private void checkMinMaxValues(int min, int max) {
064                Assert.isTrue(min>0, "Min value must be higher than zero");
065                Assert.isTrue(min<=max, "Min value should be lower or equal to max value");             
066        }
067}