Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IndexedEntityCollectionInstantiatingReflector |
|
| 1.75;1.75 |
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 | package org.apache.commons.flatfile.morph; | |
18 | ||
19 | import net.sf.morph.reflect.InstantiatingReflector; | |
20 | import net.sf.morph.reflect.Reflector; | |
21 | import net.sf.morph.reflect.reflectors.BaseReflector; | |
22 | import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector; | |
23 | ||
24 | import org.apache.commons.flatfile.Entity; | |
25 | import org.apache.commons.flatfile.EntityArray; | |
26 | import org.apache.commons.flatfile.IndexedEntityCollection; | |
27 | ||
28 | /** | |
29 | * IndexedEntityCollection InstantiatingReflector. | |
30 | * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $ | |
31 | */ | |
32 | 0 | public class IndexedEntityCollectionInstantiatingReflector extends |
33 | BaseReflector implements InstantiatingReflector { | |
34 | ||
35 | // use a SimpleDelegatingReflector prepended with an | |
36 | // EntityCollectionReflector to get the | |
37 | // size and contained type of source objects when implementing | |
38 | // InstantiatingReflector | |
39 | 0 | private static final SimpleDelegatingReflector CONTAINER_REFLECTOR = new SimpleDelegatingReflector( |
40 | 0 | new Reflector[] { new EntityCollectionReflector() }, true); |
41 | ||
42 | private InstantiatingReflector componentReflector; | |
43 | ||
44 | /** | |
45 | * {@inheritDoc} | |
46 | */ | |
47 | @Override | |
48 | protected Class<?>[] getReflectableClassesImpl() throws Exception { | |
49 | 0 | return new Class[] { IndexedEntityCollection.class }; |
50 | } | |
51 | ||
52 | /** | |
53 | * Get the componentReflector. | |
54 | * @return InstantiatingReflector. | |
55 | */ | |
56 | public synchronized InstantiatingReflector getComponentReflector() { | |
57 | 0 | return componentReflector; |
58 | } | |
59 | ||
60 | /** | |
61 | * Set the componentReflector. Essential for use of this class as an InstantiatingReflector. | |
62 | * @param componentReflector The InstantiatingReflector componentReflector to set. | |
63 | */ | |
64 | public synchronized void setComponentReflector( | |
65 | InstantiatingReflector componentReflector) { | |
66 | 0 | this.componentReflector = componentReflector; |
67 | 0 | } |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | @Override | |
73 | protected Object newInstanceImpl(@SuppressWarnings("rawtypes") Class destType, Object source) | |
74 | throws Exception { | |
75 | 0 | int size = CONTAINER_REFLECTOR.getSize(source); |
76 | 0 | if (size == 0) { |
77 | 0 | return new EntityArray(0); |
78 | } | |
79 | 0 | Class<?> containedType = source == null ? null : CONTAINER_REFLECTOR |
80 | 0 | .getContainedType(source.getClass()); |
81 | 0 | Entity prototype = (Entity) getComponentReflector().newInstance( |
82 | 0 | destType, containedType); |
83 | 0 | return new EntityArray(prototype, size); |
84 | } | |
85 | ||
86 | } |