01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //
09: //
10:
11: package de.uka.ilkd.key.casetool;
12:
13: import java.util.StringTokenizer;
14:
15: public class Multiplicity {
16:
17: public static int INFINITE = -10;
18:
19: public static Multiplicity STANDARD = new Multiplicity(1, 1);
20: public static Multiplicity ZERO_TO_INF = new Multiplicity(0,
21: INFINITE);
22: public static Multiplicity ONE_TO_INF = new Multiplicity(1,
23: INFINITE);
24: public static Multiplicity ZERO_OR_ONE = new Multiplicity(0, 1);
25:
26: protected int min, max;
27:
28: public Multiplicity(int min, int max) {
29: if (min < 0)
30: throw new IllegalArgumentException(
31: "minimum multiplicity must be at least 0, is "
32: + min);
33: if (max == 0)
34: throw new IllegalArgumentException(
35: "maximum multiplicity must not be 0");
36: if (!(min <= max || max == INFINITE))
37: throw new IllegalArgumentException(
38: "minimum multiplicity ("
39: + min
40: + ") must not be greater than maximum multiplicity ("
41: + max + ")");
42: this .min = min;
43: this .max = max;
44: }
45:
46: public int getMax() {
47: return max;
48: }
49:
50: public int getMin() {
51: return min;
52: }
53:
54: public static Multiplicity getMultiplicityFromString(String s) {
55: if (s.equals("0..*") || s.equals("*")) {
56: return Multiplicity.ZERO_TO_INF;
57: } else if (s.equals("1..*")) {
58: return Multiplicity.ONE_TO_INF;
59: } else if (s.equals("1")) {
60: return Multiplicity.STANDARD;
61: } else if (s.equals("0..1")) {
62: return Multiplicity.ZERO_OR_ONE;
63: } else {
64: StringTokenizer st = new StringTokenizer(s, "..");
65:
66: if (st.countTokens() > 0 || st.countTokens() <= 2) {
67: int[] boundaries = new int[st.countTokens()];
68: int count = 0;
69: while (st.hasMoreTokens()) {
70: int nr;
71: try {
72: nr = Integer.parseInt(st.nextToken());
73: } catch (NumberFormatException nfe) {
74: //not a number
75: return null;
76: }
77: boundaries[count] = nr;
78: count++;
79: }
80: final int min = boundaries[0];
81: final int max = count > 1 ? boundaries[1]
82: : boundaries[0];
83: return new Multiplicity(min, max);
84: }
85: }
86: return null;
87: }
88:
89: public String toString() {
90: if (max == -10)
91: return "" + min + "..*";
92: else
93: return "" + min + ".." + max;
94: }
95:
96: }
|