1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // Example.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
19 //
20 //----------------------------------------------------------------------
21 package org.perfectjpattern.core.structural.adapter;
22
23 import java.util.*;
24
25 import org.perfectjpattern.core.api.structural.adapter.*;
26 import org.slf4j.*;
27
28 /**
29 * Startup Main for the Adapter Pattern Example code
30 *
31 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
32 * @version $Revision: 1.0 $Date: Jan 29, 2009 11:48:47 AM $
33 */
34 //CHECKSTYLE:OFF
35 public
36 class Example
37 //CHECKSTYLE:ON
38 {
39 //------------------------------------------------------------------------
40 // public
41 //------------------------------------------------------------------------
42 public static void
43 main(String[] anArguments)
44 {
45 //---------------------------------------------------------------
46 // Create List2QueueAdapter instance
47 //---------------------------------------------------------------
48 List<String> myList = new LinkedList<String>();
49
50 IAdapter<Queue<String>, List<String>> myAdapter =
51 new List2QueueAdapter<String>(myList);
52
53 //---------------------------------------------------------------
54 // Retrieve the Adapter's Target interface view i.e. the Queue
55 //---------------------------------------------------------------
56 Queue<String> myQueue = myAdapter.getTarget();
57
58 //---------------------------------------------------------------
59 // Use the Queue as you would normally do, unaware that the
60 // underlying implementation comes from a List
61 //---------------------------------------------------------------
62 myQueue.offer("Hello");
63 myQueue.add("Adapter");
64 myQueue.offer("World!");
65
66 String myPollValue = myQueue.poll();
67 theLogger.debug(myPollValue);
68
69 String myPeekValue = myQueue.peek();
70 theLogger.debug(myPeekValue);
71
72 theLogger.debug(Arrays.toString(myQueue.toArray()));
73 }
74
75 //------------------------------------------------------------------------
76 // protected
77 //------------------------------------------------------------------------
78 protected static void
79 setLogger(Logger aLogger)
80 {
81 theLogger = aLogger;
82 }
83
84 //------------------------------------------------------------------------
85 // members
86 //------------------------------------------------------------------------
87 /**
88 * Provides logging facilities for this class instance
89 */
90 private static Logger theLogger = LoggerFactory.getLogger(Example.class);
91 }