1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2 *
3 * This program and the accompanying materials are made available under
4 * the terms of the Common Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * $Id: InterfaceCollection.java,v 1.1.1.1 2004/05/09 16:57:46 vlad_r Exp $
8 */
9package com.vladium.jcd.cls;
10
11import java.io.IOException;
12
13import com.vladium.jcd.lib.UDataOutputStream;
14import com.vladium.util.IntVector;
15
16// ----------------------------------------------------------------------------
17/**
18 * @author (C) 2001, Vlad Roubtsov
19 */
20final class InterfaceCollection implements IInterfaceCollection
21{
22    // public: ................................................................
23
24    // ACCESSORS:
25
26    public int get (final int offset)
27    {
28        return m_interfaces.get (offset);
29    }
30
31    public int size ()
32    {
33        return m_interfaces.size ();
34    }
35
36    // Cloneable:
37
38    /**
39     * Performs a deep copy.
40     */
41    public Object clone ()
42    {
43        try
44        {
45            final InterfaceCollection _clone = (InterfaceCollection) super.clone ();
46
47            // deep clone:
48            _clone.m_interfaces = (IntVector) m_interfaces.clone ();
49
50            return _clone;
51        }
52        catch (CloneNotSupportedException e)
53        {
54            throw new InternalError (e.toString ());
55        }
56    }
57
58    // IClassFormatOutput:
59
60    public void writeInClassFormat (final UDataOutputStream out) throws IOException
61    {
62        int _interfaces_count = m_interfaces.size (); // use size() if class becomes non-final
63        out.writeU2 (_interfaces_count);
64
65        for (int i = 0; i < _interfaces_count; i++)
66        {
67            out.writeU2 (get (i));
68        }
69    }
70
71    // Visitor:
72
73    public void accept (final IClassDefVisitor visitor, final Object ctx)
74    {
75        visitor.visit (this, ctx);
76    }
77
78
79    // MUTATORS:
80
81    public int add (final int interface_index)
82    {
83        final int newoffset = m_interfaces.size (); // use size() if class becomes non-final
84        m_interfaces.add (interface_index);
85
86        return newoffset;
87    }
88
89    public int set (final int offset, final int interface_index)
90    {
91        return m_interfaces.set (offset, interface_index);
92    }
93
94    // protected: .............................................................
95
96    // package: ...............................................................
97
98
99    InterfaceCollection (final int capacity)
100    {
101        m_interfaces = capacity < 0 ? new IntVector () : new IntVector (capacity);
102    }
103
104    // private: ...............................................................
105
106
107    private IntVector m_interfaces; // vector of constant pool indices
108
109} // end of class
110// ----------------------------------------------------------------------------
111