Source Code Cross Referenced for PriorityList.java in  » Testing » jacareto » jacareto » toolkit » 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 » jacareto » jacareto.toolkit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Jacareto Copyright (c) 2002-2005
003:         * Applied Computer Science Research Group, Darmstadt University of
004:         * Technology, Institute of Mathematics & Computer Science,
005:         * Ludwigsburg University of Education, and Computer Based
006:         * Learning Research Group, Aachen University. All rights reserved.
007:         *
008:         * Jacareto is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public
010:         * License as published by the Free Software Foundation; either
011:         * version 2 of the License, or (at your option) any later version.
012:         *
013:         * Jacareto is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016:         * General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public
019:         * License along with Jacareto; if not, write to the Free
020:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021:         *
022:         */
023:
024:        package jacareto.toolkit;
025:
026:        import java.util.Iterator;
027:        import java.util.LinkedList;
028:        import java.util.List;
029:        import java.util.NoSuchElementException;
030:
031:        /**
032:         * List for elements which have a priority.
033:         * 
034:         * <p>
035:         * If o1 and o2 are objects in the priority list with the priorities p(o1) and p(o2), the following
036:         * statement is true: p(o1) &lt; p(o2) =&gt; index(o1) &gt; index(02). Objects with the same
037:         * priority are ordered randomly.
038:         * </p>
039:         * If an object is added with no priority or a priority less than zero, then its priority is set to
040:         * -1 (which stands for the lowest priority).
041:         *
042:         * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
043:         * @version 1.0
044:         */
045:        public class PriorityList {
046:            /** The internal list. */
047:            private List list;
048:
049:            /**
050:             * Creates a new Priority List.
051:             */
052:            public PriorityList() {
053:                list = new LinkedList();
054:            }
055:
056:            /**
057:             * Add an element with a specified priority.
058:             *
059:             * @param element the element to add
060:             * @param priority the priority
061:             */
062:            public void add(Object element, int priority) {
063:                PriorityNode p = new PriorityNode(element, priority);
064:
065:                if (priority < 0) {
066:                    priority = -1;
067:                }
068:
069:                if (list.isEmpty()) {
070:                    list.add(p);
071:                } else {
072:                    for (int i = 0; i < list.size(); i++) {
073:                        PriorityNode t = (PriorityNode) list.get(i);
074:
075:                        if (t.getPriority() < p.getPriority()) {
076:                            list.add(i, p);
077:
078:                            return;
079:                        }
080:                    }
081:
082:                    list.add(p);
083:                }
084:            }
085:
086:            /**
087:             * Add an element with the lowest priority.
088:             *
089:             * @param element the element to add
090:             */
091:            public void add(Object element) {
092:                add(element, -1);
093:            }
094:
095:            /**
096:             * Removes an element.
097:             *
098:             * @param element the element to remove
099:             */
100:            public void remove(Object element) {
101:                for (int i = 0; i < list.size(); i++) {
102:                    if (get(i) == element) {
103:                        list.remove(i);
104:
105:                        break;
106:                    }
107:                }
108:            }
109:
110:            /**
111:             * Returns the priority of a given object.
112:             *
113:             * @param object the object
114:             *
115:             * @return the priority, or <code>-2</code> if the object is not contained.
116:             */
117:            public int getPriority(Object object) {
118:                int result = -2;
119:                Iterator it = list.iterator();
120:
121:                while (it.hasNext()) {
122:                    PriorityNode node = (PriorityNode) it.next();
123:
124:                    if (node.getElement() == object) {
125:                        result = node.getPriority();
126:
127:                        break;
128:                    }
129:                }
130:
131:                return result;
132:            }
133:
134:            /**
135:             * Returns an element at the specified position.
136:             *
137:             * @param position the position of the desired element
138:             *
139:             * @return the element
140:             */
141:            public Object get(int position) {
142:                return ((PriorityNode) list.get(position)).getElement();
143:            }
144:
145:            /**
146:             * Returns the size of the list.
147:             *
148:             * @return the size
149:             */
150:            public int size() {
151:                return list.size();
152:            }
153:
154:            /**
155:             * Returns whether an object is contained in the list or not.
156:             *
157:             * @param element the element to be tested
158:             *
159:             * @return <code>true</code> if the element is contained in the list, otherwise
160:             *         <code>false</code>
161:             */
162:            public boolean contains(Object element) {
163:                for (int i = 0; i < list.size(); i++) {
164:                    if (get(i) == element) {
165:                        return true;
166:                    }
167:                }
168:
169:                return false;
170:            }
171:
172:            /**
173:             * Returns an iterator on the list. The element with the highest priority comes first.
174:             *
175:             * @return the iterator
176:             */
177:            public Iterator iterator() {
178:                return new PriorityIterator();
179:            }
180:
181:            /**
182:             * A list node.
183:             */
184:            static class PriorityNode {
185:                //~ Instance fields ------------------------------------------------------------------------
186:
187:                private Object element;
188:                private int priority;
189:
190:                //~ Constructors ---------------------------------------------------------------------------
191:
192:                public PriorityNode(Object element, int priority) {
193:                    setElement(element);
194:                    setPriority(priority);
195:                }
196:
197:                //~ Methods --------------------------------------------------------------------------------
198:
199:                private void setElement(Object element) {
200:                    this .element = element;
201:                }
202:
203:                private void setPriority(int priority) {
204:                    this .priority = priority;
205:                }
206:
207:                public Object getElement() {
208:                    return element;
209:                }
210:
211:                public int getPriority() {
212:                    return priority;
213:                }
214:            }
215:
216:            /**
217:             * An iterator.
218:             */
219:            public class PriorityIterator implements  Iterator {
220:                //~ Instance fields ------------------------------------------------------------------------
221:
222:                private int index;
223:
224:                //~ Constructors ---------------------------------------------------------------------------
225:
226:                public PriorityIterator() {
227:                    this .index = 0;
228:                }
229:
230:                //~ Methods --------------------------------------------------------------------------------
231:
232:                public boolean hasNext() {
233:                    return index < list.size();
234:                }
235:
236:                public Object next() throws NoSuchElementException {
237:                    if (index >= list.size()) {
238:                        throw new NoSuchElementException();
239:                    }
240:
241:                    return ((PriorityNode) list.get(index++)).getElement();
242:                }
243:
244:                public void remove() {
245:                    throw new UnsupportedOperationException();
246:                }
247:            }
248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.