FileDocCategorySizeDatePackage
NewsBean.javaAPI DocExample2490Tue Jun 08 11:26:42 BST 2004com.mycompany.messages

NewsBean

public class NewsBean extends Object implements Serializable
This class maintains a list of NewsItemBean objects. It's only intended as an example. A real implementation would use a database to keep track of the objects instead.
author
Hans Bergsten, Gefion Software
version
1.0

Fields Summary
private List
newsItems
private int[]
idSequence
Constructors Summary
public NewsBean()
Creates a new instance and fills it with a few sample news items.

    
                      
      
        addDefaultItems();
    
Methods Summary
private voidaddDefaultItems()
Creates sample news items and adds them to the list.

        NewsItemBean item = new NewsItemBean();
	item.setCategory("JSF");
        item.setMsg("O'Reilly's JSF book available now!");
        item.setPostedBy("Hans Bergsten");
        setNewsItem(item);

        item = new NewsItemBean();
        item.setCategory("JSP");
        item.setMsg("3rd edition of O'Reilly JSP book released!");
        item.setPostedBy("Hans Bergsten");
        setNewsItem(item);
        
        item = new NewsItemBean();
        item.setCategory("Servlet");
        item.setMsg("Servets and JSP: a powerful duo.");
        item.setPostedBy("Hans Bergsten");
        setNewsItem(item);
    
public NewsItemBean[]getNewsItems()
Returns an array of all items.

	NewsItemBean[] a = null;
        synchronized (newsItems) {
	    a = (NewsItemBean[])
		newsItems.toArray(new NewsItemBean[newsItems.size()]);
        }
        return a;
    
public voidremoveNewsItem(int id)
Removes the news item with the specified id.

        synchronized (newsItems) {
            for (int i = 0; i < newsItems.size(); i++) {
                NewsItemBean item = (NewsItemBean) newsItems.get(i);
                if (id == item.getId()) {
                    newsItems.remove(i);
                    break;
                }
            }
        }
    
public voidsetNewsItem(NewsItemBean newsItem)
Adds a news item to the list.

        synchronized (idSequence) {
            newsItem.setId(idSequence[0]++);
        }
	synchronized (newsItems) {
	    newsItems.add(newsItem);
	}