FileDocCategorySizeDatePackage
NewsBean.javaAPI DocExample2497Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.beans.news

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
2.0

Fields Summary
private ArrayList
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("JSP");
        item.setMsg("2nd 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);
        
        item = new NewsItemBean();
        item.setCategory("EJB");
        item.setMsg("JSP makes a great front-end for EJB.");
        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);
	}