| 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.ClassCastStrategy; |
| 24 | import org.fuwjax.jon.IndirectType; |
| 25 | import org.fuwjax.jon.ObjectAccessException; |
| 26 | import org.fuwjax.jon.Reader; |
| 27 | import org.fuwjax.jon.ReferenceStrategy; |
| 28 | import org.fuwjax.jon.SerialFormatException; |
| 29 | import org.fuwjax.jon.Writer; |
| 30 | import org.fuwjax.jon.accessor.Literal; |
| 31 | import org.fuwjax.jon.accessor.LiteralAccessor; |
| 32 | import org.fuwjax.jon.accessor.Symbol; |
| 33 | |
| 34 | /** |
| 35 | * An abstract type to be used only for as long as we don't know the type of the |
| 36 | * object being serialized. |
| 37 | * @author michaeldoberenz |
| 38 | */ |
| 39 | public final class NullType implements IndirectType, LiteralAccessor{ |
| 40 | /** |
| 41 | * The singleton instance. |
| 42 | */ |
| 43 | public static final NullType DEFAULT = new NullType(); |
| 44 | private static final String NULL = "null"; //$NON-NLS-1$ |
| 45 | |
| 46 | private NullType(){ |
| 47 | // for singleton |
| 48 | } |
| 49 | |
| 50 | public IndirectType getActualType(final Object object){ |
| 51 | if(object == null){ |
| 52 | return this; |
| 53 | } |
| 54 | throw new ClassCastException(); |
| 55 | } |
| 56 | |
| 57 | public IndirectType getIndirectType(final Class<?> cls){ |
| 58 | if(cls == null){ |
| 59 | return this; |
| 60 | } |
| 61 | throw new ClassCastException(); |
| 62 | } |
| 63 | |
| 64 | public ClassCastStrategy getClassCastStrategy(){ |
| 65 | return ClassCastStrategy.NeverCast; |
| 66 | } |
| 67 | |
| 68 | public String getName(){ |
| 69 | throw new UnsupportedOperationException(); |
| 70 | } |
| 71 | |
| 72 | public ReferenceStrategy getReferenceStrategy(){ |
| 73 | return ReferenceStrategy.NeverReference; |
| 74 | } |
| 75 | |
| 76 | public Symbol getSymbolSpec(){ |
| 77 | return Symbol.Literal; |
| 78 | } |
| 79 | |
| 80 | public Literal getLiteral(){ |
| 81 | return Literal.Identifier; |
| 82 | } |
| 83 | |
| 84 | public CharSequence toString(final Object object) throws ObjectAccessException{ |
| 85 | if(object == null){ |
| 86 | return String.valueOf((Object)null); |
| 87 | } |
| 88 | throw ObjectAccessException.Message.NonNullNotInstanceOfNullType.exception(); |
| 89 | } |
| 90 | |
| 91 | public Object createInstance(final CharSequence parse, final Object source) throws ObjectAccessException{ |
| 92 | if(NULL.equals(parse.toString())){ |
| 93 | return null; |
| 94 | } |
| 95 | throw ObjectAccessException.Message.NonNullNotInstanceOfNullType.exception(); |
| 96 | } |
| 97 | |
| 98 | public Writer createWriter(final CachedAppendable appender){ |
| 99 | return new Writer(){ |
| 100 | public void write(final Object object) throws ObjectAccessException, SerialFormatException{ |
| 101 | Symbol.Element.write(appender, object, NullType.this); |
| 102 | } |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | public Reader createReader(final CachedLexable lexer){ |
| 107 | return new Reader(){ |
| 108 | public Object read() throws ObjectAccessException, SerialFormatException{ |
| 109 | return Symbol.Element.read(lexer, null, NullType.this); |
| 110 | } |
| 111 | |
| 112 | public Object read(final Object object) throws ObjectAccessException, SerialFormatException{ |
| 113 | return Symbol.Element.read(lexer, object, NullType.this); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | } |