Debian Codegraph @

src/main/java/org/jboss/classfilewriter/code/LookupSwitchBuilder.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 lookupswitch statement.
28 *
29 * @author Stuart Douglas
30 */
31 public class LookupSwitchBuilder {
32
33 private final CodeLocation defaultLocation;
34 private final AtomicReference<BranchEnd> defaultBranchEnd;
35 private final List<ValuePair> values = new ArrayList<ValuePair>();
36
37 /**
38 * Builds a lookup switch statement with no specified default location.
39 *
40 * When the lookup switch is added to the code attribute a {@link BranchEnd} will be returned that can be used to
41 * set the location.
42 */
43 public LookupSwitchBuilder() {
44 defaultBranchEnd = new AtomicReference<BranchEnd>();
45 defaultLocation = null;
46 }
47
48 /**
49 * Builds a lookup switch statement, specifying the default location
50 * @param defaultLocation The default location
51 */
52 public LookupSwitchBuilder(final CodeLocation defaultLocation) {
53 this.defaultLocation = defaultLocation;
54 defaultBranchEnd = null;
55 }
56
57 /**
58 * Adds a value to the table that is at a location yet to be written.
59 *
60 * After this lookup switch has been written then the BranchEnd can be retrieved
61 * from the returned reference.
62 *
63 * @param value The value to add to the lookup table
64 * @return A reference to the BranchEnd that will be created.
65 */
66 public AtomicReference<BranchEnd> add(int value) {
67 final AtomicReference<BranchEnd> end = new AtomicReference<BranchEnd>();
68 ValuePair vp = new ValuePair(value, end);
69 values.add(vp);
70 return end;
71 }
72 /**
73 * Adds a value to the table
74 *
75 *
76 * @param value The value to add to the lookup table
77 */
78 public LookupSwitchBuilder add(int value, final CodeLocation location) {
79 values.add(new ValuePair(value, location));
80 return this;
81 }
82
83 public CodeLocation getDefaultLocation() {
84 return defaultLocation;
85 }
86
87 public AtomicReference<BranchEnd> getDefaultBranchEnd() {
88 return defaultBranchEnd;
89 }
90
91 public List<ValuePair> getValues() {
92 return Collections.unmodifiableList(values);
93 }
94
95 public static class ValuePair implements Comparable<ValuePair> {
96 private final int value;
97 private final CodeLocation location;
98 private final AtomicReference<BranchEnd> branchEnd;
99
100 public ValuePair(final int value, final AtomicReference<BranchEnd> branchEnd) {
101 this.value = value;
102 this.location = null;
103 this.branchEnd = branchEnd;
104 }
105 public ValuePair(final int value, final CodeLocation location) {
106 this.value = value;
107 this.location = location;
108 this.branchEnd = null;
109 }
110
111 @Override
112 public int compareTo(final ValuePair o) {
113 return Integer.valueOf(value).compareTo(Integer.valueOf(o.value));
114 }
115
116 public int getValue() {
117 return value;
118 }
119
120 public CodeLocation getLocation() {
121 return location;
122 }
123
124 public AtomicReference<BranchEnd> getBranchEnd() {
125 return branchEnd;
126 }
127 }
128 }