From 73e9bc7c93a61fb9f5320757b43d6850533d2482 Mon Sep 17 00:00:00 2001 From: Rainer Klute Date: Sun, 3 Aug 2003 20:16:46 +0000 Subject: [PATCH] Writing preparations git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353285 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hpsf/ClassID.java | 23 ++++++++ src/java/org/apache/poi/hpsf/Property.java | 22 +++++++- src/java/org/apache/poi/hpsf/PropertySet.java | 38 ++++++++++++- src/java/org/apache/poi/hpsf/Section.java | 35 +++++++++++- src/java/org/apache/poi/hpsf/Util.java | 56 +++++++++++++++++++ 5 files changed, 168 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/ClassID.java b/src/java/org/apache/poi/hpsf/ClassID.java index 1aff51b9b8..898364fce3 100644 --- a/src/java/org/apache/poi/hpsf/ClassID.java +++ b/src/java/org/apache/poi/hpsf/ClassID.java @@ -205,4 +205,27 @@ public class ClassID dst[i + offset] = bytes[i]; } + + + /** + *

Checks whether this ClassID is equal to another + * object.

+ * + * @param o the object to compare this PropertySet with + * @return true if the objects are equal, else + * false.

+ */ + public boolean equals(final Object o) + { + if (o == null || !(o instanceof ClassID)) + return false; + final ClassID cid = (ClassID) o; + if (bytes.length != cid.bytes.length) + return false; + for (int i = 0; i < bytes.length; i++) + if (bytes[i] != cid.bytes[i]) + return false; + return true; + } + } diff --git a/src/java/org/apache/poi/hpsf/Property.java b/src/java/org/apache/poi/hpsf/Property.java index b6d92ebeb2..227f69d163 100644 --- a/src/java/org/apache/poi/hpsf/Property.java +++ b/src/java/org/apache/poi/hpsf/Property.java @@ -100,7 +100,7 @@ public class Property private static final int CP_UNICODE = 1200; /**

The property's ID.

*/ - private int id; + protected int id; /** @@ -116,7 +116,7 @@ public class Property /**

The property's type.

*/ - private long type; + protected long type; /** @@ -132,7 +132,7 @@ public class Property /**

The property's value.

*/ - private Object value; + protected Object value; /** @@ -191,6 +191,15 @@ public class Property + /** + *

Creates an empty property. It must be filled using the set method to + * be usable.

+ */ + protected Property() + {} + + + /** *

Reads a dictionary.

* @@ -275,4 +284,11 @@ public class Property throw new UnsupportedOperationException("FIXME: Not yet implemented."); } + + + public boolean equals(Object o) + { + throw new UnsupportedOperationException("FIXME: Not yet implemented."); + } + } diff --git a/src/java/org/apache/poi/hpsf/PropertySet.java b/src/java/org/apache/poi/hpsf/PropertySet.java index a0b2f9bc3a..a039ff9bc0 100644 --- a/src/java/org/apache/poi/hpsf/PropertySet.java +++ b/src/java/org/apache/poi/hpsf/PropertySet.java @@ -54,10 +54,12 @@ */ package org.apache.poi.hpsf; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; + import org.apache.poi.hpsf.wellknown.SectionIDMap; import org.apache.poi.util.LittleEndian; @@ -636,4 +638,38 @@ public class PropertySet return ((Section) sections.get(0)); } + + + /** + *

Returns true if the PropertySet is equal + * to the specified parameter, else false.

+ * + * @param o the object to compare this PropertySet with + */ + public boolean equals(final Object o) + { + if (o == null || !(o instanceof PropertySet)) + return false; + final PropertySet ps = (PropertySet) o; + int byteOrder1 = ps.getByteOrder(); + int byteOrder2 = getByteOrder(); + ClassID classId1 = ps.getClassID(); + ClassID classID2 = getClassID(); + int format1 = ps.getFormat(); + int format2 = getFormat(); + int osVersion1 = ps.getOSVersion(); + int osVersion2 = getOSVersion(); + int sectionCount1 = ps.getSectionCount(); + int sectionCount2 = getSectionCount(); + if (byteOrder1 != byteOrder2 || + !classId1.equals(classID2) || + format1 != format2 || + osVersion1 != osVersion2 || + sectionCount1 != sectionCount2) + return false; + + /* Compare the sections: */ + return Util.equals(getSections(), ps.getSections()); + } + } diff --git a/src/java/org/apache/poi/hpsf/Section.java b/src/java/org/apache/poi/hpsf/Section.java index bd8bcf0d6f..abeed3a7cd 100644 --- a/src/java/org/apache/poi/hpsf/Section.java +++ b/src/java/org/apache/poi/hpsf/Section.java @@ -138,7 +138,7 @@ public class Section /** * @see #getPropertyCount */ - protected int propertyCount; + private int propertyCount; /** @@ -156,7 +156,7 @@ public class Section /** * @see #getProperties */ - protected Property[] properties; + private Property[] properties; /** @@ -169,6 +169,16 @@ public class Section return properties; } + /** + *

Sets this section's properties.

+ * + * @param properties This section's new properties. + */ + protected void setProperties(final Property[] properties) + { + this.properties = properties; + } + /** @@ -423,4 +433,25 @@ public class Section return s; } + + + /** + *

Checks whether this section is equal to another object.

+ * + * @param o The object to cpmpare this section with + * @return true if the objects are equal, false if + * not + */ + public boolean equals(final Object o) + { + if (o == null || !(o instanceof Section)) + return false; + final Section s = (Section) o; + if (!s.getFormatID().equals(getFormatID())) + return false; + if (s.getPropertyCount() != getPropertyCount()) + return false; + return Util.equals(s.getProperties(), getProperties()); + } + } diff --git a/src/java/org/apache/poi/hpsf/Util.java b/src/java/org/apache/poi/hpsf/Util.java index 00182e2259..4a92633d0b 100644 --- a/src/java/org/apache/poi/hpsf/Util.java +++ b/src/java/org/apache/poi/hpsf/Util.java @@ -54,6 +54,7 @@ */ package org.apache.poi.hpsf; +import java.util.Collection; import java.util.Date; /** @@ -190,4 +191,59 @@ public class Util return new Date(ms_since_19700101); } + + + /** + *

Checks whether two collections are equal. Two collections + * C1 and C2 are equal, if the following conditions + * are true:

+ * + * + * + * @param c1 the first collection + * @param c2 the second collection + * @return true if the collections are equal, else + * false. + */ + public static boolean equals(final Collection c1, final Collection c2) + { + final Object[] o1 = c1.toArray(); + final Object[] o2 = c2.toArray(); + return internalEquals(o1, o2); + } + + public static boolean equals(final Object[] c1, final Object[] c2) + { + final Object[] o1 = (Object[]) c1.clone(); + final Object[] o2 = (Object[]) c2.clone(); + return internalEquals(o1, o2); + } + + private static boolean internalEquals(final Object[] o1, final Object[] o2) + { + for (int i1 = 0; i1 < o1.length; i1++) + { + boolean matchFound = false; + for (int i2 = 0; !matchFound && i2 < o1.length; i2++) + if (o1[i1].equals(o2[i2])) + { + matchFound = true; + o2[i2] = null; + } + if (!matchFound) + return false; + } + return true; + } + } -- 2.39.5