Debian Codegraph @

docs/collections/tutorial/entitieswithcollections.html

raw
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Using Entities with Collections</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Collections Tutorial" />
10 <link rel="up" href="Entity.html" title="Chapter 4.  Using Entity Classes" />
11 <link rel="prev" href="collectionswithentities.html" title="Creating Collections with Entity Bindings" />
12 <link rel="next" href="Tuple.html" title="Chapter 5.  Using Tuples" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">
22 Using Entities with Collections
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="collectionswithentities.html">Prev</a> </td>
27 <th width="60%" align="center">Chapter 4. 
28 Using Entity Classes
29 </th>
30 <td width="20%" align="right"> <a accesskey="n" href="Tuple.html">Next</a></td>
31 </tr>
32 </table>
33 <hr />
34 </div>
35 <div class="sect1" lang="en" xml:lang="en">
36 <div class="titlepage">
37 <div>
38 <div>
39 <h2 class="title" style="clear: both"><a id="entitieswithcollections"></a>
40 Using Entities with Collections
41 </h2>
42 </div>
43 </div>
44 </div>
45 <p>
46 In this example entity objects, rather than key and value
47 objects, are used for adding and enumerating the records in a
48 collection. Because fewer classes and objects are involved, adding
49 and enumerating is done more conveniently and more simply than in
50 the prior examples.
51 </p>
52 <p>
53 For adding and iterating entities, the collection of entities
54 returned by
55 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#values()" target="_top">Map.values</a>
56
57 is used. In general, when using an entity binding, all Java
58 collection methods that are passed or returned a value object will
59 be passed or returned an entity object instead.
60 </p>
61 <p>
62 The <code class="classname">Sample</code> class has been changed in this example to add
63 objects using the
64 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Set.html#add(E)" target="_top">Set.add</a>
65
66 method rather than the
67 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#put(K, V)" target="_top">Map.put</a>
68
69 method that was used in the prior examples. Entity objects are
70 constructed and passed to
71 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Set.html#add(E)" target="_top">Set.add</a>.
72 </p>
73 <a id="entity_addsuppliers"></a>
74 <pre class="programlisting">import java.util.Set;
75 ...
76 public class Sample
77 {
78 ...
79 private void addSuppliers()
80 {
81 <strong class="userinput"><code> Set suppliers = views.getSupplierSet();
82 if (suppliers.isEmpty())
83 {
84 System.out.println("Adding Suppliers");
85 suppliers.add(new Supplier("S1", "Smith", 20, "London"));
86 suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
87 suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
88 suppliers.add(new Supplier("S4", "Clark", 20, "London"));
89 suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
90 }</code></strong>
91 }
92
93 private void addParts()
94 {
95 <strong class="userinput"><code> Set parts = views.getPartSet();
96 if (parts.isEmpty())
97 {
98 System.out.println("Adding Parts");
99 parts.add(new Part("P1", "Nut", "Red",
100 new Weight(12.0, Weight.GRAMS), "London"));
101 parts.add(new Part("P2", "Bolt", "Green",
102 new Weight(17.0, Weight.GRAMS), "Paris"));
103 parts.add(new Part("P3", "Screw", "Blue",
104 new Weight(17.0, Weight.GRAMS), "Rome"));
105 parts.add(new Part("P4", "Screw", "Red",
106 new Weight(14.0, Weight.GRAMS), "London"));
107 parts.add(new Part("P5", "Cam", "Blue",
108 new Weight(12.0, Weight.GRAMS), "Paris"));
109 parts.add(new Part("P6", "Cog", "Red",
110 new Weight(19.0, Weight.GRAMS), "London"));
111 }</code></strong>
112 }
113
114 private void addShipments()
115 {
116 <strong class="userinput"><code> Set shipments = views.getShipmentSet();
117 if (shipments.isEmpty())
118 {
119 System.out.println("Adding Shipments");
120 shipments.add(new Shipment("P1", "S1", 300));
121 shipments.add(new Shipment("P2", "S1", 200));
122 shipments.add(new Shipment("P3", "S1", 400));
123 shipments.add(new Shipment("P4", "S1", 200));
124 shipments.add(new Shipment("P5", "S1", 100));
125 shipments.add(new Shipment("P6", "S1", 100));
126 shipments.add(new Shipment("P1", "S2", 300));
127 shipments.add(new Shipment("P2", "S2", 400));
128 shipments.add(new Shipment("P2", "S3", 200));
129 shipments.add(new Shipment("P2", "S4", 200));
130 shipments.add(new Shipment("P4", "S4", 300));
131 shipments.add(new Shipment("P5", "S4", 400));
132 }</code></strong>
133 } </pre>
134 <p>
135 Instead of printing the key/value pairs by iterating over the
136 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#entrySet()" target="_top">Map.entrySet</a>
137
138 as done in the prior example, this example
139 iterates over the entities in the
140 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#values()" target="_top">Map.values</a>
141
142 collection.
143 </p>
144 <a id="entity_printdatabase"></a>
145 <pre class="programlisting">import java.util.Iterator;
146 import java.util.Set;
147 ...
148 public class Sample
149 {
150 ...
151 private class PrintDatabase implements TransactionWorker
152 {
153 public void doWork()
154 throws Exception
155 {
156 <strong class="userinput"><code> printValues("Parts",
157 views.getPartSet().iterator());
158 printValues("Suppliers",
159 views.getSupplierSet().iterator());</code></strong>
160 printValues("Suppliers for City Paris",
161 views.getSupplierByCityMap().duplicates(
162 "Paris").iterator());
163 <strong class="userinput"><code> printValues("Shipments",
164 views.getShipmentSet().iterator());</code></strong>
165 printValues("Shipments for Part P1",
166 views.getShipmentByPartMap().duplicates(
167 new PartKey("P1")).iterator());
168 printValues("Shipments for Supplier S1",
169 views.getShipmentBySupplierMap().duplicates(
170 new SupplierKey("S1")).iterator());
171 }
172 }
173 ...
174 } </pre>
175 <p>
176 The output of the example program is shown below.
177 </p>
178 <pre class="programlisting">Adding Suppliers
179 Adding Parts
180 Adding Shipments
181
182 --- Parts ---
183 Part: number=P1 name=Nut color=Red weight=[12.0 grams] city=London
184 Part: number=P2 name=Bolt color=Green weight=[17.0 grams] city=Paris
185 Part: number=P3 name=Screw color=Blue weight=[17.0 grams] city=Rome
186 Part: number=P4 name=Screw color=Red weight=[14.0 grams] city=London
187 Part: number=P5 name=Cam color=Blue weight=[12.0 grams] city=Paris
188 Part: number=P6 name=Cog color=Red weight=[19.0 grams] city=London
189
190 --- Suppliers ---
191 Supplier: number=S1 name=Smith status=20 city=London
192 Supplier: number=S2 name=Jones status=10 city=Paris
193 Supplier: number=S3 name=Blake status=30 city=Paris
194 Supplier: number=S4 name=Clark status=20 city=London
195 Supplier: number=S5 name=Adams status=30 city=Athens
196
197 --- Suppliers for City Paris ---
198 Supplier: number=S2 name=Jones status=10 city=Paris
199 Supplier: number=S3 name=Blake status=30 city=Paris
200
201 --- Shipments ---
202 Shipment: part=P1 supplier=S1 quantity=300
203 Shipment: part=P1 supplier=S2 quantity=300
204 Shipment: part=P2 supplier=S1 quantity=200
205 Shipment: part=P2 supplier=S2 quantity=400
206 Shipment: part=P2 supplier=S3 quantity=200
207 Shipment: part=P2 supplier=S4 quantity=200
208 Shipment: part=P3 supplier=S1 quantity=400
209 Shipment: part=P4 supplier=S1 quantity=200
210 Shipment: part=P4 supplier=S4 quantity=300
211 Shipment: part=P5 supplier=S1 quantity=100
212 Shipment: part=P5 supplier=S4 quantity=400
213 Shipment: part=P6 supplier=S1 quantity=100
214
215 --- Shipments for Part P1 ---
216 Shipment: part=P1 supplier=S1 quantity=300
217 Shipment: part=P1 supplier=S2 quantity=300
218
219 --- Shipments for Supplier S1 ---
220 Shipment: part=P1 supplier=S1 quantity=300
221 Shipment: part=P2 supplier=S1 quantity=200
222 Shipment: part=P3 supplier=S1 quantity=400
223 Shipment: part=P4 supplier=S1 quantity=200
224 Shipment: part=P5 supplier=S1 quantity=100
225 Shipment: part=P6 supplier=S1 quantity=100 </pre>
226 </div>
227 <div class="navfooter">
228 <hr />
229 <table width="100%" summary="Navigation footer">
230 <tr>
231 <td width="40%" align="left"><a accesskey="p" href="collectionswithentities.html">Prev</a> </td>
232 <td width="20%" align="center">
233 <a accesskey="u" href="Entity.html">Up</a>
234 </td>
235 <td width="40%" align="right"> <a accesskey="n" href="Tuple.html">Next</a></td>
236 </tr>
237 <tr>
238 <td width="40%" align="left" valign="top">
239 Creating Collections with Entity Bindings
240  </td>
241 <td width="20%" align="center">
242 <a accesskey="h" href="index.html">Home</a>
243 </td>
244 <td width="40%" align="right" valign="top"> Chapter 5. 
245 Using Tuples
246 </td>
247 </tr>
248 </table>
249 </div>
250 </body>
251 </html>