| 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; |
| 20 | |
| 21 | import org.fuwjax.jon.accessor.Symbol; |
| 22 | import org.fuwjax.jon.type.AbstractIndirectType; |
| 23 | import org.fuwjax.jon.type.ClassType; |
| 24 | import org.fuwjax.jon.type.PrimitiveType; |
| 25 | |
| 26 | /** |
| 27 | * Determines the class cast strategy. |
| 28 | * @author michaeldoberenz |
| 29 | */ |
| 30 | public enum ClassCastStrategy{ |
| 31 | /** |
| 32 | * Always serialize a cast to the writer. |
| 33 | */ |
| 34 | AlwaysCast{ |
| 35 | @Override |
| 36 | public void write(final CachedAppendable writer, final IndirectType actualType, final IndirectType expectedType) |
| 37 | throws ObjectAccessException, SerialFormatException{ |
| 38 | if(!expectedType.equals(actualType) && actualType instanceof AbstractIndirectType){ |
| 39 | Symbol.ClassCast.write(writer, ((AbstractIndirectType)actualType).getType(), ClassType.DEFAULT); |
| 40 | } |
| 41 | } |
| 42 | }, |
| 43 | /** |
| 44 | * Call an explicit method to perform tail-casting. |
| 45 | */ |
| 46 | PrimitiveCast{ |
| 47 | @Override |
| 48 | public void write(final CachedAppendable writer, final IndirectType actualType, final IndirectType expectedType) |
| 49 | throws ObjectAccessException, SerialFormatException{ |
| 50 | if(!expectedType.equals(actualType) && actualType instanceof PrimitiveType){ |
| 51 | ((PrimitiveType)actualType).castNext(expectedType); |
| 52 | } |
| 53 | } |
| 54 | }, |
| 55 | /** |
| 56 | * Never serialize a cast to the writer. |
| 57 | */ |
| 58 | NeverCast{ |
| 59 | @Override |
| 60 | public void write(final CachedAppendable writer, final IndirectType actualType, final IndirectType expectedType) |
| 61 | throws ObjectAccessException, SerialFormatException{ |
| 62 | // write nothing |
| 63 | } |
| 64 | }; |
| 65 | /** |
| 66 | * Writes the type name to the writer according to the strategy definition. |
| 67 | * @param writer the destination for the class cast |
| 68 | * @param actualType the type the source object is casting to |
| 69 | * @param expectedType the type the source object is casting from. |
| 70 | * @throws ObjectAccessException if an object relationship cannot be followed |
| 71 | * @throws SerialFormatException if the JON format cannot be preserved |
| 72 | */ |
| 73 | public abstract void write(CachedAppendable writer, IndirectType actualType, IndirectType expectedType) |
| 74 | throws ObjectAccessException, SerialFormatException; |
| 75 | |
| 76 | /** |
| 77 | * Reads an indirect type from <code>lexer</code>. If there is not a type |
| 78 | * on the input, <code>defaultType</code> is returned. |
| 79 | * @param lexer the input for the read operation |
| 80 | * @param defaultType the default type |
| 81 | * @return the type read from the input, or <code>defaultType</code> if no |
| 82 | * type is found in the input |
| 83 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 84 | * @throws SerialFormatException if the JON format has not been observed |
| 85 | */ |
| 86 | public static IndirectType read(final CachedLexable lexer, final IndirectType defaultType) |
| 87 | throws ObjectAccessException, SerialFormatException{ |
| 88 | final int pos = lexer.getPosition(); |
| 89 | try{ |
| 90 | return (IndirectType)Symbol.ClassCast.read(lexer, null, ClassType.DEFAULT); |
| 91 | }catch(SerialFormatException e){ |
| 92 | e.assertPosition(pos); |
| 93 | return defaultType; |
| 94 | } |
| 95 | } |
| 96 | } |