| 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.Map; |
| 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.EntriesAccessor; |
| 30 | import org.fuwjax.jon.accessor.KeyValueReader; |
| 31 | import org.fuwjax.jon.accessor.KeyValueWriter; |
| 32 | import org.fuwjax.jon.accessor.ListReader; |
| 33 | import org.fuwjax.jon.accessor.ListWriter; |
| 34 | import org.fuwjax.jon.accessor.ObjectAccessor; |
| 35 | import org.fuwjax.jon.accessor.Symbol; |
| 36 | |
| 37 | /** |
| 38 | * An abstraction over {@link Map} subclasses. |
| 39 | * @author michaeldoberenz |
| 40 | */ |
| 41 | public class MapType extends AbstractIndirectType implements EntriesAccessor, ObjectAccessor{ |
| 42 | private IndirectType keyType; |
| 43 | private IndirectType valueType; |
| 44 | |
| 45 | /** |
| 46 | * Creates a new instance. |
| 47 | * @param cls the {@link Map} subtype |
| 48 | */ |
| 49 | public MapType(final Class<?> cls){ |
| 50 | super(cls); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void init(){ |
| 55 | keyType = getIndirectType(null); |
| 56 | valueType = getIndirectType(null); |
| 57 | } |
| 58 | |
| 59 | public ClassCastStrategy getClassCastStrategy(){ |
| 60 | return ClassCastStrategy.AlwaysCast; |
| 61 | } |
| 62 | |
| 63 | public ReferenceStrategy getReferenceStrategy(){ |
| 64 | return ReferenceStrategy.AssignedReference; |
| 65 | } |
| 66 | |
| 67 | public Symbol getSymbolSpec(){ |
| 68 | return Symbol.Object; |
| 69 | } |
| 70 | |
| 71 | public void writeEntries(final KeyValueWriter writer, final Object container) throws ObjectAccessException, |
| 72 | SerialFormatException{ |
| 73 | for(Map.Entry<?, ?> entry: ((Map<?, ?>)container).entrySet()){ |
| 74 | writer.writeKey(entry.getKey(), keyType); |
| 75 | writer.writeValue(entry.getValue(), valueType); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public void writePartitions(final ListWriter writer, final Object object) throws ObjectAccessException, |
| 80 | SerialFormatException{ |
| 81 | writer.write(object, this); |
| 82 | } |
| 83 | |
| 84 | public Object createContainer(final Object object) throws ObjectAccessException{ |
| 85 | if(object != null){ |
| 86 | ((Map<?, ?>)object).clear(); |
| 87 | return object; |
| 88 | } |
| 89 | return NaiveInstantiator.create(getType()); |
| 90 | } |
| 91 | |
| 92 | public Object createInstance(final Object container, final Object object){ |
| 93 | return container; |
| 94 | } |
| 95 | |
| 96 | public void readPartitions(final ListReader reader, final Object container) throws ObjectAccessException, |
| 97 | SerialFormatException{ |
| 98 | reader.read(container, this); |
| 99 | } |
| 100 | |
| 101 | /* SW unchecked from generic collection cast */ |
| 102 | @SuppressWarnings("unchecked") |
| 103 | public void readEntries(final KeyValueReader reader, final Object container) throws ObjectAccessException, |
| 104 | SerialFormatException{ |
| 105 | final Map<Object, Object> map = (Map<Object, Object>)container; |
| 106 | boolean entryFound; |
| 107 | do{ |
| 108 | entryFound = readEntry(reader, map); |
| 109 | }while(entryFound); |
| 110 | } |
| 111 | |
| 112 | private boolean readEntry(final KeyValueReader reader, final Map<Object, Object> map) throws ObjectAccessException, |
| 113 | SerialFormatException{ |
| 114 | final int pos = reader.getPosition(); |
| 115 | try{ |
| 116 | final Object key = reader.readKey(null, keyType); |
| 117 | final Object value = reader.readValue(null, valueType); |
| 118 | Reference.putTo(map, key, value); |
| 119 | return true; |
| 120 | }catch(SerialFormatException e){ |
| 121 | e.assertPosition(pos); |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | } |