1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dx.cf.iface; 18 19 import com.android.dx.util.FixedSizeList; 20 21 /** 22 * Standard implementation of {@link AttributeList}, which directly stores 23 * an array of {@link Attribute} objects and can be made immutable. 24 */ 25 public final class StdAttributeList extends FixedSizeList 26 implements AttributeList { 27 /** 28 * Constructs an instance. All indices initially contain {@code null}. 29 * 30 * @param size the size of the list 31 */ StdAttributeList(int size)32 public StdAttributeList(int size) { 33 super(size); 34 } 35 36 /** {@inheritDoc} */ 37 @Override get(int n)38 public Attribute get(int n) { 39 return (Attribute) get0(n); 40 } 41 42 /** {@inheritDoc} */ 43 @Override byteLength()44 public int byteLength() { 45 int sz = size(); 46 int result = 2; // u2 attributes_count 47 48 for (int i = 0; i < sz; i++) { 49 result += get(i).byteLength(); 50 } 51 52 return result; 53 } 54 55 /** {@inheritDoc} */ 56 @Override findFirst(String name)57 public Attribute findFirst(String name) { 58 int sz = size(); 59 60 for (int i = 0; i < sz; i++) { 61 Attribute att = get(i); 62 if (att.getName().equals(name)) { 63 return att; 64 } 65 } 66 67 return null; 68 } 69 70 /** {@inheritDoc} */ 71 @Override findNext(Attribute attrib)72 public Attribute findNext(Attribute attrib) { 73 int sz = size(); 74 int at; 75 76 outer: { 77 for (at = 0; at < sz; at++) { 78 Attribute att = get(at); 79 if (att == attrib) { 80 break outer; 81 } 82 } 83 84 return null; 85 } 86 87 String name = attrib.getName(); 88 89 for (at++; at < sz; at++) { 90 Attribute att = get(at); 91 if (att.getName().equals(name)) { 92 return att; 93 } 94 } 95 96 return null; 97 } 98 99 /** 100 * Sets the attribute at the given index. 101 * 102 * @param n {@code >= 0, < size();} which attribute 103 * @param attribute {@code null-ok;} the attribute object 104 */ set(int n, Attribute attribute)105 public void set(int n, Attribute attribute) { 106 set0(n, attribute); 107 } 108 } 109