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      return put(name, type, value, isDdl, false);
100   }
101 
102   @Override
103   public Property put(String name, DataType type, Object value, boolean isDdl,
104                       boolean isSkipHandler) {
105     // the only property which seems to go in the "primary" is the "multi
106     // value" property
107     if(isPrimaryKey(name)) {
108       return _primary.put(name, DataType.BOOLEAN, value, true, isSkipHandler);
109     }
110     return _complex.put(name, type, value, isDdl, isSkipHandler);
111   }
112 
113   @Override
114   public void putAll(Iterable<? extends Property> props) {
115     if(props == null) {
116       return;
117     }
118 
119     for(Property prop : props) {
120       if(isPrimaryKey(prop.getName())) {
121         ((PropertyMapImpl)_primary).put(prop);
122       } else {
123         ((PropertyMapImpl)_complex).put(prop);
124       }
125     }
126   }  
127 
128   @Override
129   public Property remove(String name) {
130     if(isPrimaryKey(name)) {
131       return _primary.remove(name);
132     }
133     return _complex.remove(name);
134   }
135 
136   @Override
137   public void save() throws IOException {
138     _primary.save();
139     _complex.save();
140   }
141 
142   @Override
143   public Iterator<Property> iterator() {
144     final List<Iterator<Property>> iters = new ArrayList<>(2);
145     iters.add(_primary.iterator());
146     iters.add(_complex.iterator());
147 
148     return new Iterator<Property>() {
149       private Iterator<Property> _cur;
150       private Property _next = findNext();
151 
152       private Property findNext() {
153         while(!iters.isEmpty()) {
154           _cur = iters.get(0);
155           if(_cur.hasNext()) {
156             return _cur.next();
157           }
158           iters.remove(0);
159           _cur = null;
160         }
161         return null;
162       }
163 
164       @Override
165       public boolean hasNext() {
166         return (_next != null);
167       }
168 
169       @Override
170       public Property next() {
171         if(!hasNext()) {
172           throw new NoSuchElementException();
173         }
174         Property prop = _next;
175         _next = findNext();
176         return prop;
177       }
178 
179       @Override
180       public void remove() {
181         if(_cur != null) {
182           _cur.remove();
183           _cur = null;
184         }
185       }
186     };
187   }
188 
189   @Override
190   public String toString() {
191     return PropertyMapImpl.toString(this);
192   }
193 
194   private static boolean isPrimaryKey(String name) {
195     // the multi-value key seems to be the only one on the primary column
196     return ALLOW_MULTI_VALUE_PROP.equals(name);
197   } 
198 }