TOB 6 的编程界面最近完成了一次重大简化, 附件是更新后的持久应用样板程序, 用6.0的SUN JDK和1.6.5或更新的Apache Ant就可以编译.
简化后的持久类模样从下面代码可见一斑, 特别注意 getAllProducts() 的实现.
完整项目源码在附件zip中.
package tob.bookstore;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import av.tob.IAm;
import av.tob.Index;
import av.tob.Kin;
import av.tob.KinSet;
import av.tob.Retying;
import av.tob.Swappable;
import av.tob.TheRelation;
import av.tob.Writing;
@Swappable(typeInSwap = "Category")
public class Category extends TheRelation
{
@Index(unique = false)
protected static final String NAME_INDEX = "name";
protected Tie<Category> parent;
@IAm("parent")
protected KinSet<Category, Category> subcategories = null;
@IAm("maincate")
protected KinSet<Product, Product> products = null;
protected KinSet<Categorization, Product> moreProducts = null;
private String name;
private String description;
protected Category()
{
}
public Category(Category parentCate, String name, String desc)
{
this.parent = (parentCate == null) ? null : new Tie<Category>(
parentCate);
this.name = name;
this.description = desc;
}
public Category getParent()
{
return parent == null ? null : parent.o;
}
@Retying("parent")
public void changeParent(Category newParent)
{
this.parent = (newParent == null) ? null : new Tie<Category>(newParent);
}
public KinSet<Category, Category> getSubcategories()
{
return this.subcategories;
}
public KinSet<Product, Product> getMainProducts()
{
return this.products;
}
public KinSet<Categorization, Product> getMoreProducts()
{
return this.moreProducts;
}
private void enlistProducts(List<Product> l)
{
for (Kin<?, Product> p : products)
l.add(p.getO());
for (Kin<?, Product> p : moreProducts)
l.add(p.getO());
for (Kin<?, Category> subcate : subcategories)
subcate.getO().enlistProducts(l);
}
public List<Product> getAllProducts()
{
List<Product> all = new ArrayList<Product>(100);
this.enlistProducts(all);
return Collections.unmodifiableList(all);
}
public String getName()
{
return this.name;
}
@Writing
public void setName(String name)
{
this.name = name;
}
public String getDesciption()
{
return this.description;
}
@Writing
public void setDescription(String desc)
{
this.description = desc;
}
}