1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.io;
19
20/**
21 * An EmulatedFieldsForLoading is an object that represents a set of emulated
22 * fields for an object being loaded. It is a concrete implementation for
23 * ObjectInputStream.GetField
24 *
25 * @see ObjectInputStream.GetField
26 * @see EmulatedFieldsForDumping
27 */
28class EmulatedFieldsForLoading extends ObjectInputStream.GetField {
29
30    // The class descriptor with the declared fields the receiver emulates
31    private ObjectStreamClass streamClass;
32
33    // The actual representation, with a more powerful API (set&get)
34    private EmulatedFields emulatedFields;
35
36    /**
37     * Constructs a new instance of EmulatedFieldsForDumping.
38     *
39     * @param streamClass
40     *            an ObjectStreamClass, defining the class for which to emulate
41     *            fields.
42     */
43    EmulatedFieldsForLoading(ObjectStreamClass streamClass) {
44        this.streamClass = streamClass;
45        emulatedFields = new EmulatedFields(streamClass.getLoadFields(), streamClass.fields());
46    }
47
48    /**
49     * Return a boolean indicating if the field named <code>name</code> has
50     * been assigned a value explicitly (false) or if it still holds a default
51     * value for the type (true) because it hasn't been assigned to yet.
52     *
53     * @param name
54     *            A String, the name of the field to test
55     * @return <code>true</code> if the field holds it default value,
56     *         <code>false</code> otherwise.
57     *
58     * @throws IOException
59     *             If an IO error occurs
60     * @throws IllegalArgumentException
61     *             If the corresponding field can not be found.
62     */
63    @Override
64    public boolean defaulted(String name) throws IOException,
65            IllegalArgumentException {
66        return emulatedFields.defaulted(name);
67    }
68
69    /**
70     * Return the actual EmulatedFields instance used by the receiver. We have
71     * the actual work in a separate class so that the code can be shared. The
72     * receiver has to be of a subclass of GetField.
73     *
74     * @return array of ObjectSlot the receiver represents.
75     */
76    EmulatedFields emulatedFields() {
77        return emulatedFields;
78    }
79
80    /**
81     * Find and return the byte value of a given field named <code>name</code>
82     * in the receiver. If the field has not been assigned any value yet, the
83     * default value <code>defaultValue</code> is returned instead.
84     *
85     * @param name
86     *            A String, the name of the field to find
87     * @param defaultValue
88     *            Return value in case the field has not been assigned to yet.
89     * @return the value of the given field if it has been assigned, or the
90     *         default value otherwise
91     *
92     * @throws IOException
93     *             If an IO error occurs
94     * @throws IllegalArgumentException
95     *             If the corresponding field can not be found.
96     */
97    @Override
98    public byte get(String name, byte defaultValue) throws IOException,
99            IllegalArgumentException {
100        return emulatedFields.get(name, defaultValue);
101    }
102
103    /**
104     * Find and return the char value of a given field named <code>name</code>
105     * in the receiver. If the field has not been assigned any value yet, the
106     * default value <code>defaultValue</code> is returned instead.
107     *
108     * @param name
109     *            A String, the name of the field to find
110     * @param defaultValue
111     *            Return value in case the field has not been assigned to yet.
112     * @return the value of the given field if it has been assigned, or the
113     *         default value otherwise
114     *
115     * @throws IOException
116     *             If an IO error occurs
117     * @throws IllegalArgumentException
118     *             If the corresponding field can not be found.
119     */
120    @Override
121    public char get(String name, char defaultValue) throws IOException,
122            IllegalArgumentException {
123        return emulatedFields.get(name, defaultValue);
124    }
125
126    /**
127     * Find and return the double value of a given field named <code>name</code>
128     * in the receiver. If the field has not been assigned any value yet, the
129     * default value <code>defaultValue</code> is returned instead.
130     *
131     * @param name
132     *            A String, the name of the field to find
133     * @param defaultValue
134     *            Return value in case the field has not been assigned to yet.
135     * @return the value of the given field if it has been assigned, or the
136     *         default value otherwise
137     *
138     * @throws IOException
139     *             If an IO error occurs
140     * @throws IllegalArgumentException
141     *             If the corresponding field can not be found.
142     */
143    @Override
144    public double get(String name, double defaultValue) throws IOException,
145            IllegalArgumentException {
146        return emulatedFields.get(name, defaultValue);
147    }
148
149    /**
150     * Find and return the float value of a given field named <code>name</code>
151     * in the receiver. If the field has not been assigned any value yet, the
152     * default value <code>defaultValue</code> is returned instead.
153     *
154     * @param name
155     *            A String, the name of the field to find
156     * @param defaultValue
157     *            Return value in case the field has not been assigned to yet.
158     * @return the value of the given field if it has been assigned, or the
159     *         default value otherwise
160     *
161     * @throws IOException
162     *             If an IO error occurs
163     * @throws IllegalArgumentException
164     *             If the corresponding field can not be found.
165     */
166    @Override
167    public float get(String name, float defaultValue) throws IOException,
168            IllegalArgumentException {
169        return emulatedFields.get(name, defaultValue);
170    }
171
172    /**
173     * Find and return the int value of a given field named <code>name</code>
174     * in the receiver. If the field has not been assigned any value yet, the
175     * default value <code>defaultValue</code> is returned instead.
176     *
177     * @param name
178     *            A String, the name of the field to find
179     * @param defaultValue
180     *            Return value in case the field has not been assigned to yet.
181     * @return the value of the given field if it has been assigned, or the
182     *         default value otherwise
183     *
184     * @throws IOException
185     *             If an IO error occurs
186     * @throws IllegalArgumentException
187     *             If the corresponding field can not be found.
188     */
189    @Override
190    public int get(String name, int defaultValue) throws IOException,
191            IllegalArgumentException {
192        return emulatedFields.get(name, defaultValue);
193    }
194
195    /**
196     * Find and return the long value of a given field named <code>name</code>
197     * in the receiver. If the field has not been assigned any value yet, the
198     * default value <code>defaultValue</code> is returned instead.
199     *
200     * @param name
201     *            A String, the name of the field to find
202     * @param defaultValue
203     *            Return value in case the field has not been assigned to yet.
204     * @return the value of the given field if it has been assigned, or the
205     *         default value otherwise
206     *
207     * @throws IOException
208     *             If an IO error occurs
209     * @throws IllegalArgumentException
210     *             If the corresponding field can not be found.
211     */
212    @Override
213    public long get(String name, long defaultValue) throws IOException,
214            IllegalArgumentException {
215        return emulatedFields.get(name, defaultValue);
216    }
217
218    /**
219     * Find and return the Object value of a given field named <code>name</code>
220     * in the receiver. If the field has not been assigned any value yet, the
221     * default value <code>defaultValue</code> is returned instead.
222     *
223     * @param name
224     *            A String, the name of the field to find
225     * @param defaultValue
226     *            Return value in case the field has not been assigned to yet.
227     * @return the value of the given field if it has been assigned, or the
228     *         default value otherwise
229     *
230     * @throws IOException
231     *             If an IO error occurs
232     * @throws IllegalArgumentException
233     *             If the corresponding field can not be found.
234     */
235    @Override
236    public Object get(String name, Object defaultValue) throws IOException,
237            IllegalArgumentException {
238        return emulatedFields.get(name, defaultValue);
239    }
240
241    /**
242     * Find and return the short value of a given field named <code>name</code>
243     * in the receiver. If the field has not been assigned any value yet, the
244     * default value <code>defaultValue</code> is returned instead.
245     *
246     * @param name
247     *            A String, the name of the field to find
248     * @param defaultValue
249     *            Return value in case the field has not been assigned to yet.
250     * @return the value of the given field if it has been assigned, or the
251     *         default value otherwise
252     *
253     * @throws IOException
254     *             If an IO error occurs
255     * @throws IllegalArgumentException
256     *             If the corresponding field can not be found.
257     */
258    @Override
259    public short get(String name, short defaultValue) throws IOException,
260            IllegalArgumentException {
261        return emulatedFields.get(name, defaultValue);
262    }
263
264    /**
265     * Find and return the boolean value of a given field named
266     * <code>name</code> in the receiver. If the field has not been assigned
267     * any value yet, the default value <code>defaultValue</code> is returned
268     * instead.
269     *
270     * @param name
271     *            A String, the name of the field to find
272     * @param defaultValue
273     *            Return value in case the field has not been assigned to yet.
274     * @return the value of the given field if it has been assigned, or the
275     *         default value otherwise
276     *
277     * @throws IOException
278     *             If an IO error occurs
279     * @throws IllegalArgumentException
280     *             If the corresponding field can not be found.
281     */
282    @Override
283    public boolean get(String name, boolean defaultValue) throws IOException,
284            IllegalArgumentException {
285        return emulatedFields.get(name, defaultValue);
286    }
287
288    /**
289     * Return the class descriptor for which the emulated fields are defined.
290     *
291     * @return ObjectStreamClass The class descriptor for which the emulated
292     *         fields are defined.
293     */
294    @Override
295    public ObjectStreamClass getObjectStreamClass() {
296        return streamClass;
297    }
298}
299