1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36
37
38
39 public class MultiValueColumnPropertyMap implements PropertyMap
40 {
41
42 private final PropertyMap _primary;
43
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
100
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
190 return ALLOW_MULTI_VALUE_PROP.equals(name);
191 }
192 }