KickJava   Java API By Example, From Geeks To Geeks.

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


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 BookWithFormat implements Serializable JavaDoc {
33     private BookWithFormat() {
34     }
35
36     private BookWithFormat(String JavaDoc title, int pages, int price, Format format) {
37         this.pages = pages;
38         this.title = title;
39         this.price = price;
40         this.format = format;
41     }
42
43     public Author getAuthor() {
44         return author;
45     }
46
47     public void setAuthor(Author 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 Format getFormat() {
76         return format;
77     }
78
79     public void setFormat(Format format) {
80         this.format = format;
81     }
82
83     public boolean equals(Object JavaDoc o) {
84         try {
85             BookWithFormat candidate = (BookWithFormat) o;
86             boolean titleTest = ((candidate.title != null) && (candidate.title.equals(title))) ||
87                                 ((candidate.title == null) && (title == null));
88             boolean intTest = (candidate.pages == pages) && (candidate.price == price);
89             boolean authorTest = ((candidate.author != null) && (candidate.author.equals(author))) ||
90                                  ((candidate.author == null) && (author == null));
91             boolean formatTest = ((candidate.format != null) && (candidate.format.equals(format))) ||
92                                  ((candidate.format == null) && (format == null));
93
94             return (titleTest && intTest && authorTest && formatTest);
95         } catch (Exception JavaDoc e) {
96         }
97         return false;
98     }
99
100     public Object JavaDoc[] toArray() {
101         Object JavaDoc[] result = new Object JavaDoc[5];
102         result[0] = title;
103         result[1] = new Integer JavaDoc(pages);
104         result[2] = new Integer JavaDoc(price);
105         result[3] = author;
106         result[4] = format;
107         return result;
108     }
109
110     public String JavaDoc toString() {
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
112         sb.append("'").append(title);
113         sb.append("from ").append(author).append(", ");
114         sb.append(pages).append(" pages, ");
115         sb.append(price).append(" E");
116         sb.append("'");
117         return sb.toString();
118     }
119
120     public static BookWithFormat toBook(Object JavaDoc[] array) {
121         BookWithFormat book = new BookWithFormat();
122         book.setTitle((String JavaDoc) array[0]);
123         book.setPages(((Integer JavaDoc) array[1]).intValue());
124         book.setPrice(((Integer JavaDoc) array[2]).intValue());
125         book.setAuthor((Author) array[3]);
126         book.setFormat((Format) array[4]);
127         return book;
128     }
129
130     public static ClassDescription getMetaDescription() {
131         ClassDescription meta = JalistoFactory.createClassDescription(BookWithFormat.class.getName());
132         meta.addField(JalistoFactory.createFieldDescription("title", MetaTypeFactory.StringType));
133         meta.addField(JalistoFactory.createFieldDescription("pages", MetaTypeFactory.IntegerType));
134         meta.addField(JalistoFactory.createFieldDescription("price", MetaTypeFactory.IntegerType));
135         meta.addField(JalistoFactory.createFieldDescription("author", MetaTypeFactory.LinkType));
136         meta.addField(JalistoFactory.createFieldDescription("format", MetaTypeFactory.SerializedType));
137         return meta;
138     }
139
140     public static BookWithFormat newBook() {
141         counter++;
142         return new BookWithFormat(getNewTitle(counter),
143                                   getNewPages(counter),
144                                   getNewPrice(counter),
145                                   Format.getFormat());
146     }
147
148     private static String JavaDoc getNewTitle(int c) {
149         return titles[c % titles.length];
150     }
151
152     private static int getNewPrice(int c) {
153         return prices[c % prices.length];
154     }
155
156     public static int getNewPages(int c) {
157         return (c % maxPage);
158     }
159
160     private String JavaDoc title;
161     private int pages;
162     private int price;
163     private Author author;
164     private Format format;
165
166     public static int counter = -1;
167     public static final String JavaDoc[] titles = {"Death Off Stage",
168                                            "At the Edge",
169                                            "Fingersmith",
170                                            "Metes and Bounds",
171                                            "A Fountain Filled with Blood",
172                                            "A Cold Case of Murder",
173                                            "Innocent Hearts",
174                                            "Tipping the Velvet",
175                                            "Bittersweet",
176                                            "The Jester"};
177     public static final int[] prices = {5,
178                                         10,
179                                         15,
180                                         20,
181                                         25,
182                                         30};
183
184     public static final int maxPage = 1000;
185
186
187     static final long serialVersionUID = -7589461099514761459L;
188 }
189
Popular Tags