KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > tagkit > AbstractHost


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

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26 package soot.tagkit;
27
28 import soot.*;
29
30 import java.util.*;
31
32 // extended by SootClass, SootField, SootMethod, Scene
33

34 /**
35  * This class is the reference implementation for
36  * the Host interface, which allows arbitrary taggable
37  * data to be stored with Soot objects.
38  */

39 public class AbstractHost implements Host
40 {
41     // avoid creating an empty list for each element, when it is not used
42
// use lazy instantation (in addTag) instead
43
private final static List emptyList = Collections.EMPTY_LIST;
44     private List mTagList = emptyList;
45     
46     /** get the list of tags. This list should not be modified! */
47     public List getTags()
48     {
49         return mTagList;
50     }
51
52     /** remove the tag named <code>aName</code> */
53     public void removeTag(String JavaDoc aName)
54     {
55         int tagIndex;
56         if((tagIndex = searchForTag(aName)) != -1) {
57             mTagList.remove(tagIndex);
58         }
59     }
60
61     /** search for tag named <code>aName</code> */
62     private int searchForTag(String JavaDoc aName)
63     {
64         int i = 0;
65         Iterator it = mTagList.iterator();
66         while(it.hasNext()) {
67             Tag tag = (Tag) it.next();
68             if(tag.getName().equals(aName))
69                 return i;
70             i++;
71         }
72         return -1;
73     }
74
75     /** get the Tag object named <code>aName</code> */
76    public Tag getTag(String JavaDoc aName)
77     {
78         int tagIndex;
79         if((tagIndex = searchForTag(aName)) != -1) {
80             return (Tag) mTagList.get(tagIndex);
81         }
82         
83                 return null;
84     }
85
86     /** look if this host has a tag named <code>aName</code> */
87     public boolean hasTag(String JavaDoc aName)
88     {
89         return (searchForTag(aName) != -1);
90     }
91     
92     /** add tag <code>t</code> to this host */
93     public void addTag(Tag t)
94     {
95         if (mTagList == emptyList)
96             mTagList = new ArrayList(1);
97         mTagList.add(t);
98     }
99
100     /** Removes all the tags from this host. */
101     public void removeAllTags() {
102         mTagList = emptyList;
103     }
104
105     /** Adds all the tags from h to this host. */
106     public void addAllTagsOf( Host h ) {
107         for( Iterator tIt = h.getTags().iterator(); tIt.hasNext(); ) {
108             final Tag t = (Tag) tIt.next();
109             addTag( t );
110         }
111     }
112 }
113
114
115
116
117
Popular Tags