View Javadoc
1   /*
2    * Copyright 2009, openv4j.sf.net, and individual contributors as indicated
3    * by the @authors tag. See the copyright.txt in the distribution for a
4    * full listing of individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU General Public License as
8    * published by the Free Software Foundation; either version 3 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   *
21   * $Id: $
22   *
23   * @author arnep
24   */
25  package net.sf.openv4j.protocolhandlers;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /*
34   * To change this template, choose Tools | Templates
35   * and open the template in the editor.
36   */
37  /**
38   * DOCUMENT ME!
39   *
40   * @author aploese
41   */
42  public class SimpleDataContainer extends DataContainer {
43      private static Logger log = LoggerFactory.getLogger(SimpleDataContainer.class);
44      private List<DataBlock> data = new ArrayList<DataBlock>();
45  
46      /**
47       * DOCUMENT ME!
48       *
49       * @param startAddress DOCUMENT ME!
50       * @param length DOCUMENT ME!
51       */
52      @Override
53      public void addToDataContainer(int startAddress, int length) {
54          data.add(new DataBlock(startAddress, length));
55      }
56  
57      /**
58       * DOCUMENT ME!
59       *
60       * @param startAddress DOCUMENT ME!
61       * @param bytes DOCUMENT ME!
62       */
63      @Override
64      public void addToDataContainer(int startAddress, int[] bytes) {
65          data.add(new DataBlock(startAddress, bytes));
66      }
67  
68      /**
69       * DOCUMENT ME!
70       *
71       * @param i DOCUMENT ME!
72       *
73       * @return DOCUMENT ME!
74       */
75      @Override
76      public DataBlock getDataBlock(int i) {
77          return data.get(i);
78      }
79  
80      /**
81       * DOCUMENT ME!
82       *
83       * @return DOCUMENT ME!
84       */
85      @Override
86      public int getDataBlockCount() {
87          return data.size();
88      }
89  
90      /**
91       * DOCUMENT ME!
92       *
93       * @return DOCUMENT ME!
94       */
95      public int getLength() {
96          return data.size();
97      }
98  
99      /**
100      * DOCUMENT ME!
101      *
102      * @param address DOCUMENT ME!
103      *
104      * @return DOCUMENT ME!
105      */
106     @Override
107     public int getRawByte(int address) {
108         for (DataBlock db : data) {
109             if (hasAddress(db, address)) {
110                 return db.getByteAtPos(address - db.getBaseAddress()) & 0x00ff;
111             }
112         }
113 
114         log.error(String.format("No such Address %04x", address));
115 
116         return 0x00ff;
117     }
118 
119     /**
120      * DOCUMENT ME!
121      *
122      * @param address DOCUMENT ME!
123      * @param theData DOCUMENT ME!
124      */
125     @Override
126     public void setRawByte(int address, byte theData) {
127         for (DataBlock db : data) {
128             if (hasAddress(db, address)) {
129                 db.setByteAtPos(address - db.getBaseAddress(), theData);
130             }
131         }
132 
133         log.error(String.format("No such Address %04x", address));
134     }
135 
136     /**
137      * DOCUMENT ME!
138      *
139      * @return DOCUMENT ME!
140      */
141     @Override
142     public String toString() {
143         StringBuilder sb = new StringBuilder();
144 
145         for (DataBlock db : data) {
146             sb.append(String.format("0x04x :", db.getBaseAddress()));
147 
148             for (int i = 0; i < db.getLength(); i++) {
149                 sb.append(String.format(" %02x", db.getByteAtPos(i)));
150             }
151         }
152 
153         return sb.toString();
154     }
155 
156     private boolean hasAddress(DataBlock db, int address) {
157         return (db.getBaseAddress() <= address) && ((db.getBaseAddress() + db.getLength()) > address);
158     }
159 }