KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > LoopBean


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.betwixt;
18
19 /** <p> This is a bean specifically designed to test cyclic references.
20   * The idea is that there's a count that counts every time <code>getFriend</code>
21   * gets called and throws a <code>RuntimeException</code> if the count gets too high.</p>
22   *
23   * @author Robert Burrell Donkin
24   * @version $Revision: 1.4 $
25   */

26 public class LoopBean
27 {
28     private static int count = 0;
29     
30     private static final int max_count = 100;
31
32     private LoopBean friend;
33     
34     private String JavaDoc name;
35     
36     public static final LoopBean createNoLoopExampleBean()
37     {
38         LoopBean root = new LoopBean("Root");
39         LoopBean levelOne = new LoopBean("level1");
40         LoopBean levelTwo = new LoopBean("level2");
41         LoopBean levelThree = new LoopBean("level3");
42         LoopBean levelFour = new LoopBean("level4");
43         LoopBean levelFive = new LoopBean("level5");
44         
45         root.setFriend(levelOne);
46         levelOne.setFriend(levelTwo);
47         levelTwo.setFriend(levelThree);
48         levelThree.setFriend(levelFour);
49         levelFour.setFriend(levelFive);
50         
51         return root;
52     }
53         
54     public static final LoopBean createLoopExampleBean()
55     {
56         LoopBean root = new LoopBean("Root");
57         LoopBean levelOne = new LoopBean("level1");
58         LoopBean levelTwo = new LoopBean("level2");
59         LoopBean levelThree = new LoopBean("level3");
60         LoopBean levelFour = new LoopBean("level4");
61         LoopBean levelFive = new LoopBean("level5");
62         
63         root.setFriend(levelOne);
64         levelOne.setFriend(levelTwo);
65         levelTwo.setFriend(levelThree);
66         levelThree.setFriend(levelFour);
67         levelFour.setFriend(levelFive);
68         levelFive.setFriend(root);
69         
70         return root;
71     }
72     
73     
74     public static final LoopBean createNotEmptyNoLoopExampleBean()
75     {
76         LoopBean root = new LoopBean("");
77         LoopBean levelOne = new LoopBean("");
78         LoopBean levelTwo = new LoopBean("");
79         LoopBean levelThree = new LoopBean("");
80         LoopBean levelFour = new LoopBean("");
81         LoopBean levelFive = new LoopBean("Not Empty");
82         
83         root.setFriend(levelOne);
84         levelOne.setFriend(levelTwo);
85         levelTwo.setFriend(levelThree);
86         levelThree.setFriend(levelFour);
87         levelFour.setFriend(levelFive);
88         
89         return root;
90     }
91     
92     public static final LoopBean createEmptyLoopExampleBean()
93     {
94         LoopBean root = new LoopBean("");
95         LoopBean levelOne = new LoopBean("");
96         LoopBean levelTwo = new LoopBean("");
97         LoopBean levelThree = new LoopBean("");
98         LoopBean levelFour = new LoopBean("");
99         LoopBean levelFive = new LoopBean("");
100         
101         root.setFriend(levelOne);
102         levelOne.setFriend(levelTwo);
103         levelTwo.setFriend(levelThree);
104         levelThree.setFriend(levelFour);
105         levelFour.setFriend(levelFive);
106         levelFive.setFriend(root);
107         
108         return root;
109     }
110     
111     public static final LoopBean createIdOnlyLoopExampleBean()
112     {
113         LoopBean root = new LoopBean("Root");
114         LoopBean levelOne = new LoopBean("level1");
115         LoopBean levelTwo = new LoopBean("level2");
116         LoopBean levelThree = new LoopBean("level3");
117         LoopBean levelFour = new LoopBean("level4");
118         LoopBean levelFive = new LoopBean("level5");
119         LoopBean notRoot = new LoopBean("Root");
120         
121         root.setFriend(levelOne);
122         levelOne.setFriend(levelTwo);
123         levelTwo.setFriend(levelThree);
124         levelThree.setFriend(levelFour);
125         levelFour.setFriend(levelFive);
126         levelFive.setFriend(notRoot);
127         
128         return root;
129     }
130     
131     public LoopBean(String JavaDoc name)
132     {
133         this.name = name;
134     }
135     
136     public LoopBean getFriend()
137     {
138         if (++count > max_count)
139         {
140             throw new RuntimeException JavaDoc("Looping!");
141         }
142         return friend;
143     }
144     
145     public void setFriend(LoopBean friend)
146     {
147         this.friend = friend;
148     }
149     
150     public String JavaDoc getName()
151     {
152         return name;
153     }
154     
155     public String JavaDoc toString()
156     {
157         return "[LoopBean] name=" + name;
158     }
159 }
160
Popular Tags