KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > test > data > Book


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.test.data;
25
26 import org.objectweb.jalisto.se.api.ClassDescription;
27 import org.objectweb.jalisto.se.JalistoFactory;
28 import org.objectweb.jalisto.se.impl.meta.type.MetaTypeFactory;
29
30 import java.io.Serializable JavaDoc;
31
32 public class Book implements Serializable JavaDoc {
33     private Book() {
34     }
35
36     private Book(String JavaDoc title, int pages, int price, String JavaDoc author) {
37         this.pages = pages;
38         this.title = title;
39         this.price = price;
40         this.author = author;
41     }
42
43     public String JavaDoc getAuthor() {
44         return author;
45     }
46
47     public void setAuthor(String JavaDoc author) {
48         this.author = author;
49     }
50
51     public int getPrice() {
52         return price;
53     }
54
55     public void setPrice(int price) {
56         this.price = price;
57     }
58
59     public int getPages() {
60         return pages;
61     }
62
63     public void setPages(int pages) {
64         this.pages = pages;
65     }
66
67     public String JavaDoc getTitle() {
68         return title;
69     }
70
71     public void setTitle(String JavaDoc title) {
72         this.title = title;
73     }
74
75     public static Book newBook() {
76         counter++;
77         return new Book(getNewTitle(counter),
78                         getNewPages(counter),
79                         getNewPrice(counter),
80                         getNewAuthor(counter));
81     }
82
83     public Object JavaDoc[] toArray() {
84         Object JavaDoc[] result = new Object JavaDoc[4];
85         result[0] = title;
86         result[1] = new Integer JavaDoc(pages);
87         result[2] = new Integer JavaDoc(price);
88         result[3] = author;
89         return result;
90     }
91
92     public String JavaDoc toString() {
93         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
94         sb.append("'").append(title);
95         sb.append("from ").append(author).append(", ");
96         sb.append(pages).append(" pages, ");
97         sb.append(price).append(" E");
98         sb.append("'");
99         return sb.toString();
100     }
101
102     public static Book toBook(Object JavaDoc[] array) {
103         Book book = new Book();
104         book.setTitle((String JavaDoc) array[0]);
105         book.setPages(((Integer JavaDoc) array[1]).intValue());
106         book.setPrice(((Integer JavaDoc) array[2]).intValue());
107         book.setAuthor((String JavaDoc) array[3]);
108         return book;
109     }
110
111     public static ClassDescription getMetaDescription() {
112         ClassDescription meta = JalistoFactory.createClassDescription(Book.class.getName());
113         meta.addField(JalistoFactory.createFieldDescription("title", MetaTypeFactory.StringType));
114         meta.addField(JalistoFactory.createFieldDescription("pages", MetaTypeFactory.IntegerType));
115         meta.addField(JalistoFactory.createFieldDescription("price", MetaTypeFactory.IntegerType));
116         meta.addField(JalistoFactory.createFieldDescription("author", MetaTypeFactory.StringType));
117         return meta;
118     }
119
120     private static String JavaDoc getNewTitle(int c) {
121         return titles[c % titles.length];
122     }
123
124     private static String JavaDoc getNewAuthor(int c) {
125         return authors[c % authors.length];
126     }
127
128     private static int getNewPrice(int c) {
129         return prices[c % prices.length];
130     }
131
132     public static int getNewPages(int c) {
133         return (c % maxPage);
134     }
135
136     private String JavaDoc title;
137     private int pages;
138     private int price;
139     private String JavaDoc author;
140
141     public static int counter = -1;
142     public static final String JavaDoc[] titles = {"Death Off Stage",
143                                            "At the Edge",
144                                            "Fingersmith",
145                                            "Metes and Bounds",
146                                            "A Fountain Filled with Blood",
147                                            "A Cold Case of Murder",
148                                            "Innocent Hearts",
149                                            "Tipping the Velvet",
150                                            "Bittersweet",
151                                            "The Jester"};
152     public static final int[] prices = {5,
153                                         10,
154                                         15,
155                                         20,
156                                         25,
157                                         30};
158     public static final String JavaDoc[] authors = {"James McManus",
159                                             "John Sandford",
160                                             "Robert Dallek",
161                                             "Andy Andrews"};
162     public static final int maxPage = 1000;
163
164
165     static final long serialVersionUID = -7589376666964761459L;
166 }
167
Popular Tags