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