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 02-Aug-2005
010 */
011package proxytoys.examples.overview;
012
013import java.util.zip.CRC32;
014import java.util.zip.Checksum;
015
016import com.thoughtworks.proxy.factory.CglibProxyFactory;
017import com.thoughtworks.proxy.kit.Resetter;
018import com.thoughtworks.proxy.toys.pool.Pool;
019import com.thoughtworks.proxy.toys.pool.Poolable;
020
021
022/**
023 * @author Jörg Schaible
024 */
025public class PoolToyExample {
026
027    public static void packageOverviewExample1() {
028        Resetter<Checksum> resetter = new Resetter<Checksum>() {
029            public boolean reset(Checksum object) {
030                object.reset();
031                return true;
032            }
033        };
034        Pool<Checksum> pool = Pool.create(Checksum.class).resettedBy(resetter).build(new CglibProxyFactory());
035        pool.add(new CRC32());
036        {
037            Checksum checksum = pool.get();
038            checksum.update("JUnit".getBytes(), 0, 5);
039            System.out.println("CRC32 checksum of \"JUnit\": " + checksum.getValue());
040        }
041        {
042            Checksum checksum = pool.get();
043            if (checksum == null) {
044                System.out.println("No checksum available, force gc ...");
045                System.gc();
046            }
047            checksum = pool.get();
048            System.out.println("CRC32 of an resetted checksum: " + checksum.getValue());
049            Poolable.class.cast(checksum).returnInstanceToPool();
050        }
051    }
052
053    public static void main(String[] args) {
054        System.out.println();
055        System.out.println();
056        System.out.println("Running Pool Toy Examples");
057        System.out.println();
058        System.out.println("Example 1 of Package Overview:");
059        packageOverviewExample1();
060    }
061}