View Javadoc

1   /*
2    * $Id: FruitBowl.java 22409 2011-07-14 05:14:27Z dirk.olmes $
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.List;
17  import java.util.Map;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  public class FruitBowl
23  {
24      /**
25       * logger used by this class
26       */
27      private static final Log logger = LogFactory.getLog(FruitBowl.class);
28  
29      private final Map<Class<?>, Fruit> bowl = Collections.synchronizedMap(new HashMap<Class<?>, Fruit>());
30  
31      public FruitBowl()
32      {
33          super();
34      }
35  
36      public FruitBowl(Fruit fruit[])
37      {
38          for (int i = 0; i < fruit.length; i++)
39          {
40              bowl.put(fruit[i].getClass(), fruit[i]);
41          }
42      }
43  
44      public FruitBowl(Apple apple, Banana banana)
45      {
46          bowl.put(Apple.class, apple);
47          bowl.put(Banana.class, banana);
48      }
49  
50      public boolean hasApple()
51      {
52          return bowl.get(Apple.class) != null;
53      }
54  
55      public boolean hasBanana()
56      {
57          return bowl.get(Banana.class) != null;
58      }
59  
60      public void addFruit(Fruit fruit)
61      {
62          bowl.put(fruit.getClass(), fruit);
63      }
64  
65      public Fruit[] addAppleAndBanana(Apple apple, Banana banana)
66      {
67          bowl.put(Apple.class, apple);
68          bowl.put(Banana.class, banana);
69          return new Fruit[]{apple, banana};
70      }
71  
72      public Fruit[] addBananaAndApple(Banana banana, Apple apple)
73      {
74          bowl.put(Apple.class, apple);
75          bowl.put(Banana.class, banana);
76          return new Fruit[]{banana, apple};
77  
78      }
79  
80      public List<Fruit> getFruit()
81      {
82          return new ArrayList<Fruit>(bowl.values());
83      }
84  
85      public Object consumeFruit(FruitLover fruitlover)
86      {
87          logger.debug("Got a fruit lover who says: " + fruitlover.speak());
88          for (Fruit fruit : bowl.values())
89          {
90              fruit.bite();
91          }
92          return fruitlover;
93      }
94  
95      public void setFruit(Fruit[] fruit)
96      {
97          for (int i = 0; i < fruit.length; i++)
98          {
99              bowl.put(fruit[i].getClass(), fruit[i]);
100         }
101     }
102 
103     public void setFruit(List<Fruit> fruit)
104     {
105         this.setFruit(fruit.toArray(new Fruit[fruit.size()]));
106     }
107 
108     public Apple getApple()
109     {
110         return (Apple) bowl.get(Apple.class);
111     }
112 
113     public void setApple(Apple apple)
114     {
115         bowl.put(Apple.class, apple);
116     }
117 
118     public Banana getBanana()
119     {
120         return (Banana) bowl.get(Banana.class);
121     }
122 
123     public void setBanana(Banana banana)
124     {
125         bowl.put(Banana.class, banana);
126     }
127 
128 }