diff --git a/src/main/java/com/exxeleron/qjava/DefaultQReader.java b/src/main/java/com/exxeleron/qjava/DefaultQReader.java index 162370c..0d91e15 100644 --- a/src/main/java/com/exxeleron/qjava/DefaultQReader.java +++ b/src/main/java/com/exxeleron/qjava/DefaultQReader.java @@ -362,8 +362,9 @@ protected QFunction readFunction( final QType qtype ) throws QException, IOExcep * @throws QReaderException */ public static Class getType( final QType type ) throws QReaderException { - if ( fromQ.containsKey(type) ) { - return fromQ.get(type); + Class aClass = fromQ.get(type); + if ( aClass != null ) { + return aClass; } else { throw new QReaderException("Cannot deserialize object of type: " + type); } diff --git a/src/main/java/com/exxeleron/qjava/DefaultQWriter.java b/src/main/java/com/exxeleron/qjava/DefaultQWriter.java index 7df3470..89a90ab 100644 --- a/src/main/java/com/exxeleron/qjava/DefaultQWriter.java +++ b/src/main/java/com/exxeleron/qjava/DefaultQWriter.java @@ -624,8 +624,11 @@ protected void writeProjection( final QProjection p ) throws IOException, QExcep public static QType getQType( final Object obj ) throws QWriterException { if ( obj == null ) { return QType.NULL_ITEM; - } else if ( toQ.containsKey(obj.getClass()) ) { - return toQ.get(obj.getClass()); + } + + QType qType = toQ.get(obj.getClass()); + if ( qType != null ) { + return qType; } else { throw new QWriterException("Cannot serialize object of type: " + obj.getClass().getCanonicalName()); } diff --git a/src/main/java/com/exxeleron/qjava/QType.java b/src/main/java/com/exxeleron/qjava/QType.java index 9dcac40..644d7af 100644 --- a/src/main/java/com/exxeleron/qjava/QType.java +++ b/src/main/java/com/exxeleron/qjava/QType.java @@ -117,16 +117,13 @@ public byte getTypeCode() { } }); - private static final Map lookup = Collections.unmodifiableMap(new HashMap() { - private static final long serialVersionUID = 7199217298785029441L; - - { - for ( final QType qtype : EnumSet.allOf(QType.class) ) { - put(qtype.getTypeCode(), qtype); - } + private static final int LOOKUP_SHIFT = 128; + private static final QType[] lookup = new QType[256]; + static { + for ( final QType qtype : EnumSet.allOf(QType.class) ) { + lookup[qtype.getTypeCode() + LOOKUP_SHIFT] = qtype; } - - }); + } /** * Returns {@link QType} based on type code identifier. @@ -138,8 +135,9 @@ public byte getTypeCode() { * in case q type code is unknown */ public static QType valueOf( final byte typecode ) { - if ( lookup.containsKey(typecode) ) { - return lookup.get(typecode); + QType qType = lookup[typecode + LOOKUP_SHIFT]; + if ( qType != null ) { + return qType; } else { throw new IllegalArgumentException("Invalid q type code: " + typecode); } @@ -153,9 +151,10 @@ public static QType valueOf( final byte typecode ) { * @return {@link QType} enum bound with type code identifier * @throws QReaderException */ - public static QType getQType( final Byte typecode ) throws QReaderException { - if ( lookup.containsKey(typecode) ) { - return lookup.get(typecode); + public static QType getQType( final byte typecode ) throws QReaderException { + QType qType = lookup[typecode + LOOKUP_SHIFT]; + if ( qType != null ) { + return qType; } else { throw new QReaderException("Cannot deserialize object of type: " + typecode); } @@ -170,8 +169,9 @@ public static QType getQType( final Byte typecode ) throws QReaderException { * @throws QException */ public static Object getQNull( final QType type ) throws QException { - if ( qNulls.containsKey(type) ) { - return qNulls.get(type); + Object obj = qNulls.get(type); + if ( obj != null ) { + return obj; } else { throw new QException("Cannot find null value of type: " + type); }