KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > vna > ValueNumberFactory


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,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.ba.vna;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 /**
26  * Factory for ValueNumbers.
27  * A single Factory must be used to create all of the ValueNumbers
28  * for a method.
29  *
30  * @author David Hovemeyer
31  * @see ValueNumber
32  */

33 public class ValueNumberFactory {
34     /**
35      * Store all allocated value numbers.
36      */

37     private ArrayList JavaDoc<ValueNumber> allocatedValueList = new ArrayList JavaDoc<ValueNumber>();
38
39     /**
40      * Create a fresh (unique) value number.
41      */

42     public ValueNumber createFreshValue() {
43         ValueNumber result = new ValueNumber(getNumValuesAllocated());
44         allocatedValueList.add(result);
45         return result;
46     }
47
48     /**
49      * Return a previously allocated value.
50      */

51     public ValueNumber forNumber(int number) {
52         if (number >= getNumValuesAllocated())
53             throw new IllegalArgumentException JavaDoc("Value " + number + " has not been allocated");
54         return allocatedValueList.get(number);
55     }
56
57     /**
58      * Get the number of values which have been created.
59      */

60     public int getNumValuesAllocated() {
61         return allocatedValueList.size();
62     }
63
64     /**
65      * Compact the value numbers produced by this factory.
66      *
67      * @param map array mapping old numbers to new numbers
68      * @param numValuesAllocated the number of values allocated in the new numbering
69      */

70     public void compact(int[] map, int numValuesAllocated) {
71         ArrayList JavaDoc<ValueNumber> oldList = this.allocatedValueList;
72         ArrayList JavaDoc<ValueNumber> newList = new ArrayList JavaDoc<ValueNumber>(Collections.nCopies(numValuesAllocated, (ValueNumber)null));
73
74         for (ValueNumber value : oldList) {
75             int newNumber = map[value.getNumber()];
76             if (newNumber >= 0) {
77                 // Note: because we are simply assigning new numbers to the
78
// old ValueNumber objects, their flags remain valid.
79
value.number = newNumber;
80                 newList.set(newNumber, value);
81             }
82         }
83
84         this.allocatedValueList = newList;
85     }
86
87 }
88
89 // vim:ts=4
90
Popular Tags