View Javadoc

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.ICobolArrayOctetStreamBinding;
15  import com.legstar.coxb.ICobolComplexBinding;
16  import com.legstar.coxb.CobolElementVisitor;
17  import com.legstar.coxb.common.CArrayBinding;
18  import com.legstar.coxb.host.HostException;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /**
24   * This class implements the behavior of an array of binary cobol elements
25   * bound to a JAXB byte array property.
26   *
27   * @author Fady Moussallam
28   * 
29   */
30  public class CArrayOctetStreamBinding extends CArrayBinding
31  implements ICobolArrayOctetStreamBinding {
32  
33      /** The current list for this array. */
34      private List < byte[] > mList = null;
35  
36      /**
37       * Constructor for a cobol element to java binding.
38       * 
39       * @param bindingName the identifier for this binding
40       * @param jaxbName the name of the bound java property
41       * @param jaxbType the type of the bound java property
42       * @param cobolAnnotations the cobol annotations for this element
43       * @param parentBinding a reference to the parent binding if any
44       */
45      public CArrayOctetStreamBinding(
46              final String bindingName,
47              final String jaxbName,
48              final Class < ? > jaxbType,
49              final CobolElement cobolAnnotations,
50              final ICobolComplexBinding parentBinding) {
51          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
52      }
53  
54      /** {@inheritDoc} */
55      public void accept(final CobolElementVisitor cev)
56      throws HostException {
57          cev.visit(this);
58      }
59  
60      /** {@inheritDoc} */
61      public int calcItemByteLength() {
62          return COctetStreamBinding.calcOctetStreamByteLength(getPicture(), getUsage());
63      }
64  
65      /** {@inheritDoc} */
66      public List < byte[] > getValue() throws HostException {
67  
68          if (mList == null) {
69              /* If this element is involved in a redefinition, null means
70               * this alternative is not present so send back this information. */
71              if (isRedefined() || getRedefines().length() > 0) {
72                  return null;
73              }
74              /* Send back a default value */
75              mList = new ArrayList < byte[] >();
76          }
77  
78          return mList;
79      }
80  
81      /** {@inheritDoc} */
82      public void setValue(
83              final List < byte[] > list) throws HostException {
84          mList = list;
85      }
86  
87      /** {@inheritDoc} */
88      @SuppressWarnings("unchecked")
89      public void setValue(final Object value) throws HostException {
90          if (value == null) {
91              mList = null;
92              return;
93          }
94          if (value instanceof List) {
95              mList = (List < byte[] >) value;
96          } else {
97              throw new HostException(
98                      "Value passed to " + getBindingName() + " is not a List");
99          }
100     }
101 
102     /**
103      * @return the List of items
104      */
105     public List < byte[] > getByteArrayList() {
106         return mList;
107     }
108 
109     /**
110      * @param list the items List to set
111      */
112     public void setByteArrayList(
113             final List < byte[] > list) {
114         mList = list;
115     }
116 
117     /** {@inheritDoc} */
118     public Object getObjectValue(
119             final Class < ? > type) throws HostException {
120         if (type.equals(byte[].class)) {
121             return mList;
122         } else {
123             throw new HostException("Attempt to get binding " + getBindingName()
124                     + " as an incompatible type " + type);
125         }
126     }
127 
128     /** {@inheritDoc} */
129     @SuppressWarnings("unchecked")
130     public void setObjectValue(final Object value) throws HostException {
131         if (value == null) {
132             mList = null;
133             return;
134         }
135         if (value instanceof List) {
136             if (((List < ? >) value).size() == 0) {
137                 mList = new ArrayList < byte[] >();
138                 return;
139             }
140             /* We assume all items will have the same type as the first one.
141              * The unchecked cast might break at runtime. */
142             Object item = ((List < ? >) value).get(0);
143             if (item instanceof byte[]) {
144                 mList = (List < byte[] >) value;
145                 return;
146             }
147         }
148         throw new HostException("Attempt to set binding " + getBindingName()
149                 + " from an incompatible value " + value);
150     }
151 
152     /** {@inheritDoc} */
153     public boolean isSet() {
154         return (mList != null);
155     }
156 }