KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ResourceCollection


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import edu.umd.cs.findbugs.ba.Location;
29
30 /**
31  * A ResourceCollection defines all of the resources created
32  * and used in a particular method. It serves two related
33  * purposes:
34  * <ol>
35  * <li> Define all of the resources which exist in the method, and
36  * <li> Record where resources created in the method are created
37  * </ol>
38  * <p> This distinction is important because some resources
39  * which exist in the method aren't created in the method:
40  * for example, resources passed in as parameters.
41  *
42  * @author David Hovemeyer
43  */

44 public class ResourceCollection <Resource> {
45     private List JavaDoc<Resource> resourceList;
46     private Map JavaDoc<Location, Resource> locationToResourceMap;
47
48     /**
49      * Constructor.
50      * Creates empty collection.
51      */

52     public ResourceCollection() {
53         this.resourceList = new LinkedList JavaDoc<Resource>();
54         this.locationToResourceMap = new HashMap JavaDoc<Location, Resource>();
55     }
56
57     /**
58      * Add a preexisting resource.
59      * That is, one that is not created within the analyzed method.
60      * Resources passed to the method as parameters fall into
61      * this category.
62      *
63      * @param resource the preexisting resource
64      */

65     public void addPreexistingResource(Resource resource) {
66         resourceList.add(resource);
67     }
68
69     /**
70      * Add a resource created within the analyzed method.
71      *
72      * @param location the location
73      * @param resource the resource created at that location
74      */

75     public void addCreatedResource(Location location, Resource resource) {
76         resourceList.add(resource);
77         locationToResourceMap.put(location, resource);
78     }
79
80     /**
81      * Return whether or not there are any resources in the collection.
82      */

83     public boolean isEmpty() {
84         return resourceList.isEmpty();
85     }
86
87     /**
88      * Get an Iterator over all resources in the method.
89      * This includes both preexisting and created resources.
90      */

91     public Iterator JavaDoc<Resource> resourceIterator() {
92         return resourceList.iterator();
93     }
94
95     /**
96      * Get the resource that is created at given location.
97      *
98      * @param location the Location
99      * @return the Resource created at that location,
100      * or null if no resource is created at the location
101      */

102     public Resource getCreatedResource(Location location) {
103         return locationToResourceMap.get(location);
104     }
105 }
106
107 // vim:ts=4
108
Popular Tags