You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiValueColumnPropertyMap.java 4.6KB

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