1/*
2 * Copyright 2001-2009 OFFIS, Tammo Freese
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.internal;
17
18import java.io.Serializable;
19
20public class Range implements Serializable {
21
22    private static final long serialVersionUID = -6743402320315331536L;
23
24    private final int minimum;
25
26    private final int maximum;
27
28    public Range(int count) {
29        this(count, count);
30    }
31
32    public Range(int minimum, int maximum) {
33        if (!(minimum <= maximum)) {
34            throw new RuntimeExceptionWrapper(new IllegalArgumentException(
35                    "minimum must be <= maximum"));
36        }
37
38        if (!(minimum >= 0)) {
39            throw new RuntimeExceptionWrapper(new IllegalArgumentException(
40                    "minimum must be >= 0"));
41        }
42
43        if (!(maximum >= 1)) {
44            throw new RuntimeExceptionWrapper(new IllegalArgumentException(
45                    "maximum must be >= 1"));
46        }
47        this.minimum = minimum;
48        this.maximum = maximum;
49    }
50
51    public boolean hasFixedCount() {
52        return minimum == maximum;
53    }
54
55    public int getMaximum() {
56        return maximum;
57    }
58
59    public int getMinimum() {
60        return minimum;
61    }
62
63    @Override
64    public String toString() {
65        if (hasFixedCount()) {
66            return "" + minimum;
67        } else if (hasOpenCount()) {
68            return "at least " + minimum;
69        } else {
70            return "between " + minimum + " and " + maximum;
71        }
72    }
73
74    public String expectedCount() {
75        return "expected: " + this.toString();
76    }
77
78    public boolean contains(int count) {
79        return minimum <= count && count <= maximum;
80    }
81
82    public boolean hasOpenCount() {
83        return maximum == Integer.MAX_VALUE;
84    }
85}
86