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: FieldCollection.java,v 1.1.1.1 2004/05/09 16:57:45 vlad_r Exp $
8 */
9package com.vladium.jcd.cls;
10
11import java.io.IOException;
12import java.util.ArrayList;
13import java.util.List;
14
15import com.vladium.jcd.lib.UDataOutputStream;
16import com.vladium.util.IntVector;
17
18// ----------------------------------------------------------------------------
19/**
20 * @author (C) 2001, Vlad Roubtsov
21 */
22final class FieldCollection implements IFieldCollection
23{
24    // public: ................................................................
25
26    // ACCESSORS:
27
28    public Field_info get (final int offset)
29    {
30        return (Field_info) m_fields.get (offset);
31    }
32
33    public int [] get (final ClassDef cls, final String name)
34    {
35        if (cls == null) throw new IllegalArgumentException  ("null input: cls");
36
37        // TODO: hash impl [not possible without having access to the parent ClassDef]
38
39        final int count = m_fields.size (); // use size() if class becomes non-final
40        final IntVector result = new IntVector (count);
41
42        for (int f = 0; f < count; ++ f)
43        {
44            final Field_info field = (Field_info) m_fields.get (f);
45
46            if (field.getName (cls).equals (name))
47                result.add (f);
48        }
49
50        return result.values (); // IntVector optimizes for the empty case
51    }
52
53    public int size ()
54    {
55        return m_fields.size ();
56    }
57
58    // Cloneable:
59
60    /**
61     * Performs a deep copy.
62     */
63    public Object clone ()
64    {
65        try
66        {
67            final FieldCollection _clone = (FieldCollection) super.clone ();
68
69            // deep clone:
70            final int fields_count = m_fields.size (); // use size() if class becomes non-final
71            _clone.m_fields = new ArrayList (fields_count);
72            for (int f = 0; f < fields_count; ++ f)
73            {
74                _clone.m_fields.add (((Field_info) m_fields.get (f)).clone ());
75            }
76
77            return _clone;
78        }
79        catch (CloneNotSupportedException e)
80        {
81            throw new InternalError (e.toString ());
82        }
83    }
84
85    // IClassFormatOutput:
86
87    public void writeInClassFormat (final UDataOutputStream out) throws IOException
88    {
89        final int fields_count = m_fields.size (); // use size() if class becomes non-final
90        out.writeU2 (fields_count);
91
92        for (int i = 0; i < fields_count; i++)
93        {
94            get (i).writeInClassFormat (out);
95        }
96    }
97
98    // Visitor:
99
100    public void accept (final IClassDefVisitor visitor, final Object ctx)
101    {
102        visitor.visit (this, ctx);
103    }
104
105
106    // MUTATORS:
107
108    public int add (final Field_info field)
109    {
110        final int newoffset = m_fields.size (); // use size() if class becomes non-final
111        m_fields.add (field);
112
113        return newoffset;
114    }
115
116    public Field_info set (final int offset, final Field_info field)
117    {
118        return (Field_info) m_fields.set (offset, field);
119    }
120
121    // protected: .............................................................
122
123    // package: ...............................................................
124
125
126    FieldCollection (final int capacity)
127    {
128        m_fields = capacity < 0 ? new ArrayList () : new ArrayList (capacity);
129    }
130
131    // private: ...............................................................
132
133
134    private List/* Field_info */ m_fields; // never null
135
136} // end of class
137// ----------------------------------------------------------------------------
138