View Javadoc
1   /*
2    * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.tools.asm;
27  
28  import sun.tools.java.*;
29  import java.util.Hashtable;
30  import java.util.Enumeration;
31  import java.util.Arrays;
32  
33  /**
34   * WARNING: The contents of this source file are not part of any
35   * supported API.  Code that depends on them does so at its own risk:
36   * they are subject to change or removal without notice.
37   */
38  public final
39  class SwitchData {
40      int minValue, maxValue;
41      Label defaultLabel = new Label();
42      Hashtable<Integer,Label> tab = new Hashtable<>();
43  // JCOV
44      Hashtable<Integer,Long> whereCaseTab = null;
45  // end JCOV
46  
47      /**
48       * Get a label
49       */
50      public Label get(int n) {
51          return tab.get(n);
52      }
53  
54      /**
55       * Get a label
56       */
57      public Label get(Integer n) {
58          return tab.get(n);
59      }
60  
61      /**
62       * Add a label
63       */
64      public void add(int n, Label lbl) {
65          if (tab.size() == 0) {
66              minValue = n;
67              maxValue = n;
68          } else {
69              if (n < minValue) {
70                  minValue = n;
71              }
72              if (n > maxValue) {
73                  maxValue = n;
74              }
75          }
76          tab.put(Integer.valueOf(n), lbl);
77      }
78  
79      /**
80       * Get the default label
81       */
82      public Label getDefaultLabel() {
83          return defaultLabel;
84      }
85  
86      /**
87       * Return the keys of this enumaration sorted in ascending order
88       */
89      public synchronized Enumeration<Integer> sortedKeys() {
90          return new SwitchDataEnumeration(tab);
91      }
92  
93  // JCOV
94      public void initTableCase() {
95          whereCaseTab = new Hashtable<Integer,Long>();
96      }
97      public void addTableCase(int index, long where) {
98          if (whereCaseTab != null)
99              whereCaseTab.put(Integer.valueOf(index), Long.valueOf(where));
100     }
101     // this puts String key into Hashtable<Integer,Long>
102     @SuppressWarnings("unchecked")
103     public void addTableDefault(long where) {
104         if (whereCaseTab != null)
105             ((Hashtable)whereCaseTab).put("default", Long.valueOf(where));
106     }
107     public long whereCase(Object key) {
108         Long i = whereCaseTab.get(key);
109         return (i == null) ? 0L : i.longValue();
110     }
111     public boolean getDefault() {
112          return (whereCase("default") != 0L);
113     }
114 // end JCOV
115 }
116 
117 class SwitchDataEnumeration implements Enumeration<Integer> {
118     private Integer table[];
119     private int current_index = 0;
120 
121     /**
122      * Create a new enumeration from the hashtable.  Each key in the
123      * hash table will be an Integer, with the value being a label.  The
124      * enumeration returns the keys in sorted order.
125      */
126     SwitchDataEnumeration(Hashtable<Integer,Label> tab) {
127         table = new Integer[tab.size()];
128         int i = 0;
129         for (Enumeration<Integer> e = tab.keys() ; e.hasMoreElements() ; ) {
130             table[i++] = e.nextElement();
131         }
132         Arrays.sort(table);
133         current_index = 0;
134     }
135 
136     /**
137      * Are there more keys to return?
138      */
139     public boolean hasMoreElements() {
140         return current_index < table.length;
141     }
142 
143     /**
144      * Return the next key.
145      */
146     public Integer nextElement() {
147         return table[current_index++];
148     }
149 }