| 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 org.fuwjax.jon.CachedAppendable; |
| 22 | import org.fuwjax.jon.CachedLexable; |
| 23 | import org.fuwjax.jon.IndirectType; |
| 24 | import org.fuwjax.jon.IndirectTypeLibrary; |
| 25 | import org.fuwjax.jon.ObjectAccessException; |
| 26 | import org.fuwjax.jon.Reader; |
| 27 | import org.fuwjax.jon.SerialFormatException; |
| 28 | import org.fuwjax.jon.Writer; |
| 29 | import org.fuwjax.jon.accessor.Symbol; |
| 30 | |
| 31 | /** |
| 32 | * A base class for {@link IndirectType}. |
| 33 | * @author michaeldoberenz |
| 34 | */ |
| 35 | public abstract class AbstractIndirectType implements IndirectType{ |
| 36 | private Class<?> cls; |
| 37 | private IndirectTypeLibrary library; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new instance. |
| 41 | * @param cls class this indirect type is referencing |
| 42 | */ |
| 43 | protected AbstractIndirectType(final Class<?> cls){ |
| 44 | this.cls = cls; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets the library which contains this type. |
| 49 | * @param library the library which produced this type |
| 50 | */ |
| 51 | public final void setLibrary(final IndirectTypeLibrary library){ |
| 52 | this.library = library; |
| 53 | init(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Callback for subclass initialization. Called after this class has been |
| 58 | * added to its parent library. |
| 59 | */ |
| 60 | protected void init(){ |
| 61 | // do nothing by default |
| 62 | } |
| 63 | |
| 64 | public IndirectType getActualType(final Object object) throws ClassCastException{ |
| 65 | if(cls.equals(object.getClass())){ |
| 66 | return this; |
| 67 | } |
| 68 | return library.getTypeFor(object); |
| 69 | } |
| 70 | |
| 71 | public String getName(){ |
| 72 | return cls.getCanonicalName(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the class instance backing this indirect type. |
| 77 | * @return the class instance backing this indirect type |
| 78 | */ |
| 79 | public Class<?> getType(){ |
| 80 | return cls; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Deduces the indirect type for <code>type</code> from the library. |
| 85 | * @param type the class instance whose indirect type is deduced |
| 86 | * @return the deduced indirect type |
| 87 | */ |
| 88 | public IndirectType getIndirectType(final Class<?> type){ |
| 89 | return library.getType(type); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Deduces the indirect type for the class of <code>object</code> from the |
| 94 | * library. |
| 95 | * @param object the object whose class the indirect type is deduced from |
| 96 | * @return the deduced indirect type |
| 97 | */ |
| 98 | protected IndirectType getIndirectTypeFor(final Object object){ |
| 99 | return library.getTypeFor(object); |
| 100 | } |
| 101 | |
| 102 | public Writer createWriter(final CachedAppendable appender){ |
| 103 | return new Writer(){ |
| 104 | public void write(final Object object) throws ObjectAccessException, SerialFormatException{ |
| 105 | Symbol.Element.write(appender, object, AbstractIndirectType.this); |
| 106 | } |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | public Reader createReader(final CachedLexable lexer){ |
| 111 | return new Reader(){ |
| 112 | public Object read() throws ObjectAccessException, SerialFormatException{ |
| 113 | return Symbol.Element.read(lexer, null, AbstractIndirectType.this); |
| 114 | } |
| 115 | |
| 116 | public Object read(final Object object) throws ObjectAccessException, SerialFormatException{ |
| 117 | return Symbol.Element.read(lexer, object, AbstractIndirectType.this); |
| 118 | } |
| 119 | }; |
| 120 | } |
| 121 | } |