EMMA Coverage Report (generated Wed Aug 29 00:03:59 CDT 2007)
[all classes][org.fuwjax.jon.type]

COVERAGE SUMMARY FOR SOURCE FILE [ArrayType.java]

nameclass, %method, %block, %line, %
ArrayType.java100% (1/1)100% (10/10)100% (116/116)100% (38/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ArrayType100% (1/1)100% (10/10)100% (116/116)100% (38/38)
ArrayType (Class): void 100% (1/1)100% (4/4)100% (2/2)
createContainer (Object): Object 100% (1/1)100% (4/4)100% (1/1)
createInstance (Object, Object): Object 100% (1/1)100% (34/34)100% (10/10)
getClassCastStrategy (): ClassCastStrategy 100% (1/1)100% (2/2)100% (1/1)
getReferenceStrategy (): ReferenceStrategy 100% (1/1)100% (2/2)100% (1/1)
getSymbolSpec (): Symbol 100% (1/1)100% (2/2)100% (1/1)
init (): void 100% (1/1)100% (8/8)100% (2/2)
readElements (ListReader, Object): void 100% (1/1)100% (28/28)100% (11/11)
setArrayElement (Object, int, Object): void 100% (1/1)100% (14/14)100% (5/5)
writeElements (ListWriter, Object): void 100% (1/1)100% (18/18)100% (4/4)

1/*
2 * This file is part of JON.
3 *
4 * JON is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * JON is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 * 
17 * Copyright 2007 Michael Doberenz
18 */
19package org.fuwjax.jon.type;
20 
21import java.lang.reflect.Array;
22import java.util.ArrayList;
23import java.util.List;
24 
25import org.fuwjax.jon.ClassCastStrategy;
26import org.fuwjax.jon.IndirectType;
27import org.fuwjax.jon.ObjectAccessException;
28import org.fuwjax.jon.Reference;
29import org.fuwjax.jon.ReferenceStrategy;
30import org.fuwjax.jon.SerialFormatException;
31import org.fuwjax.jon.accessor.ListAccessor;
32import org.fuwjax.jon.accessor.ListReader;
33import org.fuwjax.jon.accessor.ListWriter;
34import org.fuwjax.jon.accessor.Symbol;
35 
36/**
37 * An abstraction layer over array classes.
38 * @author michaeldoberenz
39 */
40public class ArrayType extends AbstractIndirectType implements ListAccessor{
41        private IndirectType component;
42 
43        /**
44         * Creates an abstraction layer for <code>cls</code>.
45         * @param cls an array classtype
46         */
47        public ArrayType(final Class<?> cls){
48                super(cls);
49        }
50 
51        @Override
52        public void init(){
53                component = getIndirectType(getType().getComponentType());
54        }
55 
56        public ClassCastStrategy getClassCastStrategy(){
57                return ClassCastStrategy.AlwaysCast;
58        }
59 
60        public ReferenceStrategy getReferenceStrategy(){
61                return ReferenceStrategy.AssignedReference;
62        }
63 
64        public Symbol getSymbolSpec(){
65                return Symbol.List;
66        }
67 
68        public void writeElements(final ListWriter writer, final Object container) throws ObjectAccessException,
69              SerialFormatException{
70                for(int i = 0; i < Array.getLength(container); ++i){
71                        final Object elm = Array.get(container, i);
72                        writer.write(elm, component);
73                }
74        }
75 
76        /* SW unchecked for collection generic cast */
77        @SuppressWarnings("unchecked")
78        public void readElements(final ListReader reader, final Object object) throws ObjectAccessException,
79              SerialFormatException{
80                final List<Object> list = (List<Object>)object;
81                boolean foundElement = true;
82                do{
83                        final int pos = reader.getPosition();
84                        try{
85                                final Object value = reader.read(null, component);
86                                list.add(value);
87                        }catch(SerialFormatException e){
88                                e.assertPosition(pos);
89                                foundElement = false;
90                        }
91                }while(foundElement);
92        }
93 
94        public Object createContainer(final Object object) throws ObjectAccessException{
95                return new ArrayList<Object>();
96        }
97 
98        public Object createInstance(final Object container, final Object object) throws ObjectAccessException{
99                final List<?> list = (List<?>)container;
100                Object array = object;
101                if(array == null){
102                        array = Array.newInstance(getType().getComponentType(), list.size());
103                }
104                int index = 0;
105                for(Object value: list){
106                        setArrayElement(array, index, value);
107                        ++index;
108                }
109                return array;
110        }
111 
112        private void setArrayElement(final Object array, final int index, final Object value) throws ObjectAccessException{
113                if(value instanceof Reference){
114                        ((Reference)value).setArrayElement(array, index);
115                }else{
116                        Array.set(array, index, value);
117                }
118        }
119}

[all classes][org.fuwjax.jon.type]
EMMA 2.0.5312 (C) Vladimir Roubtsov