View Javadoc

1   package org.lsst.ccs.bus;
2   
3   import java.io.IOException;
4   import java.io.Serializable;
5   import java.util.ArrayList;
6   import java.util.Iterator;
7   
8   /**
9    * @author bamade
10   */
11  // Date: 23/12/2013
12  
13  public class KVList implements Serializable, Iterable<KeyData> {
14      private ArrayList<KeyData> list;
15  
16      public KVList(int size) {
17          list = new ArrayList<>(size);
18      }
19  
20      public KVList() {
21          list = new ArrayList<>();
22      }
23  
24      public KVList(String key, Object value) {
25          this(1);
26          this.add(key, value);
27      }
28  
29      public KVList add(String key, Object value) {
30          Object encoded = null;
31          try {
32              encoded = StatusCodec.encode(value);
33          } catch (IOException e) {
34              throw new IllegalArgumentException("can't encode this value: " + value, e);
35          }
36          list.add(new KeyData(key, encoded));
37          return this ;
38      }
39  
40      /**
41       * not extremely efficient way to retrieve an element
42       *
43       * @param key
44       * @return null if not found!
45       */
46      public KeyData getPair(String key) {
47          for (KeyData keyData : list) {
48              if (keyData.getKey().equals(key)) {
49                  return keyData;
50              }
51          }
52          return null;
53      }
54  
55      @Override
56      public Iterator<KeyData> iterator() {
57          return list.iterator();
58      }
59  }