From c1aa151cd41d0b8da5f16ac2b40bdc7f084f7d70 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Mon, 17 Dec 2018 23:10:13 +0000 Subject: add LocalDateTime variants for various public Date based methods git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jdk8@1241 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../healthmarketscience/jackcess/DateTimeType.java | 4 +- .../java/com/healthmarketscience/jackcess/Row.java | 10 +- .../jackcess/complex/Attachment.java | 28 ++++- .../jackcess/complex/ComplexValueForeignKey.java | 46 ++++++-- .../jackcess/complex/Version.java | 10 ++ .../healthmarketscience/jackcess/impl/RowImpl.java | 1 + .../impl/complex/AttachmentColumnInfoImpl.java | 34 ++++-- .../impl/complex/ComplexValueForeignKeyImpl.java | 123 +++++++++++++++------ .../impl/complex/VersionHistoryColumnInfoImpl.java | 68 +++++++----- .../jackcess/ComplexColumnTest.java | 73 ++++++------ .../healthmarketscience/jackcess/DatabaseTest.java | 1 + .../com/healthmarketscience/jackcess/TestUtil.java | 1 + 12 files changed, 278 insertions(+), 121 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/DateTimeType.java b/src/main/java/com/healthmarketscience/jackcess/DateTimeType.java index 7f5cdb1..8704350 100644 --- a/src/main/java/com/healthmarketscience/jackcess/DateTimeType.java +++ b/src/main/java/com/healthmarketscience/jackcess/DateTimeType.java @@ -17,7 +17,9 @@ limitations under the License. package com.healthmarketscience.jackcess; /** - * Enum for selecting how a Database returns date/time types. + * Enum for selecting how a Database returns date/time types. Prefer using + * {@link DateTimeType#LOCAL_DATE_TIME} as using Date is being phased out and + * will eventually be removed. * * @author James Ahlborn */ diff --git a/src/main/java/com/healthmarketscience/jackcess/Row.java b/src/main/java/com/healthmarketscience/jackcess/Row.java index 6917628..8fcaf67 100644 --- a/src/main/java/com/healthmarketscience/jackcess/Row.java +++ b/src/main/java/com/healthmarketscience/jackcess/Row.java @@ -91,12 +91,20 @@ public interface Row extends Map /** * Convenience method which gets the value for the row with the given name, * casting it to a Date (DataType SHORT_DATE_TIME). + * @deprecated this is only valid for Database instances configured for the + * legacy {@link DateTimeType#DATE}. Prefer using + * {@link DateTimeType#LOCAL_DATE_TIME} and the corresponding + * {@link #getLocalDateTime} method. Using Date is being phased + * out and will eventually be removed. */ + @Deprecated public Date getDate(String name); /** * Convenience method which gets the value for the row with the given name, - * casting it to a LocalDateTime (DataType SHORT_DATE_TIME). + * casting it to a LocalDateTime (DataType SHORT_DATE_TIME). This method + * will only work for Database instances configured for + * {@link DateTimeType#LOCAL_DATE_TIME}. */ public LocalDateTime getLocalDateTime(String name); diff --git a/src/main/java/com/healthmarketscience/jackcess/complex/Attachment.java b/src/main/java/com/healthmarketscience/jackcess/complex/Attachment.java index d35559e..0047719 100644 --- a/src/main/java/com/healthmarketscience/jackcess/complex/Attachment.java +++ b/src/main/java/com/healthmarketscience/jackcess/complex/Attachment.java @@ -17,14 +17,16 @@ limitations under the License. package com.healthmarketscience.jackcess.complex; import java.io.IOException; +import java.time.LocalDateTime; import java.util.Date; +import com.healthmarketscience.jackcess.DateTimeType; /** * Complex value corresponding to an attachment. * * @author James Ahlborn */ -public interface Attachment extends ComplexValue +public interface Attachment extends ComplexValue { public byte[] getFileData() throws IOException; @@ -37,20 +39,34 @@ public interface Attachment extends ComplexValue public String getFileName(); public void setFileName(String fileName); - + public String getFileUrl(); public void setFileUrl(String fileUrl); - + public String getFileType(); public void setFileType(String fileType); - + + /** + * @deprecated see {@link DateTimeType} for details + */ + @Deprecated public Date getFileTimeStamp(); + /** + * @deprecated see {@link DateTimeType} for details + */ + @Deprecated public void setFileTimeStamp(Date fileTimeStamp); - + + public LocalDateTime getFileLocalTimeStamp(); + + public void setFileLocalTimeStamp(LocalDateTime fileTimeStamp); + + public Object getFileTimeStampObject(); + public Integer getFileFlags(); - public void setFileFlags(Integer fileFlags); + public void setFileFlags(Integer fileFlags); } diff --git a/src/main/java/com/healthmarketscience/jackcess/complex/ComplexValueForeignKey.java b/src/main/java/com/healthmarketscience/jackcess/complex/ComplexValueForeignKey.java index 150dd07..0e0bc13 100644 --- a/src/main/java/com/healthmarketscience/jackcess/complex/ComplexValueForeignKey.java +++ b/src/main/java/com/healthmarketscience/jackcess/complex/ComplexValueForeignKey.java @@ -18,10 +18,13 @@ package com.healthmarketscience.jackcess.complex; import java.io.IOException; import java.io.ObjectStreamException; +import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.Map; + import com.healthmarketscience.jackcess.Column; +import com.healthmarketscience.jackcess.DateTimeType; /** @@ -40,33 +43,33 @@ import com.healthmarketscience.jackcess.Column; */ public abstract class ComplexValueForeignKey extends Number { - private static final long serialVersionUID = 20130319L; + private static final long serialVersionUID = 20130319L; @Override public byte byteValue() { return (byte)get(); } - + @Override public short shortValue() { return (short)get(); } - + @Override public int intValue() { return get(); } - + @Override public long longValue() { return get(); } - + @Override public float floatValue() { return get(); } - + @Override public double doubleValue() { return get(); @@ -78,12 +81,12 @@ public abstract class ComplexValueForeignKey extends Number // of jackcess) return Integer.valueOf(get()); } - + @Override public int hashCode() { return get(); } - + @Override public boolean equals(Object o) { return ((this == o) || @@ -94,7 +97,7 @@ public abstract class ComplexValueForeignKey extends Number @Override public String toString() { return String.valueOf(get()); - } + } public abstract int get(); @@ -122,25 +125,50 @@ public abstract class ComplexValueForeignKey extends Number public abstract Version addVersion(String value) throws IOException; + /** + * @deprecated see {@link DateTimeType} for details + */ + @Deprecated public abstract Version addVersion(String value, Date modifiedDate) throws IOException; + public abstract Version addVersion(String value, LocalDateTime modifiedDate) + throws IOException; + public abstract Attachment addAttachment(byte[] data) throws IOException; + /** + * @deprecated see {@link DateTimeType} for details + */ + @Deprecated public abstract Attachment addAttachment( String url, String name, String type, byte[] data, Date timeStamp, Integer flags) throws IOException; + public abstract Attachment addAttachment( + String url, String name, String type, byte[] data, + LocalDateTime timeStamp, Integer flags) + throws IOException; + public abstract Attachment addEncodedAttachment(byte[] encodedData) throws IOException; + /** + * @deprecated see {@link DateTimeType} for details + */ + @Deprecated public abstract Attachment addEncodedAttachment( String url, String name, String type, byte[] encodedData, Date timeStamp, Integer flags) throws IOException; + public abstract Attachment addEncodedAttachment( + String url, String name, String type, byte[] encodedData, + LocalDateTime timeStamp, Integer flags) + throws IOException; + public abstract Attachment updateAttachment(Attachment attachment) throws IOException; diff --git a/src/main/java/com/healthmarketscience/jackcess/complex/Version.java b/src/main/java/com/healthmarketscience/jackcess/complex/Version.java index a1ace1b..374e047 100644 --- a/src/main/java/com/healthmarketscience/jackcess/complex/Version.java +++ b/src/main/java/com/healthmarketscience/jackcess/complex/Version.java @@ -16,7 +16,9 @@ limitations under the License. package com.healthmarketscience.jackcess.complex; +import java.time.LocalDateTime; import java.util.Date; +import com.healthmarketscience.jackcess.DateTimeType; /** * Complex value corresponding to a version of a memo column. @@ -27,5 +29,13 @@ public interface Version extends ComplexValue, Comparable { public String getValue(); + /** + * @deprecated see {@link DateTimeType} for details + */ + @Deprecated public Date getModifiedDate(); + + public LocalDateTime getModifiedLocalDate(); + + public Object getModifiedDateObject(); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java index 0e6fe6e..9b9ee7a 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java @@ -91,6 +91,7 @@ public class RowImpl extends LinkedHashMap implements Row return (Double)get(name); } + @SuppressWarnings("deprecation") public Date getDate(String name) { return (Date)get(name); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java index 6642a69..1789e92 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.time.LocalDateTime; import java.util.Arrays; import java.util.Date; import java.util.HashSet; @@ -166,7 +167,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl String name = (String)getFileNameColumn().getRowValue(rawValue); String type = (String)getFileTypeColumn().getRowValue(rawValue); Integer flags = (Integer)getFileFlagsColumn().getRowValue(rawValue); - Date ts = (Date)getFileTimeStampColumn().getRowValue(rawValue); + Object ts = getFileTimeStampColumn().getRowValue(rawValue); byte[] data = (byte[])getFileDataColumn().getRowValue(rawValue); return new AttachmentImpl(id, complexValueFk, url, name, type, null, @@ -182,7 +183,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl getFileNameColumn().setRowValue(row, attachment.getFileName()); getFileTypeColumn().setRowValue(row, attachment.getFileType()); getFileFlagsColumn().setRowValue(row, attachment.getFileFlags()); - getFileTimeStampColumn().setRowValue(row, attachment.getFileTimeStamp()); + getFileTimeStampColumn().setRowValue(row, attachment.getFileTimeStampObject()); getFileDataColumn().setRowValue(row, attachment.getEncodedFileData()); return row; } @@ -198,7 +199,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl public static Attachment newAttachment( String url, String name, String type, byte[] data, - Date timeStamp, Integer flags) + Object timeStamp, Integer flags) { return newAttachment(INVALID_FK, url, name, type, data, timeStamp, flags); @@ -206,7 +207,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl public static Attachment newAttachment( ComplexValueForeignKey complexValueFk, String url, String name, - String type, byte[] data, Date timeStamp, Integer flags) + String type, byte[] data, Object timeStamp, Integer flags) { return new AttachmentImpl(INVALID_ID, complexValueFk, url, name, type, data, timeStamp, flags, null); @@ -224,7 +225,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl public static Attachment newEncodedAttachment( String url, String name, String type, byte[] encodedData, - Date timeStamp, Integer flags) + Object timeStamp, Integer flags) { return newEncodedAttachment(INVALID_FK, url, name, type, encodedData, timeStamp, flags); @@ -232,13 +233,14 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl public static Attachment newEncodedAttachment( ComplexValueForeignKey complexValueFk, String url, String name, - String type, byte[] encodedData, Date timeStamp, Integer flags) + String type, byte[] encodedData, Object timeStamp, Integer flags) { return new AttachmentImpl(INVALID_ID, complexValueFk, url, name, type, null, timeStamp, flags, encodedData); } + @SuppressWarnings("deprecation") private static class AttachmentImpl extends ComplexValueImpl implements Attachment { @@ -246,13 +248,13 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl private String _name; private String _type; private byte[] _data; - private Date _timeStamp; + private Object _timeStamp; private Integer _flags; private byte[] _encodedData; private AttachmentImpl(Id id, ComplexValueForeignKey complexValueFk, String url, String name, String type, byte[] data, - Date timeStamp, Integer flags, byte[] encodedData) + Object timeStamp, Integer flags, byte[] encodedData) { super(id, complexValueFk); _url = url; @@ -313,13 +315,25 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl } public Date getFileTimeStamp() { - return _timeStamp; + return (Date)_timeStamp; } public void setFileTimeStamp(Date fileTimeStamp) { _timeStamp = fileTimeStamp; } + public LocalDateTime getFileLocalTimeStamp() { + return (LocalDateTime)_timeStamp; + } + + public void setFileLocalTimeStamp(LocalDateTime fileTimeStamp) { + _timeStamp = fileTimeStamp; + } + + public Object getFileTimeStampObject() { + return _timeStamp; + } + public Integer getFileFlags() { return _flags; } @@ -348,7 +362,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl return "Attachment(" + getComplexValueForeignKey() + "," + getId() + ") " + getFileUrl() + ", " + getFileName() + ", " + getFileType() - + ", " + getFileTimeStamp() + ", " + getFileFlags() + ", " + + + ", " + getFileTimeStampObject() + ", " + getFileFlags() + ", " + dataStr; } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexValueForeignKeyImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexValueForeignKeyImpl.java index a73d3ed..06c0cd7 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexValueForeignKeyImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexValueForeignKeyImpl.java @@ -17,11 +17,14 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.complex; import java.io.IOException; +import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.Map; import com.healthmarketscience.jackcess.Column; +import com.healthmarketscience.jackcess.Database; +import com.healthmarketscience.jackcess.DateTimeType; import com.healthmarketscience.jackcess.Row; import com.healthmarketscience.jackcess.complex.Attachment; import com.healthmarketscience.jackcess.complex.AttachmentColumnInfo; @@ -50,14 +53,15 @@ import com.healthmarketscience.jackcess.complex.VersionHistoryColumnInfo; * * @author James Ahlborn */ +@SuppressWarnings("deprecation") public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey { - private static final long serialVersionUID = 20110805L; - + private static final long serialVersionUID = 20110805L; + private transient final Column _column; private final int _value; private transient List _values; - + public ComplexValueForeignKeyImpl(Column column, int value) { _column = column; _value = value; @@ -72,12 +76,12 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey public Column getColumn() { return _column; } - + @Override public ComplexDataType getComplexType() { return getComplexInfo().getType(); } - + protected ComplexColumnInfo getComplexInfo() { return _column.getComplexInfo(); } @@ -85,7 +89,7 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey protected VersionHistoryColumnInfo getVersionInfo() { return (VersionHistoryColumnInfo)getComplexInfo(); } - + protected AttachmentColumnInfo getAttachmentInfo() { return (AttachmentColumnInfo)getComplexInfo(); } @@ -93,27 +97,27 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey protected MultiValueColumnInfo getMultiValueInfo() { return (MultiValueColumnInfo)getComplexInfo(); } - + protected UnsupportedColumnInfo getUnsupportedInfo() { return (UnsupportedColumnInfo)getComplexInfo(); } - + @Override public int countValues() throws IOException { return getComplexInfo().countValues(get()); } - + public List getRawValues() throws IOException { return getComplexInfo().getRawValues(get()); - } - + } + @Override public List getValues() throws IOException { if(_values == null) { _values = getComplexInfo().getValues(this); } return _values; - } + } @Override @SuppressWarnings("unchecked") @@ -123,7 +127,7 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey } return (List)getValues(); } - + @Override @SuppressWarnings("unchecked") public List getAttachments() throws IOException { @@ -132,7 +136,7 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey } return (List)getValues(); } - + @Override @SuppressWarnings("unchecked") public List getMultiValues() throws IOException { @@ -141,7 +145,7 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey } return (List)getValues(); } - + @Override @SuppressWarnings("unchecked") public List getUnsupportedValues() throws IOException { @@ -150,20 +154,29 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey } return (List)getValues(); } - + @Override public void reset() { // discard any cached values _values = null; } - + @Override public Version addVersion(String value) throws IOException { - return addVersion(value, new Date()); + return addVersionImpl(value, now()); } - + @Override public Version addVersion(String value, Date modifiedDate) throws IOException { + return addVersionImpl(value, modifiedDate); + } + + @Override + public Version addVersion(String value, LocalDateTime modifiedDate) throws IOException { + return addVersionImpl(value, modifiedDate); + } + + private Version addVersionImpl(String value, Object modifiedDate) throws IOException { reset(); Version v = VersionHistoryColumnInfoImpl.newVersion(this, value, modifiedDate); getVersionInfo().addValue(v); @@ -172,14 +185,31 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey @Override public Attachment addAttachment(byte[] data) throws IOException { - return addAttachment(null, null, null, data, null, null); + return addAttachmentImpl(null, null, null, data, null, null); } - + @Override public Attachment addAttachment( String url, String name, String type, byte[] data, Date timeStamp, Integer flags) throws IOException + { + return addAttachmentImpl(url, name, type, data, timeStamp, flags); + } + + @Override + public Attachment addAttachment( + String url, String name, String type, byte[] data, + LocalDateTime timeStamp, Integer flags) + throws IOException + { + return addAttachmentImpl(url, name, type, data, timeStamp, flags); + } + + private Attachment addAttachmentImpl( + String url, String name, String type, byte[] data, + Object timeStamp, Integer flags) + throws IOException { reset(); Attachment a = AttachmentColumnInfoImpl.newAttachment( @@ -192,14 +222,33 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey public Attachment addEncodedAttachment(byte[] encodedData) throws IOException { - return addEncodedAttachment(null, null, null, encodedData, null, null); + return addEncodedAttachmentImpl(null, null, null, encodedData, null, null); } - + @Override public Attachment addEncodedAttachment( String url, String name, String type, byte[] encodedData, Date timeStamp, Integer flags) throws IOException + { + return addEncodedAttachmentImpl(url, name, type, encodedData, timeStamp, + flags); + } + + @Override + public Attachment addEncodedAttachment( + String url, String name, String type, byte[] encodedData, + LocalDateTime timeStamp, Integer flags) + throws IOException + { + return addEncodedAttachmentImpl(url, name, type, encodedData, timeStamp, + flags); + } + + private Attachment addEncodedAttachmentImpl( + String url, String name, String type, byte[] encodedData, + Object timeStamp, Integer flags) + throws IOException { reset(); Attachment a = AttachmentColumnInfoImpl.newEncodedAttachment( @@ -207,21 +256,21 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey getAttachmentInfo().addValue(a); return a; } - + @Override public Attachment updateAttachment(Attachment attachment) throws IOException { reset(); getAttachmentInfo().updateValue(attachment); return attachment; } - + @Override public Attachment deleteAttachment(Attachment attachment) throws IOException { reset(); getAttachmentInfo().deleteValue(attachment); return attachment; } - + @Override public SingleValue addMultiValue(Object value) throws IOException { reset(); @@ -229,21 +278,21 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey getMultiValueInfo().addValue(v); return v; } - + @Override public SingleValue updateMultiValue(SingleValue value) throws IOException { reset(); getMultiValueInfo().updateValue(value); return value; } - + @Override public SingleValue deleteMultiValue(SingleValue value) throws IOException { reset(); getMultiValueInfo().deleteValue(value); return value; } - + @Override public UnsupportedValue addUnsupportedValue(Map values) throws IOException @@ -253,7 +302,7 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey getUnsupportedInfo().addValue(v); return v; } - + @Override public UnsupportedValue updateUnsupportedValue(UnsupportedValue value) throws IOException @@ -262,7 +311,7 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey getUnsupportedInfo().updateValue(value); return value; } - + @Override public UnsupportedValue deleteUnsupportedValue(UnsupportedValue value) throws IOException @@ -271,16 +320,24 @@ public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey getUnsupportedInfo().deleteValue(value); return value; } - + @Override public void deleteAllValues() throws IOException { reset(); getComplexInfo().deleteAllValues(this); } - + @Override public boolean equals(Object o) { return(super.equals(o) && (_column == ((ComplexValueForeignKeyImpl)o)._column)); } + + private Object now() { + Database db = getColumn().getDatabase(); + if(db.getDateTimeType() == DateTimeType.DATE) { + return new Date(); + } + return LocalDateTime.now(db.getZoneId()); + } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/complex/VersionHistoryColumnInfoImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/complex/VersionHistoryColumnInfoImpl.java index a64788f..14667d0 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/complex/VersionHistoryColumnInfoImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/complex/VersionHistoryColumnInfoImpl.java @@ -17,6 +17,7 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.complex; import java.io.IOException; +import java.time.LocalDateTime; import java.util.Collections; import java.util.Date; import java.util.List; @@ -42,14 +43,14 @@ import com.healthmarketscience.jackcess.impl.ColumnImpl; * * @author James Ahlborn */ -public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl +public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl implements VersionHistoryColumnInfo { private final Column _valueCol; private final Column _modifiedCol; - + public VersionHistoryColumnInfoImpl(Column column, int complexId, - Table typeObjTable, Table flatTable) + Table typeObjTable, Table flatTable) throws IOException { super(column, complexId, typeObjTable, flatTable); @@ -83,7 +84,7 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl getValueColumn().getName()); ((ColumnImpl)versionedCol).setVersionHistoryColumn((ColumnImpl)getColumn()); } - + public Column getValueColumn() { return _valueCol; } @@ -91,7 +92,7 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl public Column getModifiedDateColumn() { return _modifiedCol; } - + @Override public ComplexDataType getType() { return ComplexDataType.VERSION_HISTORY; @@ -124,7 +125,7 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl // order versions newest to oldest Collections.sort(versions); - + return versions; } @@ -133,7 +134,7 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl Row rawValue) { ComplexValue.Id id = getValueId(rawValue); String value = (String)getValueColumn().getRowValue(rawValue); - Date modifiedDate = (Date)getModifiedDateColumn().getRowValue(rawValue); + Object modifiedDate = getModifiedDateColumn().getRowValue(rawValue); return new VersionImpl(id, complexValueFk, value, modifiedDate); } @@ -142,47 +143,55 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl protected Object[] asRow(Object[] row, Version version) throws IOException { super.asRow(row, version); getValueColumn().setRowValue(row, version.getValue()); - getModifiedDateColumn().setRowValue(row, version.getModifiedDate()); + getModifiedDateColumn().setRowValue(row, version.getModifiedDateObject()); return row; } - - public static Version newVersion(String value, Date modifiedDate) { + + public static Version newVersion(String value, Object modifiedDate) { return newVersion(INVALID_FK, value, modifiedDate); } - + public static Version newVersion(ComplexValueForeignKey complexValueFk, - String value, Date modifiedDate) { + String value, Object modifiedDate) { return new VersionImpl(INVALID_ID, complexValueFk, value, modifiedDate); } - + @SuppressWarnings("deprecation") private static class VersionImpl extends ComplexValueImpl implements Version { private final String _value; - private final Date _modifiedDate; + private final Object _modifiedDate; private VersionImpl(Id id, ComplexValueForeignKey complexValueFk, - String value, Date modifiedDate) + String value, Object modifiedDate) { super(id, complexValueFk); _value = value; _modifiedDate = modifiedDate; } - + public String getValue() { return _value; } public Date getModifiedDate() { + return (Date)_modifiedDate; + } + + public LocalDateTime getModifiedLocalDate() { + return (LocalDateTime)_modifiedDate; + } + + public Object getModifiedDateObject() { return _modifiedDate; - } - + } + public int compareTo(Version o) { - Date d1 = getModifiedDate(); - Date d2 = o.getModifiedDate(); + Object d1 = getModifiedDateObject(); + Object d2 = o.getModifiedDateObject(); // sort by descending date (newest/greatest first) - int cmp = d2.compareTo(d1); + int cmp = compare(d2, d1); if(cmp != 0) { return cmp; } @@ -200,11 +209,20 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl ((id1 < id2) ? 1 : 0)); } + @SuppressWarnings("unchecked") + private static > int compare(Object o1, Object o2) { + // each date/time type (Date, LocalDateTime) is mutually Comparable, so + // just silence the compiler + C c1 = (C)o1; + C c2 = (C)o2; + return c1.compareTo(c2); + } + public void update() throws IOException { throw new UnsupportedOperationException( "This column does not support value updates"); } - + public void delete() throws IOException { throw new UnsupportedOperationException( "This column does not support value deletes"); @@ -214,8 +232,8 @@ public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl public String toString() { return "Version(" + getComplexValueForeignKey() + "," + getId() + ") " + - getModifiedDate() + ", " + getValue(); - } + getModifiedDateObject() + ", " + getValue(); + } } - + } diff --git a/src/test/java/com/healthmarketscience/jackcess/ComplexColumnTest.java b/src/test/java/com/healthmarketscience/jackcess/ComplexColumnTest.java index b4704bf..c8b81a3 100644 --- a/src/test/java/com/healthmarketscience/jackcess/ComplexColumnTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/ComplexColumnTest.java @@ -39,7 +39,8 @@ import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; * * @author James Ahlborn */ -public class ComplexColumnTest extends TestCase +@SuppressWarnings("deprecation") +public class ComplexColumnTest extends TestCase { public ComplexColumnTest(String name) { @@ -66,7 +67,7 @@ public class ComplexColumnTest extends TestCase (ComplexValueForeignKey)verCol.getRowValue(row); String curValue = (String)col.getRowValue(row); - + if(rowId.equals("row1")) { checkVersions(1, complexValueFk, curValue); } else if(rowId.equals("row2")) { @@ -94,7 +95,7 @@ public class ComplexColumnTest extends TestCase Date upTime = new Date(); row8ValFk.addVersion("row8-memo", upTime); checkVersions(row8ValFk.get(), row8ValFk, "row8-memo", - "row8-memo", upTime); + "row8-memo", upTime); Cursor cursor = CursorBuilder.createCursor(t1); assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row3")); @@ -120,7 +121,7 @@ public class ComplexColumnTest extends TestCase "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); - + try { v.delete(); fail("UnsupportedOperationException should have been thrown"); @@ -133,7 +134,7 @@ public class ComplexColumnTest extends TestCase "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); - + try { v.getComplexValueForeignKey().deleteAllValues(); fail("UnsupportedOperationException should have been thrown"); @@ -146,7 +147,7 @@ public class ComplexColumnTest extends TestCase "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); - + db.close(); } } @@ -154,7 +155,7 @@ public class ComplexColumnTest extends TestCase public void testAttachments() throws Exception { for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { - + Database db = openCopy(testDB); Table t1 = db.getTable("Table1"); @@ -187,12 +188,12 @@ public class ComplexColumnTest extends TestCase ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey) col.getRowValue(row8); row8ValFk.addAttachment(null, "test_data.txt", "txt", - getFileBytes("test_data.txt"), null, null); + getFileBytes("test_data.txt"), (Date)null, null); checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt"); row8ValFk.addEncodedAttachment(null, "test_data2.txt", "txt", - getEncodedFileBytes("test_data2.txt"), null, - null); - checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt", + getEncodedFileBytes("test_data2.txt"), + (Date)null, null); + checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt", "test_data2.txt"); Cursor cursor = CursorBuilder.createCursor(t1); @@ -200,8 +201,8 @@ public class ComplexColumnTest extends TestCase ComplexValueForeignKey row4ValFk = (ComplexValueForeignKey) cursor.getCurrentRowValue(col); Attachment a = row4ValFk.addAttachment(null, "test_data.txt", "txt", - getFileBytes("test_data.txt"), null, - null); + getFileBytes("test_data.txt"), + (Date)null, null); checkAttachments(4, row4ValFk, "test_data2.txt", "test_data.txt"); a.setFileType("zip"); @@ -230,8 +231,8 @@ public class ComplexColumnTest extends TestCase ComplexValueForeignKey row2ValFk = (ComplexValueForeignKey) cursor.getCurrentRowValue(col); row2ValFk.deleteAllValues(); - checkAttachments(2, row2ValFk); - + checkAttachments(2, row2ValFk); + db.close(); } } @@ -239,7 +240,7 @@ public class ComplexColumnTest extends TestCase public void testMultiValues() throws Exception { for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { - + Database db = openCopy(testDB); Table t1 = db.getTable("Table1"); @@ -264,7 +265,7 @@ public class ComplexColumnTest extends TestCase } else { assertTrue(false); } - } + } Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo", Column.AUTO_NUMBER, Column.AUTO_NUMBER}; @@ -307,17 +308,17 @@ public class ComplexColumnTest extends TestCase PropertyMap props = col.getProperties(); assertEquals(Boolean.TRUE, props.getValue(PropertyMap.ALLOW_MULTI_VALUE_PROP)); assertEquals("Value List", props.getValue(PropertyMap.ROW_SOURCE_TYPE_PROP)); - assertEquals("\"value1\";\"value2\";\"value3\";\"value4\"", + assertEquals("\"value1\";\"value2\";\"value3\";\"value4\"", props.getValue(PropertyMap.ROW_SOURCE_PROP)); - + db.close(); } } - + public void testUnsupported() throws Exception { for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) { - + Database db = openCopy(testDB); Table t1 = db.getTable("Test"); @@ -331,7 +332,7 @@ public class ComplexColumnTest extends TestCase (ComplexValueForeignKey)col.getRowValue(row); if(rowId.equals(1)) { - checkUnsupportedValues(1, complexValueFk, + checkUnsupportedValues(1, complexValueFk, "RawData[(5) FF FE 62 61 7A]"); } else if(rowId.equals(2)) { checkUnsupportedValues(2, complexValueFk, "RawData[(5) FF FE 66 6F 6F]", "RawData[(5) FF FE 62 61 7A]"); @@ -340,12 +341,12 @@ public class ComplexColumnTest extends TestCase } else { assertTrue(false); } - } - + } + db.close(); } } - + private static void checkVersions( int cValId, ComplexValueForeignKey complexValueFk, String curValue, Object... versionInfos) @@ -376,7 +377,7 @@ public class ComplexColumnTest extends TestCase throws Exception { assertEquals(cValId, complexValueFk.get()); - + List attachments = complexValueFk.getAttachments(); if(fileNames.length == 0) { assertTrue(attachments.isEmpty()); @@ -388,12 +389,12 @@ public class ComplexColumnTest extends TestCase assertEquals(fname, a.getFileName()); assertEquals("txt", a.getFileType()); assertTrue(Arrays.equals(getFileBytes(fname), a.getFileData())); - assertTrue(Arrays.equals(getEncodedFileBytes(fname), + assertTrue(Arrays.equals(getEncodedFileBytes(fname), a.getEncodedFileData())); } } } - + private static void checkMultiValues( int cValId, ComplexValueForeignKey complexValueFk, Object... expectedValues) @@ -411,7 +412,7 @@ public class ComplexColumnTest extends TestCase SingleValue v = values.get(i); assertEquals(value, v.get()); } - } + } } private static void checkUnsupportedValues( @@ -434,7 +435,7 @@ public class ComplexColumnTest extends TestCase assertTrue(ColumnImpl.isRawData(rv)); assertEquals(value, rv.toString()); } - } + } } private static byte[] getFileBytes(String fname) throws Exception @@ -447,7 +448,7 @@ public class ComplexColumnTest extends TestCase } throw new RuntimeException("unexpected bytes"); } - + private static byte[] getEncodedFileBytes(String fname) throws Exception { if("test_data.txt".equals(fname)) { @@ -458,9 +459,9 @@ public class ComplexColumnTest extends TestCase } throw new RuntimeException("unexpected bytes"); } - + private static byte b(int i) { return (byte)i; } - + private static byte[] getAsciiBytes(String str) { try { return str.getBytes("US-ASCII"); @@ -468,7 +469,7 @@ public class ComplexColumnTest extends TestCase throw new RuntimeException(e); } } - + private static final byte[] TEST_ENC_BYTES = new byte[] { b(0x01),b(0x00),b(0x00),b(0x00),b(0x3A),b(0x00),b(0x00),b(0x00),b(0x78),b(0x5E),b(0x13),b(0x61),b(0x60),b(0x60),b(0x60),b(0x04),b(0x62),b(0x16),b(0x20),b(0x2E),b(0x61),b(0xA8),b(0x00),b(0x62), b(0x20),b(0x9D),b(0x91),b(0x59),b(0xAC),b(0x00),b(0x44),b(0xC5),b(0xF9),b(0xB9),b(0xA9),b(0x0A),b(0x25),b(0xA9),b(0xC5),b(0x25),b(0x0A),b(0x29),b(0x89),b(0x25),b(0x89),b(0x0A),b(0x69),b(0xF9), @@ -476,7 +477,7 @@ public class ComplexColumnTest extends TestCase }; private static final byte[] TEST_BYTES = getAsciiBytes("this is some test data for attachment."); - + private static final byte[] TEST2_ENC_BYTES = new byte[] { b(0x01),b(0x00),b(0x00),b(0x00),b(0x3F),b(0x00),b(0x00),b(0x00),b(0x78),b(0x5E),b(0x13),b(0x61),b(0x60),b(0x60),b(0x60),b(0x04),b(0x62),b(0x16),b(0x20),b(0x2E),b(0x61),b(0xA8),b(0x00),b(0x62), b(0x20),b(0x9D),b(0x91),b(0x59),b(0xAC),b(0x00),b(0x44),b(0xC5),b(0xF9),b(0xB9),b(0xA9),b(0x0A),b(0xB9),b(0xF9),b(0x45),b(0xA9),b(0x0A),b(0x25),b(0xA9),b(0xC5),b(0x25),b(0x0A),b(0x29),b(0x89), @@ -484,5 +485,5 @@ public class ComplexColumnTest extends TestCase }; private static final byte[] TEST2_BYTES = getAsciiBytes("this is some more test data for attachment."); - + } diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index 209a63d..1aa2483 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -52,6 +52,7 @@ import static com.healthmarketscience.jackcess.TestUtil.*; /** * @author Tim McCune */ +@SuppressWarnings("deprecation") public class DatabaseTest extends TestCase { public DatabaseTest(String name) throws Exception { diff --git a/src/test/java/com/healthmarketscience/jackcess/TestUtil.java b/src/test/java/com/healthmarketscience/jackcess/TestUtil.java index 0bef2e0..b5db277 100644 --- a/src/test/java/com/healthmarketscience/jackcess/TestUtil.java +++ b/src/test/java/com/healthmarketscience/jackcess/TestUtil.java @@ -53,6 +53,7 @@ import org.junit.Assert; * * @author James Ahlborn */ +@SuppressWarnings("deprecation") public class TestUtil { public static final TimeZone TEST_TZ = -- cgit v1.2.3