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 java.util.ArrayList;
14  import java.util.List;
15  
16  import com.legstar.coxb.CobolElement;
17  import com.legstar.coxb.ICobolComplexBinding;
18  import com.legstar.coxb.host.HostException;
19  import com.legstar.coxb.common.CArrayBinding;
20  
21  /**
22   * Generic class for alphanumeric arrays bindings.
23   * 
24   */
25  public abstract class AbstractArrayAlphaNumericBinding extends CArrayBinding {
26  
27      /** The current list for this array. */
28      private List < String > mList = null;
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 if any
38       */
39      public AbstractArrayAlphaNumericBinding(
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      /**
49       * @return the List of items
50       */
51      public List < String > getStringList() {
52          return mList;
53      }
54  
55      /**
56       * @param list the items List to set
57       */
58      public void setStringList(
59              final List < String > list) {
60          mList = list;
61      }
62  
63      /** {@inheritDoc} */
64      public Object getObjectValue(
65              final Class < ? > type) throws HostException {
66          if (type.equals(String.class)) {
67              return mList;
68          } else {
69              throw new HostException("Attempt to get binding " + getBindingName()
70                      + " as an incompatible type " + type);
71          }
72      }
73  
74      /** {@inheritDoc} */
75      @SuppressWarnings("unchecked")
76      public void setObjectValue(final Object value) throws HostException {
77          if (value == null) {
78              mList = null;
79              return;
80          }
81          if (value instanceof List) {
82              if (((List <?>) value).size() == 0) {
83                  mList = new ArrayList < String >();
84                  return;
85              }
86              /* We assume all items will have the same type as the first one.
87               * The unchecked cast might break at runtime. */
88              Object item = ((List <?>) value).get(0);
89              if (item instanceof String) {
90                  mList = (List <String>) value;
91                  return;
92              }
93          }
94          throw new HostException("Attempt to set binding " + getBindingName()
95                  + " from an incompatible value " + value);
96      }
97  
98      /** {@inheritDoc} */
99      public boolean isSet() {
100         return (mList != null);
101     }
102 }