Source Code Cross Referenced for Pool.java in  » Testing » TT » U2 » T2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » TT » U2.T2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package U2.T2;
002:
003:        import java.util.*;
004:
005:        /**
006:         * Represents object pools. An instance of this class is called an
007:         * object pool, or simply pool. It is used by T2's test engine to keep
008:         * track of objects created and accessed during its executions --see
009:         * the explanation of the {@link U2.T2 U2.T2 package}.
010:         *
011:         * <p> A Pool will be organized as follows. Essentially, it is a set
012:         * of objects. Let's call this set S. The pool contains an <u>object
013:         * map</u>, which is a mapping from unique integer keys to objects in
014:         * S. The pool also has a <u>domain map</u>, which is a mapping from
015:         * Class to 'domains'. If the domain map maps a Class C to a domain D,
016:         * then D is essentially a set of objects, all should be of class
017:         * C. Each domain will be implemented as a linked list of the integer
018:         * indices over the object map.
019:         *
020:         */
021:
022:        public class Pool {
023:
024:            private HashMap<Integer, Object> objectMap;
025:            private HashMap<Class, LinkedList<Integer>> domainMap;
026:            private int objectCount;
027:
028:            /**
029:             * Just a random generator.
030:             */
031:            private Random rnd;
032:
033:            /**
034:             * Create an empty pool.
035:             */
036:            public Pool() {
037:                objectMap = new HashMap<Integer, Object>();
038:                domainMap = new HashMap<Class, LinkedList<Integer>>();
039:                objectCount = 0;
040:                rnd = new Random();
041:            }
042:
043:            /**
044:             * Returns the number of objects in the pool.
045:             */
046:            public int get_objectCount() {
047:                return objectCount;
048:            }
049:
050:            /**
051:             * This will reset the pool to whatever its normal starting
052:             * state. This method is called whenever a new execution is
053:             * started. The default behavior of this method is to empty the
054:             * pool. To create a pool that is pre-populated, make a subclass
055:             * and overide this method so that reset will fill the pool with
056:             * its pre-population.
057:             */
058:            public void reset() {
059:                objectMap = new HashMap<Integer, Object>();
060:                domainMap = new HashMap<Class, LinkedList<Integer>>();
061:                objectCount = 0;
062:            }
063:
064:            /**
065:             * Returns the object from objectMap indexed with i.
066:             *
067:             * <p>Precond: i should be an existing index.
068:             */
069:            public Object get(int i) {
070:                return objectMap.get(i);
071:            }
072:
073:            /**
074:             * Randomly draw an object of class C from the pool. It actually
075:             * returns the index of the object rather than the object
076:             * itself. It returns null if no instance of C is found in the
077:             * pool.
078:             */
079:            public Integer rndGetIndex(Class C) {
080:                // get all classes from the pool which are either C or
081:                // subclasses of C:
082:                LinkedList<Class> classes = new LinkedList<Class>();
083:                for (Class D : domainMap.keySet())
084:                    if (C.isAssignableFrom(D))
085:                        classes.add(D);
086:
087:                if (classes.isEmpty())
088:                    return null;
089:
090:                // Now pick one domain randomly:
091:                Class chosenClass = classes.get(rnd.nextInt(classes.size()));
092:                // now randomly pick an index from the corresponding domain:
093:                LinkedList<Integer> domain = domainMap.get(chosenClass);
094:                if (domain.isEmpty())
095:                    return null;
096:                else
097:                    return domain.get(rnd.nextInt(domain.size()));
098:            }
099:
100:            /**
101:             * Add an object u into the pool. It returns the index
102:             * of u in the pool's object map.
103:             */
104:            public int put(Object u) {
105:                int index = objectCount;
106:                Class C = u.getClass();
107:                objectCount++;
108:                objectMap.put(index, u);
109:                if (domainMap.containsKey(C))
110:                    (domainMap.get(C)).add(index);
111:                else {
112:                    LinkedList<Integer> domain = new LinkedList<Integer>();
113:                    domain.add(index);
114:                    domainMap.put(C, domain);
115:                }
116:                return index;
117:            }
118:
119:            /**
120:             * For testing the class.
121:             */
122:            public static void main(String[] args) {
123:                A a1 = new A("Alice");
124:                A a2 = new A("Bob");
125:                A2 a3 = new A2("Vilain", 100);
126:                A2 a4 = new A2("Vilain", 999);
127:
128:                Pool p = new Pool();
129:                p.put(a1);
130:                p.put(a2);
131:                p.put(a3);
132:                p.put(a4);
133:
134:                Class A = a1.getClass();
135:                Class A2 = a3.getClass();
136:
137:                System.out.println("# " + p.get(p.rndGetIndex(A)));
138:                System.out.println("# " + p.get(p.rndGetIndex(A)));
139:                System.out.println("# " + p.get(p.rndGetIndex(A)));
140:                System.out.println("# " + p.get(p.rndGetIndex(A)));
141:                System.out.println("# " + p.get(p.rndGetIndex(A)));
142:                System.out.println("> " + p.get(p.rndGetIndex(A2)));
143:                System.out.println("> " + p.get(p.rndGetIndex(A2)));
144:                System.out.println("> " + p.get(p.rndGetIndex(A2)));
145:                System.out.println("> " + p.get(p.rndGetIndex(A2)));
146:                System.out.println("> " + p.get(p.rndGetIndex(A2)));
147:
148:            }
149:
150:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.