Collection Example

This example will show how to use TA for classes having collections as a field. TA project provides a simple TA collection implementation in a PagedList class. This collection is a very basic implementation and most probably will be replaced with a more mature collection in the final release. However, it can give you an idea on how to work with a collection.

We will use a Team class with a collection of Pilot objects:

 

Team.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.taexamples; 04 05import java.util.Iterator; 06import java.util.List; 07 08import com.db4o.activation.Activator; 09import com.db4o.ta.Activatable; 10 11 12public class Team implements Activatable { 13 14 List _pilots = new com.db4o.ta.tests.collections.PagedList(); 15 16 String _name; 17 18 //TA Activator 19 transient Activator _activator; 20 21 // Bind the class to an object container 22 public void bind(Activator activator) { 23 if (null != _activator) { 24 throw new IllegalStateException(); 25 } 26 _activator = activator; 27 } 28 29 // activate object fields 30 protected void activate() { 31 if (_activator == null) return; 32 _activator.activate(); 33 } 34 35 public void addPilot(Pilot pilot) { 36 _pilots.add(pilot); 37 } 38 39 public void listAllPilots() { 40 // activate before printing the collection members 41 activate(); 42 43 for (Iterator iter = _pilots.iterator(); iter.hasNext();) { 44 Pilot pilot = (Pilot) iter.next(); 45 System.out.println(pilot); 46 } 47 } 48}
Pilot.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.taexamples; 04 05import com.db4o.activation.Activator; 06import com.db4o.ta.Activatable; 07 08public class Pilot implements Activatable { 09 10 private final static String EXTENTION = ".jpg"; 11 12 private String _name; 13 14 private Image _image; 15 16 transient Activator _activator; 17 18 public Pilot(String name) { 19 _name = name; 20 _image = new Image(name + EXTENTION); 21 } 22 23 // Bind the class to an object container 24 public void bind(Activator activator) { 25 if (null != _activator) { 26 throw new IllegalStateException(); 27 } 28 _activator = activator; 29 } 30 31 // activate the object fields 32 protected void activate() { 33 if (_activator == null) 34 return; 35 _activator.activate(); 36 } 37 38 public String getName() { 39 // even simple String needs to be activated 40 activate(); 41 return _name; 42 } 43 44 public String toString() { 45 // use getName method, which already contains activation call 46 return getName(); 47 } 48}

Store and retrieve using the same configuration as in the previous exampleupdated.

 

TAExample.java: storeCollection
01private static void storeCollection() { 02 new File(DB4O_FILE_NAME).delete(); 03 ObjectContainer container = database(configureTA()); 04 if (container != null) { 05 try { 06 Team team = new Team(); 07 for (int i = 0; i < 10; i++) { 08 team.addPilot(new Pilot("Pilot #" + i)); 09 } 10 container.set(team); 11 container.commit(); 12 } catch (Exception ex) { 13 ex.printStackTrace(); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }

TAExample.java: testCollectionActivation
01private static void testCollectionActivation() { 02 storeCollection(); 03 ObjectContainer container = database(configureTA()); 04 if (container != null) { 05 try { 06 Team team = (Team) container.get(new Team()).next(); 07 // this method will activate all the members in the collection 08 team.listAllPilots(); 09 } catch (Exception ex) { 10 ex.printStackTrace(); 11 } finally { 12 closeDatabase(); 13 } 14 } 15 }