| 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 | */ |
| 19 | package org.fuwjax.jon.type; |
| 20 | |
| 21 | import java.lang.reflect.Array; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import org.fuwjax.jon.ClassCastStrategy; |
| 26 | import org.fuwjax.jon.IndirectType; |
| 27 | import org.fuwjax.jon.ObjectAccessException; |
| 28 | import org.fuwjax.jon.Reference; |
| 29 | import org.fuwjax.jon.ReferenceStrategy; |
| 30 | import org.fuwjax.jon.SerialFormatException; |
| 31 | import org.fuwjax.jon.accessor.ListAccessor; |
| 32 | import org.fuwjax.jon.accessor.ListReader; |
| 33 | import org.fuwjax.jon.accessor.ListWriter; |
| 34 | import org.fuwjax.jon.accessor.Symbol; |
| 35 | |
| 36 | /** |
| 37 | * An abstraction layer over array classes. |
| 38 | * @author michaeldoberenz |
| 39 | */ |
| 40 | public 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 | } |