View Javadoc

1   /*
2    * $Id: FruitBowl.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tck.testmodels.fruit;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  public class FruitBowl
24  {
25      /**
26       * logger used by this class
27       */
28      private static final Log logger = LogFactory.getLog(FruitBowl.class);
29  
30      private final Map bowl = Collections.synchronizedMap(new HashMap());
31  
32      public FruitBowl()
33      {
34          super();
35      }
36  
37      public FruitBowl(Fruit fruit[])
38      {
39          for (int i = 0; i < fruit.length; i++)
40          {
41              bowl.put(fruit[i].getClass(), fruit[i]);
42          }
43      }
44  
45      public FruitBowl(Apple apple, Banana banana)
46      {
47          bowl.put(Apple.class, apple);
48          bowl.put(Banana.class, banana);
49      }
50  
51      public boolean hasApple()
52      {
53          return bowl.get(Apple.class) != null;
54      }
55  
56      public boolean hasBanana()
57      {
58          return bowl.get(Banana.class) != null;
59      }
60  
61      public void addFruit(Fruit fruit)
62      {
63          bowl.put(fruit.getClass(), fruit);
64      }
65  
66      public Fruit[] addAppleAndBanana(Apple apple, Banana banana)
67      {
68          bowl.put(Apple.class, apple);
69          bowl.put(Banana.class, banana);
70          return new Fruit[]{apple, banana};
71      }
72  
73      public Fruit[] addBananaAndApple(Banana banana, Apple apple)
74      {
75          bowl.put(Apple.class, apple);
76          bowl.put(Banana.class, banana);
77          return new Fruit[]{banana, apple};
78  
79      }
80  
81      public List<Fruit> getFruit()
82      {
83          return new ArrayList(bowl.values());
84      }
85  
86      public Object consumeFruit(FruitLover fruitlover)
87      {
88          logger.debug("Got a fruit lover who says: " + fruitlover.speak());
89          for (Iterator iter = bowl.values().iterator(); iter.hasNext();)
90          {
91              ((Fruit) iter.next()).bite();
92          }
93          return fruitlover;
94      }
95  
96      public void setFruit(Fruit[] fruit)
97      {
98          for (int i = 0; i < fruit.length; i++)
99          {
100             bowl.put(fruit[i].getClass(), fruit[i]);
101         }
102     }
103 
104     public void setFruit(List fruit)
105     {
106         this.setFruit((Fruit[]) fruit.toArray(new Fruit[fruit.size()]));
107     }
108 
109     public Apple getApple()
110     {
111         return (Apple) bowl.get(Apple.class);
112     }
113 
114     public void setApple(Apple apple)
115     {
116         bowl.put(Apple.class, apple);
117     }
118 
119     public Banana getBanana()
120     {
121         return (Banana) bowl.get(Banana.class);
122     }
123 
124     public void setBanana(Banana banana)
125     {
126         bowl.put(Banana.class, banana);
127     }
128 
129 }