1 /******************************************************************************* 2 * Copyright (c) 2015 LegSem. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the GNU Lesser Public License v2.1 5 * which accompanies this distribution, and is available at 6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 7 * 8 * Contributors: 9 * LegSem - initial API and implementation 10 ******************************************************************************/ 11 package com.legstar.coxb.impl; 12 13 import com.legstar.coxb.CobolElement; 14 import com.legstar.coxb.ICobolComplexBinding; 15 import com.legstar.coxb.ICobolPackedDecimalBinding; 16 import com.legstar.coxb.CobolElementVisitor; 17 import com.legstar.coxb.host.HostException; 18 19 /** 20 * This class implements the behavior of a packed decimal cobol element bound to 21 * a JAXB BigDecimal property. 22 * 23 * @author Fady Moussallam 24 * 25 */ 26 public class CPackedDecimalBinding 27 extends AbstractNumericBinding 28 implements ICobolPackedDecimalBinding { 29 30 /** 31 * Constructor for a cobol element to java binding. 32 * 33 * @param bindingName the identifier for this binding 34 * @param jaxbName the name of the bound java property 35 * @param jaxbType the type of the bound java property 36 * @param cobolAnnotations the cobol annotations for this element 37 * @param parentBinding a reference to the parent binding 38 */ 39 public CPackedDecimalBinding( 40 final String bindingName, 41 final String jaxbName, 42 final Class < ? > jaxbType, 43 final CobolElement cobolAnnotations, 44 final ICobolComplexBinding parentBinding) { 45 super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding); 46 } 47 48 /** {@inheritDoc} */ 49 public void accept(final CobolElementVisitor cev) 50 throws HostException { 51 cev.visit(this); 52 } 53 54 /** {@inheritDoc} */ 55 public int calcByteLength() { 56 return calcPackedDecimalByteLength(getTotalDigits()); 57 } 58 59 /** 60 * Calculates the host byte length for a COMP-3. 61 * Every couple of digits is encoded in one byte. The sign is encoded 62 * in the last half byte. 63 * @param totalDigits the number of digits (including fraction digits) 64 * @return the host byte length for a COMP-3 65 */ 66 public static int calcPackedDecimalByteLength(final int totalDigits) { 67 return (totalDigits / 2) + 1; 68 } 69 }