View Javadoc
1   /*
2   Copyright (c) 2016 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl.complex;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.NoSuchElementException;
24  
25  import com.healthmarketscience.jackcess.DataType;
26  import com.healthmarketscience.jackcess.PropertyMap;
27  import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
28  
29  /**
30   * PropertyMap implementation for multi-value, complex properties.  The
31   * properties for these columns seem to be dispersed between both the primary
32   * column and the complex value column.  The primary column only seems to have
33   * the simple "multi-value" property and the rest seem to be on the complex
34   * value column.  This PropertyMap implementation combines them into one
35   * synthetic map.
36   *
37   * @author James Ahlborn
38   */
39  public class MultiValueColumnPropertyMap implements PropertyMap
40  {
41    /** properties from the primary column */
42    private final PropertyMap _primary;
43    /** properties from the complex column */
44    private final PropertyMap _complex;
45  
46    public MultiValueColumnPropertyMap(PropertyMap./../../../com/healthmarketscience/jackcess/PropertyMap.html#PropertyMap">PropertyMap primary, PropertyMap complex) 
47    {
48      _primary = primary;
49      _complex = complex;
50    }
51  
52    @Override
53    public String getName() {
54      return _primary.getName();
55    }
56  
57    @Override
58    public int getSize() {
59      return _primary.getSize() + _complex.getSize();
60    }
61  
62    @Override
63    public boolean isEmpty() {
64      return _primary.isEmpty() && _complex.isEmpty();
65    }
66  
67    @Override
68    public Property get(String name) {
69      Property prop = _primary.get(name);
70      if(prop != null) {
71        return prop;
72      }
73      return _complex.get(name);
74    }
75  
76    @Override
77    public Object getValue(String name) {
78      return getValue(name, null);
79    }
80  
81    @Override
82    public Object getValue(String name, Object defaultValue) {
83      Property prop = get(name);
84      return ((prop != null) ? prop.getValue() : defaultValue);
85    }
86  
87    @Override
88    public Property put(String name, Object value) {
89      return put(name, null, value, false);
90    }
91  
92    @Override
93    public Property put(String name, DataType type, Object value) {
94      return put(name, type, value, false);
95    }
96  
97    @Override
98    public Property put(String name, DataType type, Object value, boolean isDdl) {
99      // the only property which seems to go in the "primary" is the "multi
100     // value" property
101     if(isPrimaryKey(name)) {
102       return _primary.put(name, DataType.BOOLEAN, value, true);
103     }
104     return _complex.put(name, type, value, isDdl);
105   }
106 
107   @Override
108   public void putAll(Iterable<? extends Property> props) {
109     if(props == null) {
110       return;
111     }
112 
113     for(Property prop : props) {
114       if(isPrimaryKey(prop.getName())) {
115         ((PropertyMapImpl)_primary).put(prop);
116       } else {
117         ((PropertyMapImpl)_complex).put(prop);
118       }
119     }
120   }  
121 
122   @Override
123   public Property remove(String name) {
124     if(isPrimaryKey(name)) {
125       return _primary.remove(name);
126     }
127     return _complex.remove(name);
128   }
129 
130   @Override
131   public void save() throws IOException {
132     _primary.save();
133     _complex.save();
134   }
135 
136   @Override
137   public Iterator<Property> iterator() {
138     final List<Iterator<Property>> iters = new ArrayList<Iterator<Property>>(2);
139     iters.add(_primary.iterator());
140     iters.add(_complex.iterator());
141 
142     return new Iterator<Property>() {
143       private Iterator<Property> _cur;
144       private Property _next = findNext();
145 
146       private Property findNext() {
147         while(!iters.isEmpty()) {
148           _cur = iters.get(0);
149           if(_cur.hasNext()) {
150             return _cur.next();
151           }
152           iters.remove(0);
153           _cur = null;
154         }
155         return null;
156       }
157 
158       @Override
159       public boolean hasNext() {
160         return (_next != null);
161       }
162 
163       @Override
164       public Property next() {
165         if(!hasNext()) {
166           throw new NoSuchElementException();
167         }
168         Property prop = _next;
169         _next = findNext();
170         return prop;
171       }
172 
173       @Override
174       public void remove() {
175         if(_cur != null) {
176           _cur.remove();
177           _cur = null;
178         }
179       }
180     };
181   }
182 
183   @Override
184   public String toString() {
185     return PropertyMapImpl.toString(this);
186   }
187 
188   private static boolean isPrimaryKey(String name) {
189     // the multi-value key seems to be the only one on the primary column
190     return ALLOW_MULTI_VALUE_PROP.equals(name);
191   } 
192 }