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 21-Jul-2005
010 */
011package proxytoys.examples.overview;
012
013import com.thoughtworks.proxy.factory.CglibProxyFactory;
014import com.thoughtworks.proxy.toys.echo.Echoing;
015
016import java.io.File;
017import java.io.PrintWriter;
018import java.util.Date;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022import java.util.NoSuchElementException;
023
024
025/**
026 * @author Jörg Schaible
027 */
028public class EchoToyExample {
029
030    public static void packageOverviewExample1() {
031        @SuppressWarnings("unchecked")
032        Map<String, Object> map = Echoing.proxy(Map.class)
033                .with(new HashMap<String, Object>())
034                .to(new PrintWriter(System.err))
035                .build(new CglibProxyFactory());
036        map.put("Date", new Date());
037        map.put("File", new File("."));
038        try {
039            Iterator<String> iter = map.keySet().iterator();
040            while (true) {
041                String key = iter.next();
042                Object value = map.get(key);
043                if (value instanceof Date) {
044                    Date date = (Date) value;
045                    date.setTime(4711);
046                } else if (value instanceof File) {
047                    File file = (File) value;
048                    if (file.exists()) {
049                        file.renameTo(new File(".."));
050                    }
051                }
052            }
053        } catch (NoSuchElementException e) {
054            // No further element
055        }
056    }
057
058    public static void main(String[] args) {
059        System.out.println();
060        System.out.println();
061        System.out.println("Running Echo Toy Examples");
062        System.out.println();
063        System.out.println("Example 1 of Package Overview:");
064        packageOverviewExample1();
065    }
066}