View Javadoc

1   /*
2    * $Id: FruitBowl.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.Collections;
14  import java.util.HashMap;
15  import java.util.Iterator;
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 bowl = Collections.synchronizedMap(new HashMap());
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 Object consumeFruit(FruitLover fruitlover)
61      {
62          logger.debug("Got a fruit lover who says: " + fruitlover.speak());
63          for (Iterator iter = bowl.values().iterator(); iter.hasNext();)
64          {
65              ((Fruit) iter.next()).bite();
66          }
67          return fruitlover;
68      }
69  
70      public void setFruit(Fruit[] fruit)
71      {
72          for (int i = 0; i < fruit.length; i++)
73          {
74              bowl.put(fruit[i].getClass(), fruit[i]);
75          }
76      }
77  
78      public void setFruit(List fruit)
79      {
80          this.setFruit((Fruit[]) fruit.toArray(new Fruit[fruit.size()]));
81      }
82  
83      public Apple getApple()
84      {
85          return (Apple) bowl.get(Apple.class);
86      }
87  
88      public void setApple(Apple apple)
89      {
90          bowl.put(Apple.class, apple);
91      }
92  
93      public Banana getBanana()
94      {
95          return (Banana) bowl.get(Banana.class);
96      }
97  
98      public void setBanana(Banana banana)
99      {
100         bowl.put(Banana.class, banana);
101     }
102 
103 }