| 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.util.Collection; |
| 22 | |
| 23 | import org.fuwjax.jon.ClassCastStrategy; |
| 24 | import org.fuwjax.jon.IndirectType; |
| 25 | import org.fuwjax.jon.ObjectAccessException; |
| 26 | import org.fuwjax.jon.Reference; |
| 27 | import org.fuwjax.jon.ReferenceStrategy; |
| 28 | import org.fuwjax.jon.SerialFormatException; |
| 29 | import org.fuwjax.jon.accessor.ListAccessor; |
| 30 | import org.fuwjax.jon.accessor.ListReader; |
| 31 | import org.fuwjax.jon.accessor.ListWriter; |
| 32 | import org.fuwjax.jon.accessor.Symbol; |
| 33 | |
| 34 | /** |
| 35 | * An abstraction over {@link Collection} subclasses. |
| 36 | * @author michaeldoberenz |
| 37 | */ |
| 38 | public class ListType extends AbstractIndirectType implements ListAccessor{ |
| 39 | private IndirectType component; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new instance. |
| 43 | * @param cls the {@link Collection} subtype |
| 44 | */ |
| 45 | public ListType(final Class<?> cls){ |
| 46 | super(cls); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected void init(){ |
| 51 | component = getIndirectType(null); |
| 52 | } |
| 53 | |
| 54 | public ClassCastStrategy getClassCastStrategy(){ |
| 55 | return ClassCastStrategy.AlwaysCast; |
| 56 | } |
| 57 | |
| 58 | public ReferenceStrategy getReferenceStrategy(){ |
| 59 | return ReferenceStrategy.AssignedReference; |
| 60 | } |
| 61 | |
| 62 | public Symbol getSymbolSpec(){ |
| 63 | return Symbol.List; |
| 64 | } |
| 65 | |
| 66 | public void writeElements(final ListWriter writer, final Object container) throws ObjectAccessException, |
| 67 | SerialFormatException{ |
| 68 | for(Object member: (Collection<?>)container){ |
| 69 | writer.write(member, component); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public Object createContainer(final Object object) throws ObjectAccessException{ |
| 74 | if(object instanceof Collection){ |
| 75 | ((Collection<?>)object).clear(); |
| 76 | return object; |
| 77 | } |
| 78 | return NaiveInstantiator.create(getType()); |
| 79 | } |
| 80 | |
| 81 | public Object createInstance(final Object container, final Object object){ |
| 82 | return container; |
| 83 | } |
| 84 | |
| 85 | /* SW unchecked for generic collection cast */ |
| 86 | @SuppressWarnings("unchecked") |
| 87 | public void readElements(final ListReader reader, final Object object) throws ObjectAccessException, |
| 88 | SerialFormatException{ |
| 89 | final Collection<Object> list = (Collection<Object>)object; |
| 90 | boolean foundElement = true; |
| 91 | do{ |
| 92 | final int pos = reader.getPosition(); |
| 93 | try{ |
| 94 | final Object value = reader.read(null, component); |
| 95 | addToList(list, value); |
| 96 | }catch(SerialFormatException e){ |
| 97 | e.assertPosition(pos); |
| 98 | foundElement = false; |
| 99 | } |
| 100 | }while(foundElement); |
| 101 | } |
| 102 | |
| 103 | private void addToList(final Collection<Object> list, final Object value) throws ObjectAccessException{ |
| 104 | if(value instanceof Reference){ |
| 105 | ((Reference)value).addTo(list); |
| 106 | }else{ |
| 107 | list.add(value); |
| 108 | } |
| 109 | } |
| 110 | } |