1 package com.bonevich.erj.model;
2
3 import com.bonevich.erj.ErjConstants;
4 import com.bonevich.util.dependency.Dynamic;
5 import com.bonevich.java.util.ImmutableIterator;
6
7 import java.util.*;
8
9 /***
10 * A class that represents a SQL schema, which is composed of
11 * relations and domains.
12 *
13 * @see KeyConstraint
14 * @see Relation
15 * @author Jeffrey D. Bonevich <bonevich@telocity.com>
16 */
17 public final class Schema extends ModelElement
18 {
19 //////////////////////////////////////////////////////////
20 // Constants
21 public static final String NEW_SCHEMA_STR = "untitled";
22
23 //////////////////////////////////////////////////////////
24 // Attributes
25 {
26 _name = NEW_SCHEMA_STR;
27 _identifier = NEW_SCHEMA_STR;
28 _description = ErjConstants.EMPTY_STR;
29 }
30
31 private int _relationCount = 0;
32 private int _domainCount = 0;
33
34 //////////////////////////////////////////////////////////
35 // Associations
36 private List _relations = new LinkedList();
37 private List _domains = new LinkedList();
38 private List _keys = new LinkedList();
39
40 /////////////////////////////////////////////////////////
41 // Dynamic Sentries
42 private Dynamic _dyn_relations = new Dynamic();
43 private Dynamic _dyn_keys = new Dynamic();
44 private Dynamic _dyn_domains = new Dynamic();
45
46 //////////////////////////////////////////////////////////
47 // Constructors
48 public Schema()
49 {
50 }
51
52 //////////////////////////////////////////////////////////
53 // Operations
54 public Relation createRelation()
55 {
56 _dyn_relations.onSet();
57 ++_relationCount;
58 Relation relation = new Relation(this);
59 _relations.add(relation);
60 return relation;
61 }
62 public void deleteRelation(Relation relation)
63 {
64 if (_relations.remove(relation))
65 {
66 _dyn_relations.onSet();
67 relation.dispose();
68 }
69 }
70 public boolean containsRelation(Relation relation)
71 {
72 if (_relations.contains(relation))
73 {
74 _dyn_relations.onGet();
75 return true;
76 }
77 return false;
78 }
79 public Iterator getRelationIterator()
80 {
81 _dyn_relations.onGet();
82 return new ImmutableIterator(_relations.iterator());
83 }
84 public int getRelationCount()
85 {
86 return _relationCount;
87 }
88
89 public Domain createDomain()
90 {
91 _dyn_domains.onSet();
92 ++_domainCount;
93 Domain domain = new Domain(this);
94 _domains.add(domain);
95 return domain;
96 }
97 public void deleteDomain(Domain domain)
98 {
99 if (_domains.remove(domain))
100 {
101 _dyn_domains.onSet();
102 domain.dispose();
103 }
104 }
105 public boolean containsDomain(Domain domain)
106 {
107 if (_domains.contains(domain))
108 {
109 _dyn_domains.onGet();
110 return true;
111 }
112 return false;
113 }
114 public Iterator getDomainIterator()
115 {
116 _dyn_domains.onGet();
117 return new ImmutableIterator(_domains.iterator());
118 }
119 public int getDomainCount()
120 {
121 return _domainCount;
122 }
123
124 public void addKey(ForeignKey key)
125 {
126 if (!_keys.contains(key))
127 {
128 _dyn_keys.onSet();
129 _keys.add(key);
130 }
131 }
132 public void removeKey(ForeignKey key)
133 {
134 if (_keys.remove(key))
135 {
136 _dyn_keys.onSet();
137 // Since Schema do not really own ForeignKeys,
138 // we do not dispose of them
139 }
140 }
141 public boolean containsKey(ForeignKey key)
142 {
143 if (_keys.contains(key))
144 {
145 _dyn_keys.onGet();
146 return true;
147 }
148 return false;
149 }
150 public Iterator getKeyIterator()
151 {
152 _dyn_keys.onGet();
153 return new ImmutableIterator(_keys.iterator());
154 }
155
156 public void dispose()
157 {
158 Iterator domains = getDomainIterator();
159 while (domains.hasNext())
160 {
161 ((Domain) domains.next()).dispose();
162 }
163 Iterator relations = getRelationIterator();
164 while (relations.hasNext())
165 {
166 ((Relation) relations.next()).dispose();
167 }
168 }
169
170 public void accept(ModelElementVisitor visitor)
171 {
172 visitor.visitSchema(this);
173 }
174
175 } /* end class Schema */
This page was automatically generated by Maven