001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 the ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.serialization.objects; 018 019import java.io.DataOutputStream; 020import java.io.IOException; 021import java.io.ObjectStreamConstants; 022 023import org.conqat.lib.commons.serialization.SerializedEntityParser; 024import org.conqat.lib.commons.serialization.SerializedEntityPool; 025import org.conqat.lib.commons.serialization.SerializedEntitySerializer; 026 027/** 028 * A serialized enum literal. 029 */ 030public class SerializedEnumLiteral extends SerializedObjectBase { 031 032 /** The name of the enum literal. */ 033 private final String literalName; 034 035 /** Constructor from parser. */ 036 public SerializedEnumLiteral(SerializedEntityPool pool, SerializedEntityParser parser, int classHandle) 037 throws IOException { 038 super(pool, classHandle); 039 literalName = parser.parseStringObject().getValue(); 040 } 041 042 /** Constructor with explicit value. */ 043 public SerializedEnumLiteral(SerializedEntityPool pool, int classHandle, String literalName) { 044 super(pool, classHandle); 045 this.literalName = literalName; 046 } 047 048 /** {@inheritDoc} */ 049 @Override 050 protected byte getObjectTagConstant() { 051 return ObjectStreamConstants.TC_ENUM; 052 } 053 054 /** {@inheritDoc} */ 055 @Override 056 protected void serializeObjectContent(DataOutputStream dos, SerializedEntitySerializer serializer) 057 throws IOException { 058 serializer.serializeStringObject(literalName, false); 059 } 060 061 /** Returns the name of the literal. */ 062 public String getLiteralName() { 063 return literalName; 064 } 065 066 /** 067 * Creates a new literal for the given enum value with the given class and 068 * returns a handle to the new object. 069 */ 070 public static <E extends Enum<E>> int createLiteral(SerializedEntityPool entityPool, int enumClassHandle, E value) { 071 SerializedEnumLiteral enumLiteral = new SerializedEnumLiteral(entityPool, enumClassHandle, value.name()); 072 return enumLiteral.getHandle(); 073 } 074}