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 primary, PropertyMap complex) 
47    {
48      _primary = primary;
49      _complex = complex;
50    }
51  
52    public String getName() {
53      return _primary.getName();
54    }
55  
56    public int getSize() {
57      return _primary.getSize() + _complex.getSize();
58    }
59  
60    public boolean isEmpty() {
61      return _primary.isEmpty() && _complex.isEmpty();
62    }
63  
64    public Property get(String name) {
65      Property prop = _primary.get(name);
66      if(prop != null) {
67        return prop;
68      }
69      return _complex.get(name);
70    }
71  
72    public Object getValue(String name) {
73      return getValue(name, null);
74    }
75  
76    public Object getValue(String name, Object defaultValue) {
77      Property prop = get(name);
78      return ((prop != null) ? prop.getValue() : defaultValue);
79    }
80  
81    public Property put(String name, Object value) {
82      return put(name, null, value, false);
83    }
84  
85    public Property put(String name, DataType type, Object value) {
86      return put(name, type, value, false);
87    }
88  
89    public Property put(String name, DataType type, Object value, boolean isDdl) {
90      // the only property which seems to go in the "primary" is the "multi
91      // value" property
92      if(isPrimaryKey(name)) {
93        return _primary.put(name, DataType.BOOLEAN, value, true);
94      }
95      return _complex.put(name, type, value, isDdl);
96    }
97  
98    public void putAll(Iterable<? extends Property> props) {
99      if(props == null) {
100       return;
101     }
102 
103     for(Property prop : props) {
104       if(isPrimaryKey(prop.getName())) {
105         ((PropertyMapImpl)_primary).put(prop);
106       } else {
107         ((PropertyMapImpl)_complex).put(prop);
108       }
109     }
110   }  
111 
112   public Property remove(String name) {
113     if(isPrimaryKey(name)) {
114       return _primary.remove(name);
115     }
116     return _complex.remove(name);
117   }
118 
119   public void save() throws IOException {
120     _primary.save();
121     _complex.save();
122   }
123 
124   public Iterator<Property> iterator() {
125     final List<Iterator<Property>> iters = new ArrayList<Iterator<Property>>(2);
126     iters.add(_primary.iterator());
127     iters.add(_complex.iterator());
128 
129     return new Iterator<Property>() {
130       private Iterator<Property> _cur;
131       private Property _next = findNext();
132 
133       private Property findNext() {
134         while(!iters.isEmpty()) {
135           _cur = iters.get(0);
136           if(_cur.hasNext()) {
137             return _cur.next();
138           }
139           iters.remove(0);
140           _cur = null;
141         }
142         return null;
143       }
144 
145       public boolean hasNext() {
146         return (_next != null);
147       }
148 
149       public Property next() {
150         if(!hasNext()) {
151           throw new NoSuchElementException();
152         }
153         Property prop = _next;
154         _next = findNext();
155         return prop;
156       }
157 
158       public void remove() {
159         if(_cur != null) {
160           _cur.remove();
161           _cur = null;
162         }
163       }
164     };
165   }
166 
167   @Override
168   public String toString() {
169     return PropertyMapImpl.toString(this);
170   }
171 
172   private static boolean isPrimaryKey(String name) {
173     // the multi-value key seems to be the only one on the primary column
174     return ALLOW_MULTI_VALUE_PROP.equals(name);
175   } 
176 }