001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.messaging.jmf.codec.std.impl;
023
024import java.io.IOException;
025import java.io.OutputStream;
026
027import org.granite.messaging.jmf.DumpContext;
028import org.granite.messaging.jmf.InputContext;
029import org.granite.messaging.jmf.OutputContext;
030import org.granite.messaging.jmf.codec.std.FloatCodec;
031import org.granite.messaging.jmf.codec.std.impl.util.FloatUtil;
032
033/**
034 * @author Franck WOLFF
035 */
036public class FloatCodecImpl extends AbstractStandardCodec<Float> implements FloatCodec {
037
038        public int getObjectType() {
039                return JMF_FLOAT_OBJECT;
040        }
041
042        public Class<?> getObjectClass() {
043                return Float.class;
044        }
045
046        
047        public int getPrimitiveType() {
048                return JMF_FLOAT;
049        }
050
051        public Class<?> getPrimitiveClass() {
052                return float.class;
053        }
054
055        
056        public void encode(OutputContext ctx, Float v) throws IOException {
057                final OutputStream os = ctx.getOutputStream();
058                
059                os.write(JMF_FLOAT_OBJECT);
060                FloatUtil.encodeFloat(ctx, v.floatValue());
061        }
062        
063        public Float decode(InputContext ctx, int parameterizedJmfType) throws IOException {
064                return Float.valueOf(FloatUtil.decodeFloat(ctx));
065        }
066
067        
068        public void encodePrimitive(OutputContext ctx, float v) throws IOException {
069                final OutputStream os = ctx.getOutputStream();
070                
071                os.write(JMF_FLOAT);
072                FloatUtil.encodeFloat(ctx, v);
073        }
074        
075        public float decodePrimitive(InputContext ctx) throws IOException {
076                ctx.safeRead();
077                return FloatUtil.decodeFloat(ctx);
078        }
079
080        
081        public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
082                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
083                
084                switch (jmfType) {
085                case JMF_FLOAT:
086                        ctx.indentPrintLn("float: " + FloatUtil.decodeFloat(ctx));
087                        break;
088                case JMF_FLOAT_OBJECT:
089                        ctx.indentPrintLn(Float.class.getName() + ": " + Float.valueOf(FloatUtil.decodeFloat(ctx)));
090                        break;
091                default:
092                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
093                }
094        }
095}