aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/impl/complex/MultiValueColumnPropertyMap.java
blob: 49aed52d2891b44000f4495a0864005750af33c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright (c) 2016 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.impl.complex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.PropertyMap;
import com.healthmarketscience.jackcess.impl.PropertyMapImpl;

/**
 * PropertyMap implementation for multi-value, complex properties.  The
 * properties for these columns seem to be dispersed between both the primary
 * column and the complex value column.  The primary column only seems to have
 * the simple "multi-value" property and the rest seem to be on the complex
 * value column.  This PropertyMap implementation combines them into one
 * synthetic map.
 *
 * @author James Ahlborn
 */
public class MultiValueColumnPropertyMap implements PropertyMap
{
  /** properties from the primary column */
  private final PropertyMap _primary;
  /** properties from the complex column */
  private final PropertyMap _complex;

  public MultiValueColumnPropertyMap(PropertyMap primary, PropertyMap complex) 
  {
    _primary = primary;
    _complex = complex;
  }

  @Override
  public String getName() {
    return _primary.getName();
  }

  @Override
  public int getSize() {
    return _primary.getSize() + _complex.getSize();
  }

  @Override
  public boolean isEmpty() {
    return _primary.isEmpty() && _complex.isEmpty();
  }

  @Override
  public Property get(String name) {
    Property prop = _primary.get(name);
    if(prop != null) {
      return prop;
    }
    return _complex.get(name);
  }

  @Override
  public Object getValue(String name) {
    return getValue(name, null);
  }

  @Override
  public Object getValue(String name, Object defaultValue) {
    Property prop = get(name);
    return ((prop != null) ? prop.getValue() : defaultValue);
  }

  @Override
  public Property put(String name, Object value) {
    return put(name, null, value, false);
  }

  @Override
  public Property put(String name, DataType type, Object value) {
    return put(name, type, value, false);
  }

  @Override
  public Property put(String name, DataType type, Object value, boolean isDdl) {
    // the only property which seems to go in the "primary" is the "multi
    // value" property
    if(isPrimaryKey(name)) {
      return _primary.put(name, DataType.BOOLEAN, value, true);
    }
    return _complex.put(name, type, value, isDdl);
  }

  @Override
  public void putAll(Iterable<? extends Property> props) {
    if(props == null) {
      return;
    }

    for(Property prop : props) {
      if(isPrimaryKey(prop.getName())) {
        ((PropertyMapImpl)_primary).put(prop);
      } else {
        ((PropertyMapImpl)_complex).put(prop);
      }
    }
  }  

  @Override
  public Property remove(String name) {
    if(isPrimaryKey(name)) {
      return _primary.remove(name);
    }
    return _complex.remove(name);
  }

  @Override
  public void save() throws IOException {
    _primary.save();
    _complex.save();
  }

  @Override
  public Iterator<Property> iterator() {
    final List<Iterator<Property>> iters = new ArrayList<Iterator<Property>>(2);
    iters.add(_primary.iterator());
    iters.add(_complex.iterator());

    return new Iterator<Property>() {
      private Iterator<Property> _cur;
      private Property _next = findNext();

      private Property findNext() {
        while(!iters.isEmpty()) {
          _cur = iters.get(0);
          if(_cur.hasNext()) {
            return _cur.next();
          }
          iters.remove(0);
          _cur = null;
        }
        return null;
      }

      @Override
      public boolean hasNext() {
        return (_next != null);
      }

      @Override
      public Property next() {
        if(!hasNext()) {
          throw new NoSuchElementException();
        }
        Property prop = _next;
        _next = findNext();
        return prop;
      }

      @Override
      public void remove() {
        if(_cur != null) {
          _cur.remove();
          _cur = null;
        }
      }
    };
  }

  @Override
  public String toString() {
    return PropertyMapImpl.toString(this);
  }

  private static boolean isPrimaryKey(String name) {
    // the multi-value key seems to be the only one on the primary column
    return ALLOW_MULTI_VALUE_PROP.equals(name);
  } 
}