codeLayouts = new HashMap<>();
/**
* The order of the maps in this array should not be changed as their indices correspond to the value of their context constants
* (AttributeLayout.CONTEXT_CLASS etc.)
*
* All elements are of type {@code Map}.
*
*/
@SuppressWarnings("unchecked") // Cannot create a generic array
private final Map[] layouts = new Map[] { classLayouts, fieldLayouts, methodLayouts, codeLayouts };
private final Map layoutsToBands = new HashMap<>();
public AttributeLayoutMap() throws Pack200Exception {
for (final AttributeLayout defaultAttributeLayout : getDefaultAttributeLayouts()) {
add(defaultAttributeLayout);
}
}
public void add(final AttributeLayout layout) {
getLayout(layout.getContext()).put(Integer.valueOf(layout.getIndex()), layout);
}
public void add(final AttributeLayout layout, final NewAttributeBands newBands) {
add(layout);
layoutsToBands.put(layout, newBands);
}
/**
* The map should not contain the same layout and name combination more than once for each context.
*
* @throws Pack200Exception Thrown when the name layout/name combination exists twice for a context.
*/
public void checkMap() throws Pack200Exception {
for (final Map map : layouts) {
Collection c = map.values();
if (!(c instanceof List)) {
c = new ArrayList<>(c);
}
final List layouts = (List) c;
for (int j = 0; j < layouts.size(); j++) {
final AttributeLayout layout1 = layouts.get(j);
for (int j2 = j + 1; j2 < layouts.size(); j2++) {
final AttributeLayout layout2 = layouts.get(j2);
if (layout1.getName().equals(layout2.getName()) && layout1.getLayout().equals(layout2.getLayout())) {
throw new Pack200Exception("Same layout/name combination: " + layout1.getLayout() + "/" + layout1.getName()
+ " exists twice for context: " + AttributeLayout.contextNames[layout1.getContext()]);
}
}
}
}
}
public NewAttributeBands getAttributeBands(final AttributeLayout layout) {
return layoutsToBands.get(layout);
}
public AttributeLayout getAttributeLayout(final int index, final int context) {
final Map map = getLayout(context);
return map.get(Integer.valueOf(index));
}
public AttributeLayout getAttributeLayout(final String name, final int context) {
final Map map = getLayout(context);
for (final AttributeLayout layout : map.values()) {
if (layout.getName().equals(name)) {
return layout;
}
}
return null;
}
private Map getLayout(final int context) {
return layouts[context];
}
}