001/*
002 * (c) 2005, 2009, 2010 ThoughtWorks Ltd
003 * All rights reserved.
004 *
005 * The software in this package is published under the terms of the BSD
006 * style license a copy of which has been included with this distribution in
007 * the LICENSE.txt file.
008 * 
009 * Created on 26-Jul-2005
010 */
011package proxytoys.examples.overview;
012
013import com.thoughtworks.proxy.factory.CglibProxyFactory;
014import com.thoughtworks.proxy.toys.multicast.Multicast;
015import com.thoughtworks.proxy.toys.multicast.Multicasting;
016
017import java.io.File;
018import java.lang.reflect.Method;
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.HashSet;
023import java.util.Iterator;
024import java.util.LinkedList;
025import java.util.List;
026import java.util.Set;
027
028
029/**
030 * @author Jörg Schaible
031 */
032public class MulticastToyExample {
033
034    public static void packageOverviewExample1() {
035        ArrayList<String> arrayList = new ArrayList<String>();
036        LinkedList<String> linkedList = new LinkedList<String>();
037        @SuppressWarnings("unchecked")
038        List<String> listCombined = List.class.cast(Multicasting.proxy(arrayList, linkedList).build());
039        if (listCombined.add("Hello")) {
040            System.out.println("List 1: " + arrayList.toString());
041            System.out.println("List 2: " + linkedList.toString());
042        }
043    }
044
045    public static void packageOverviewExample2() {
046        try {
047            List<Integer> list1 = new ArrayList<Integer>();
048            list1.add(5);
049            list1.add(100);
050            List<Integer> list2 = new LinkedList<Integer>();
051            list2.add(3);
052            @SuppressWarnings("unchecked")
053            List<Integer> listCombined = List.class.cast(Multicasting.proxy(list1, list2).build());
054            Multicast values = Multicast.class.cast(listCombined.get(0));
055            System.out.println("Sum of the first integers: "
056                    + values.multicastTargets(Integer.class, "intValue", null).toString());
057        } catch (NoSuchMethodException e) {
058            // Integer.class has a intValue method
059        }
060    }
061
062    public static void packageOverviewExample3() {
063        File workingDir = new File(".");
064        List<String> files = Arrays.asList(workingDir.list());
065        File multicast = Multicasting.proxy(File.class, List.class)
066                .with(workingDir, files)
067                .build(new CglibProxyFactory());
068        System.out.println("Current working directory: " + multicast.getAbsolutePath());
069        System.out.println("Files in working directory: " + List.class.cast(multicast).size());
070    }
071
072    public static void packageOverviewExample4() {
073        try {
074            Method method = String.class.getMethod("length");
075            Multicast multicast = Multicasting.proxy("ProxyToys", "is", "great").build();
076            System.out.println("Total number of characters: " + multicast.multicastTargets(method, null));
077            String[] strings = multicast.getTargetsInArray(String.class);
078            for (int i = 0; i < strings.length; i++) {
079                System.out.println("String[" + i + "]: " + strings[i]);
080            }
081        } catch (NoSuchMethodException e) {
082            // String.class has a length method
083        }
084    }
085
086    public static void packageOverviewExample5() {
087        List<String> list = new ArrayList<String>();
088        Set<String> set = new HashSet<String>();
089        list.add("ProxyToys");
090        set.add(null);
091        @SuppressWarnings("unchecked")
092        Collection<String> collection = Collection.class.cast(Multicasting.proxy(list, set).build());
093        Iterator<String> iter = collection.iterator();
094        String value = iter.next();
095        System.out.println("Element gained from the iterator: " + value);
096    }
097
098    public static void main(String[] args) {
099        System.out.println();
100        System.out.println();
101        System.out.println("Running Multicasting Toy Examples");
102        System.out.println();
103        System.out.println("Example 1 of Package Overview:");
104        packageOverviewExample1();
105        System.out.println();
106        System.out.println("Example 2 of Package Overview:");
107        packageOverviewExample2();
108        System.out.println();
109        System.out.println("Example 3 of Package Overview:");
110        packageOverviewExample3();
111        System.out.println();
112        System.out.println("Example 4 of Package Overview:");
113        packageOverviewExample4();
114        System.out.println();
115        System.out.println("Example 5 of Package Overview:");
116        packageOverviewExample5();
117    }
118}