Debian Codegraph @

src/main/java/org/jboss/classfilewriter/code/TableSwitchBuilder.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 *
4 * Copyright 2012 Red Hat, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.jboss.classfilewriter.code;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.atomic.AtomicReference;
25
26 /**
27 * builder class used to build a tableswitch statement.
28 *
29 * @author Stuart Douglas
30 */
31 public class TableSwitchBuilder {
32
33 private final CodeLocation defaultLocation;
34 private final AtomicReference<BranchEnd> defaultBranchEnd;
35 private final List<ValuePair> values = new ArrayList<ValuePair>();
36 private final int low;
37 private final int high;
38
39 /**
40 * Builds a lookup switch statement with no specified default location.
41 *
42 * When the lookup switch is added to the code attribute a {@link org.jboss.classfilewriter.code.BranchEnd} will be returned that can be used to
43 * set the location.
44 * @param low
45 * @param high
46 */
47 public TableSwitchBuilder(final int low, final int high) {
48 this.low = low;
49 this.high = high;
50 defaultBranchEnd = new AtomicReference<BranchEnd>();
51 defaultLocation = null;
52 }
53
54 /**
55 * Builds a lookup switch statement, specifying the default location
56 * @param defaultLocation The default location
57 * @param low
58 * @param high
59 */
60 public TableSwitchBuilder(final CodeLocation defaultLocation, final int low, final int high) {
61 this.defaultLocation = defaultLocation;
62 this.low = low;
63 this.high = high;
64 defaultBranchEnd = null;
65 }
66
67 /**
68 * Adds a value to the table that is at a location yet to be written.
69 *
70 * After this lookup switch has been written then the BranchEnd can be retrieved
71 * from the returned reference.
72 *
73 * @return A reference to the BranchEnd that will be created.
74 */
75 public AtomicReference<BranchEnd> add() {
76 final AtomicReference<BranchEnd> end = new AtomicReference<BranchEnd>();
77 ValuePair vp = new ValuePair(end);
78 values.add(vp);
79 return end;
80 }
81 /**
82 * Adds a value to the table
83 *
84 *
85 */
86 public TableSwitchBuilder add( final CodeLocation location) {
87 values.add(new ValuePair(location));
88 return this;
89 }
90
91 public CodeLocation getDefaultLocation() {
92 return defaultLocation;
93 }
94
95 public AtomicReference<BranchEnd> getDefaultBranchEnd() {
96 return defaultBranchEnd;
97 }
98
99 public List<ValuePair> getValues() {
100 return Collections.unmodifiableList(values);
101 }
102
103 public int getLow() {
104 return low;
105 }
106
107 public int getHigh() {
108 return high;
109 }
110
111 public static class ValuePair {
112 private final CodeLocation location;
113 private final AtomicReference<BranchEnd> branchEnd;
114
115 public ValuePair(final AtomicReference<BranchEnd> branchEnd) {
116 this.location = null;
117 this.branchEnd = branchEnd;
118 }
119 public ValuePair(final CodeLocation location) {
120 this.location = location;
121 this.branchEnd = null;
122 }
123
124 public CodeLocation getLocation() {
125 return location;
126 }
127
128 public AtomicReference<BranchEnd> getBranchEnd() {
129 return branchEnd;
130 }
131 }
132 }