1 /*
 2  * Copyright (c) 1996, 2019, 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 import java.util.List;
29 
30 /**
31  *
32  */
33 public class DataVector<T extends Data> implements Iterable<T> {
34 
35     ArrayList<T> elements;
36 
37     public DataVector(int initSize) {
38         elements = new ArrayList<>(initSize);
39     }
40 
41     public DataVector() {
42         this(12);
43     }
44 
45     public Iterator<T> iterator() {
46         return elements.iterator();
47     }
48 
49     public void add(T element) {
50         elements.add(element);
51     }
52 
53     public void addAll(List<T> collection) {
54         elements.addAll(collection);
55     }
56 
57     // full length of the attribute conveyor
58     // declared in Data
59     public int getLength() {
60         int length = 0;
61         // calculate overall size here rather than in add()
62         // because it may not be available at the time of invoking of add()
63         for (T element : elements) {
64             length += element.getLength();
65         }
66 
67         return 2 + length; // add the length of number of elements
68     }
69 
70     public void write(CheckedDataOutputStream out)
71             throws IOException {
72         out.writeShort(elements.size());
73         writeElements(out);
74     }
75 
76     public void writeElements(CheckedDataOutputStream out)
77             throws IOException {
78         for (Data element : elements) {
79             element.write(out);
80         }
81     }
82 
83     /* for compatibility with Vector */
84     public void addElement(T element) {
85         elements.add(element);
86     }
87 
88     public int size() {
89         return elements.size();
90     }
91 
92     public Data elementAt(int k) {
93         return elements.get(k);
94     }
95 }// end class DataVector
96