1
2
3
4 package net.sf.bddbddb;
5
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.math.BigInteger;
9 import jwutil.collections.IndexMap;
10
11 /***
12 * A Domain represents a domain in bddbddb.
13 * Domain objects are globally unique: There is only one Domain object for each
14 * domain in the system.
15 *
16 * @author jwhaley
17 * @version $Id: Domain.java 513 2005-04-18 20:30:57Z joewhaley $
18 */
19 public class Domain {
20 /***
21 * Name of domain.
22 */
23 protected String name;
24
25 /***
26 * Number of elements in domain.
27 */
28 protected BigInteger size;
29
30 /***
31 * Optional map from element numbers to string representations.
32 */
33 protected IndexMap map;
34
35 /***
36 * Construct a new domain.
37 * This is not to be called externally.
38 *
39 * @param name name of domain
40 * @param size size of domain
41 */
42 Domain(String name, BigInteger size) {
43 super();
44 this.name = name;
45 this.size = size;
46 }
47
48 /***
49 * Load the string map for this domain.
50 *
51 * @param in input stream of this map
52 * @throws IOException
53 */
54 public void loadMap(BufferedReader in) throws IOException {
55
56 map = IndexMap.loadStringMap(name, in);
57 }
58
59
60
61
62 public String toString() {
63 return name;
64 }
65
66 /***
67 * Returns the size of this domain.
68 *
69 * @return size of domain
70 */
71 public BigInteger getSize() {
72 return size;
73 }
74
75 /***
76 * Sets the size of this domain.
77 *
78 * @param size new size
79 */
80 public void setSize(BigInteger size) {
81 this.size = size;
82 }
83
84 /***
85 * Returns the string representation of the given element in this domain.
86 *
87 * @param v element number
88 * @return string representation
89 */
90 public String toString(BigInteger v) {
91 int val = v.intValue();
92 if (map == null || val < 0 || val >= map.size()) return Integer.toString(val);
93 else return map.get(val).toString();
94 }
95
96 /***
97 * Return the map for this domain if it exists, null otherwise.
98 *
99 * @return map for this domain, or null
100 */
101 public IndexMap getMap() {
102 return map;
103 }
104
105 /***
106 * Returns the index of the given named constant in this domain.
107 * If it doesn't exist, output a warning message and add it to the domain,
108 * giving it a new index.
109 *
110 * @param constant named constant to get
111 * @return index
112 */
113 public int namedConstant(String constant) {
114 if (false && map == null) throw new IllegalArgumentException("No constant map for Domain " + name + " in which to look up constant " + constant);
115 if (map == null) map = new IndexMap(name);
116 if (!map.contains(constant)) System.err.println("Warning: Constant " + constant + " not found in map for relation " + name);
117 return map.get(constant);
118 }
119 }