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.

ComplexValueForeignKey.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess.complex;
  17. import java.io.IOException;
  18. import java.io.ObjectStreamException;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.healthmarketscience.jackcess.Column;
  23. /**
  24. * Value which is returned for a complex column. This value corresponds to a
  25. * foreign key in a secondary table which contains the actual complex data for
  26. * this row (which could be 0 or more complex values for a given row). This
  27. * class contains various convenience methods for interacting with the actual
  28. * complex values.
  29. * <p>
  30. * This class will cache the associated complex values returned from one of
  31. * the lookup methods. The various modification methods will clear this cache
  32. * automatically. The {@link #reset} method may be called manually to clear
  33. * this internal cache.
  34. *
  35. * @author James Ahlborn
  36. */
  37. public class ComplexValueForeignKey extends Number
  38. {
  39. private static final long serialVersionUID = 20110805L;
  40. private transient final Column _column;
  41. private final int _value;
  42. private transient List<? extends ComplexValue> _values;
  43. public ComplexValueForeignKey(Column column, int value) {
  44. _column = column;
  45. _value = value;
  46. }
  47. public int get() {
  48. return _value;
  49. }
  50. public Column getColumn() {
  51. return _column;
  52. }
  53. @Override
  54. public byte byteValue() {
  55. return (byte)get();
  56. }
  57. @Override
  58. public short shortValue() {
  59. return (short)get();
  60. }
  61. @Override
  62. public int intValue() {
  63. return get();
  64. }
  65. @Override
  66. public long longValue() {
  67. return get();
  68. }
  69. @Override
  70. public float floatValue() {
  71. return get();
  72. }
  73. @Override
  74. public double doubleValue() {
  75. return get();
  76. }
  77. public ComplexDataType getComplexType() {
  78. return getComplexInfo().getType();
  79. }
  80. protected ComplexColumnInfo<? extends ComplexValue> getComplexInfo() {
  81. return _column.getComplexInfo();
  82. }
  83. protected VersionHistoryColumnInfo getVersionInfo() {
  84. return (VersionHistoryColumnInfo)getComplexInfo();
  85. }
  86. protected AttachmentColumnInfo getAttachmentInfo() {
  87. return (AttachmentColumnInfo)getComplexInfo();
  88. }
  89. protected MultiValueColumnInfo getMultiValueInfo() {
  90. return (MultiValueColumnInfo)getComplexInfo();
  91. }
  92. protected UnsupportedColumnInfo getUnsupportedInfo() {
  93. return (UnsupportedColumnInfo)getComplexInfo();
  94. }
  95. public int countValues()
  96. throws IOException
  97. {
  98. return getComplexInfo().countValues(get());
  99. }
  100. public List<Map<String,Object>> getRawValues()
  101. throws IOException
  102. {
  103. return getComplexInfo().getRawValues(get());
  104. }
  105. public List<? extends ComplexValue> getValues()
  106. throws IOException
  107. {
  108. if(_values == null) {
  109. _values = getComplexInfo().getValues(this);
  110. }
  111. return _values;
  112. }
  113. @SuppressWarnings("unchecked")
  114. public List<Version> getVersions()
  115. throws IOException
  116. {
  117. if(getComplexType() != ComplexDataType.VERSION_HISTORY) {
  118. throw new UnsupportedOperationException();
  119. }
  120. return (List<Version>)getValues();
  121. }
  122. @SuppressWarnings("unchecked")
  123. public List<Attachment> getAttachments()
  124. throws IOException
  125. {
  126. if(getComplexType() != ComplexDataType.ATTACHMENT) {
  127. throw new UnsupportedOperationException();
  128. }
  129. return (List<Attachment>)getValues();
  130. }
  131. @SuppressWarnings("unchecked")
  132. public List<SingleValue> getMultiValues()
  133. throws IOException
  134. {
  135. if(getComplexType() != ComplexDataType.MULTI_VALUE) {
  136. throw new UnsupportedOperationException();
  137. }
  138. return (List<SingleValue>)getValues();
  139. }
  140. @SuppressWarnings("unchecked")
  141. public List<UnsupportedValue> getUnsupportedValues()
  142. throws IOException
  143. {
  144. if(getComplexType() != ComplexDataType.UNSUPPORTED) {
  145. throw new UnsupportedOperationException();
  146. }
  147. return (List<UnsupportedValue>)getValues();
  148. }
  149. public void reset() {
  150. // discard any cached values
  151. _values = null;
  152. }
  153. public Version addVersion(String value)
  154. throws IOException
  155. {
  156. return addVersion(value, new Date());
  157. }
  158. public Version addVersion(String value, Date modifiedDate)
  159. throws IOException
  160. {
  161. reset();
  162. Version v = VersionHistoryColumnInfo.newVersion(this, value, modifiedDate);
  163. getVersionInfo().addValue(v);
  164. return v;
  165. }
  166. public Attachment addAttachment(byte[] data)
  167. throws IOException
  168. {
  169. return addAttachment(null, null, null, data, null, null);
  170. }
  171. public Attachment addAttachment(
  172. String url, String name, String type, byte[] data,
  173. Date timeStamp, Integer flags)
  174. throws IOException
  175. {
  176. reset();
  177. Attachment a = AttachmentColumnInfo.newAttachment(
  178. this, url, name, type, data, timeStamp, flags);
  179. getAttachmentInfo().addValue(a);
  180. return a;
  181. }
  182. public Attachment addEncodedAttachment(byte[] encodedData)
  183. throws IOException
  184. {
  185. return addEncodedAttachment(null, null, null, encodedData, null, null);
  186. }
  187. public Attachment addEncodedAttachment(
  188. String url, String name, String type, byte[] encodedData,
  189. Date timeStamp, Integer flags)
  190. throws IOException
  191. {
  192. reset();
  193. Attachment a = AttachmentColumnInfo.newEncodedAttachment(
  194. this, url, name, type, encodedData, timeStamp, flags);
  195. getAttachmentInfo().addValue(a);
  196. return a;
  197. }
  198. public Attachment updateAttachment(Attachment attachment)
  199. throws IOException
  200. {
  201. reset();
  202. getAttachmentInfo().updateValue(attachment);
  203. return attachment;
  204. }
  205. public Attachment deleteAttachment(Attachment attachment)
  206. throws IOException
  207. {
  208. reset();
  209. getAttachmentInfo().deleteValue(attachment);
  210. return attachment;
  211. }
  212. public SingleValue addMultiValue(Object value)
  213. throws IOException
  214. {
  215. reset();
  216. SingleValue v = MultiValueColumnInfo.newSingleValue(this, value);
  217. getMultiValueInfo().addValue(v);
  218. return v;
  219. }
  220. public SingleValue updateMultiValue(SingleValue value)
  221. throws IOException
  222. {
  223. reset();
  224. getMultiValueInfo().updateValue(value);
  225. return value;
  226. }
  227. public SingleValue deleteMultiValue(SingleValue value)
  228. throws IOException
  229. {
  230. reset();
  231. getMultiValueInfo().deleteValue(value);
  232. return value;
  233. }
  234. public UnsupportedValue addUnsupportedValue(Map<String,?> values)
  235. throws IOException
  236. {
  237. reset();
  238. UnsupportedValue v = UnsupportedColumnInfo.newValue(this, values);
  239. getUnsupportedInfo().addValue(v);
  240. return v;
  241. }
  242. public UnsupportedValue updateUnsupportedValue(UnsupportedValue value)
  243. throws IOException
  244. {
  245. reset();
  246. getUnsupportedInfo().updateValue(value);
  247. return value;
  248. }
  249. public UnsupportedValue deleteUnsupportedValue(UnsupportedValue value)
  250. throws IOException
  251. {
  252. reset();
  253. getUnsupportedInfo().deleteValue(value);
  254. return value;
  255. }
  256. public void deleteAllValues()
  257. throws IOException
  258. {
  259. reset();
  260. getComplexInfo().deleteAllValues(this);
  261. }
  262. private Object writeReplace() throws ObjectStreamException {
  263. // if we are going to serialize this ComplexValueForeignKey, convert it
  264. // back to a normal Integer (in case it is restored outside of the context
  265. // of jackcess)
  266. return Integer.valueOf(_value);
  267. }
  268. @Override
  269. public int hashCode() {
  270. return _value;
  271. }
  272. @Override
  273. public boolean equals(Object o) {
  274. return ((this == o) ||
  275. ((o != null) && (getClass() == o.getClass()) &&
  276. (_value == ((ComplexValueForeignKey)o)._value) &&
  277. (_column == ((ComplexValueForeignKey)o)._column)));
  278. }
  279. @Override
  280. public String toString() {
  281. return String.valueOf(_value);
  282. }
  283. }