1 /*
  2  * Copyright (c) 1996, 2014, 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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.asmtools.jasm;
 24 
 25 import java.io.IOException;
 26 import java.util.ArrayList;
 27 import java.util.Iterator;
 28 
 29 /**
 30  *
 31  */
 32 // public class DataVectorAttr extends AttrData implements Constants {
 33 // }
 34 class DataVectorAttr<T extends Data> extends AttrData implements Iterable<T> {
 35 
 36     private ArrayList<T> elements;
 37     private boolean byteIndex;
 38 
 39     private DataVectorAttr(ClassData cls, String name, boolean byteIndex, ArrayList<T> initialData) {
 40         super(cls, name);
 41         this.elements = initialData;
 42         this.byteIndex = byteIndex;
 43     }
 44 
 45     DataVectorAttr(ClassData cls, String name, ArrayList<T> initialData) {
 46         this(cls, name, false, initialData);
 47     }
 48 
 49     DataVectorAttr(ClassData cls, String name) {
 50         this(cls, name, false, new ArrayList<>());
 51 
 52     }
 53 
 54     DataVectorAttr(ClassData cls, String name, boolean byteIndex) {
 55         this(cls, name, byteIndex, new ArrayList<>());
 56 
 57     }
 58 
 59     public T get(int index) {
 60         return elements.get(index);
 61     }
 62 
 63     public void add(T element) {
 64         elements.add(element);
 65     }
 66 
 67     public void put(int i, T element) {
 68         elements.set(i, element);
 69     }
 70 
 71     public int size() {
 72         return elements.size();
 73     }
 74 
 75     @Override
 76     public Iterator<T> iterator() {
 77         return elements.iterator();
 78     }
 79 
 80     @Override
 81     public int attrLength() {
 82         int length = 0;
 83         // calculate overall size here rather than in add()
 84         // because it may not be available at the time of invoking of add()
 85         for (T elem : elements) {
 86             length += elem.getLength();
 87         }
 88 
 89         // add the length of number of elements
 90         if (byteIndex) {
 91             length += 1;
 92         } else {
 93             length += 2;
 94         }
 95 
 96         return length;
 97     }
 98 
 99     @Override
100     public void write(CheckedDataOutputStream out) throws IOException {
101         super.write(out);  // attr name, attr len
102         if (byteIndex) {
103             out.writeByte(elements.size());
104         } else {
105             out.writeShort(elements.size());
106         } // number of elements
107         for (T elem : elements) {
108             elem.write(out);
109         }
110     }
111 
112 }