From: Nick Burch Date: Wed, 13 Jan 2010 15:42:53 +0000 (+0000) Subject: Remove some generics warnings from the HPSF custom properties and sections parts X-Git-Tag: REL_3_7_BETA1~140 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=878e9880a532de3893e4df239f60a949f0fec2db;p=poi.git Remove some generics warnings from the HPSF custom properties and sections parts git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@898804 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hpsf/CustomProperties.java b/src/java/org/apache/poi/hpsf/CustomProperties.java index a3242057ba..83858eb736 100644 --- a/src/java/org/apache/poi/hpsf/CustomProperties.java +++ b/src/java/org/apache/poi/hpsf/CustomProperties.java @@ -49,21 +49,26 @@ import org.apache.poi.hpsf.wellknown.PropertyIDMap; *

This class is not thread-safe; concurrent access to instances of this * class must be synchronized.

* + *

While this class is roughly HashMap, that's the + * internal representation. To external calls, it should appear as + * HashMap mapping between Names and Custom Property Values.

+ * * @author Rainer Klute <klute@rainer-klute.de> */ -public class CustomProperties extends HashMap +@SuppressWarnings("serial") +public class CustomProperties extends HashMap { /** *

Maps property IDs to property names.

*/ - private Map dictionaryIDToName = new HashMap(); + private Map dictionaryIDToName = new HashMap(); /** *

Maps property names to property IDs.

*/ - private Map dictionaryNameToID = new HashMap(); + private Map dictionaryNameToID = new HashMap(); /** *

Tells whether this object is pure or not.

@@ -71,25 +76,19 @@ public class CustomProperties extends HashMap private boolean isPure = true; - /** *

Puts a {@link CustomProperty} into this map. It is assumed that the * {@link CustomProperty} already has a valid ID. Otherwise use * {@link #put(CustomProperty)}.

*/ - public Object put(final Object name, final Object customProperty) throws ClassCastException + public CustomProperty put(final String name, final CustomProperty cp) { - final CustomProperty cp = (CustomProperty) customProperty; if (name == null) { /* Ignoring a property without a name. */ isPure = false; return null; } - if (!(name instanceof String)) - throw new ClassCastException("The name of a custom property must " + - "be a java.lang.String, but it is a " + - name.getClass().getName()); if (!(name.equals(cp.getName()))) throw new IllegalArgumentException("Parameter \"name\" (" + name + ") and custom property's name (" + cp.getName() + @@ -97,13 +96,13 @@ public class CustomProperties extends HashMap /* Register name and ID in the dictionary. Mapping in both directions is possible. If there is already a */ final Long idKey = Long.valueOf(cp.getID()); - final Object oldID = dictionaryNameToID.get(name); + final Long oldID = dictionaryNameToID.get(name); dictionaryIDToName.remove(oldID); dictionaryNameToID.put(name, idKey); dictionaryIDToName.put(idKey, name); /* Put the custom property into this map. */ - final Object oldCp = super.remove(oldID); + final CustomProperty oldCp = super.remove(oldID); super.put(idKey, cp); return oldCp; } @@ -138,9 +137,9 @@ public class CustomProperties extends HashMap else { long max = 1; - for (final Iterator i = dictionaryIDToName.keySet().iterator(); i.hasNext();) + for (final Iterator i = dictionaryIDToName.keySet().iterator(); i.hasNext();) { - final long id = ((Long) i.next()).longValue(); + final long id = i.next().longValue(); if (id > max) max = id; } @@ -295,12 +294,28 @@ public class CustomProperties extends HashMap /** * Returns a set of all the names of our - * custom properties + * custom properties. Equivalent to + * {@link #nameSet()} */ public Set keySet() { return dictionaryNameToID.keySet(); } + /** + * Returns a set of all the names of our + * custom properties + */ + public Set nameSet() { + return dictionaryNameToID.keySet(); + } + + /** + * Returns a set of all the IDs of our + * custom properties + */ + public Set idSet() { + return dictionaryNameToID.keySet(); + } /** @@ -325,14 +340,44 @@ public class CustomProperties extends HashMap * * @return the dictionary. */ - Map getDictionary() + Map getDictionary() { return dictionaryIDToName; } - /** + * Checks against both String Name and Long ID + */ + public boolean containsKey(Object key) { + if(key instanceof Long) { + return super.containsKey((Long)key); + } + if(key instanceof String) { + return super.containsKey((Long)dictionaryNameToID.get(key)); + } + return false; + } + + /** + * Checks against both the property, and its values. + */ + public boolean containsValue(Object value) { + if(value instanceof CustomProperty) { + return super.containsValue((CustomProperty)value); + } else { + for(CustomProperty cp : super.values()) { + if(cp.getValue() == value) { + return true; + } + } + } + return false; + } + + + + /** *

Gets the codepage.

* * @return the codepage or -1 if the codepage is undefined. @@ -340,9 +385,9 @@ public class CustomProperties extends HashMap public int getCodepage() { int codepage = -1; - for (final Iterator i = this.values().iterator(); codepage == -1 && i.hasNext();) + for (final Iterator i = this.values().iterator(); codepage == -1 && i.hasNext();) { - final CustomProperty cp = (CustomProperty) i.next(); + final CustomProperty cp = i.next(); if (cp.getID() == PropertyIDMap.PID_CODEPAGE) codepage = ((Integer) cp.getValue()).intValue(); } diff --git a/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java b/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java index bfee086e45..0e98396ad4 100644 --- a/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java +++ b/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java @@ -577,7 +577,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet { cps = new CustomProperties(); final Section section = (Section) getSections().get(1); - final Map dictionary = section.getDictionary(); + final Map dictionary = section.getDictionary(); final Property[] properties = section.getProperties(); int propertyCount = 0; for (int i = 0; i < properties.length; i++) @@ -588,7 +588,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet { propertyCount++; final CustomProperty cp = new CustomProperty(p, - (String) dictionary.get(Long.valueOf(id))); + dictionary.get(Long.valueOf(id))); cps.put(cp.getName(), cp); } } @@ -607,7 +607,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet { ensureSection2(); final MutableSection section = (MutableSection) getSections().get(1); - final Map dictionary = customProperties.getDictionary(); + final Map dictionary = customProperties.getDictionary(); section.clear(); /* Set the codepage. If both custom properties and section have a @@ -621,9 +621,9 @@ public class DocumentSummaryInformation extends SpecialPropertySet customProperties.setCodepage(cpCodepage); section.setCodepage(cpCodepage); section.setDictionary(dictionary); - for (final Iterator i = customProperties.values().iterator(); i.hasNext();) + for (final Iterator i = customProperties.values().iterator(); i.hasNext();) { - final Property p = (Property) i.next(); + final Property p = i.next(); section.setProperty(p); } } diff --git a/src/java/org/apache/poi/hpsf/MutableSection.java b/src/java/org/apache/poi/hpsf/MutableSection.java index 0e521dc44d..3f7c01b3b0 100644 --- a/src/java/org/apache/poi/hpsf/MutableSection.java +++ b/src/java/org/apache/poi/hpsf/MutableSection.java @@ -53,7 +53,7 @@ public class MutableSection extends Section * decision has been taken when specifying the "properties" field * as an Property[]. It should have been a {@link java.util.List}.

*/ - private List preprops; + private List preprops; @@ -74,7 +74,7 @@ public class MutableSection extends Section dirty = true; formatID = null; offset = -1; - preprops = new LinkedList(); + preprops = new LinkedList(); } @@ -145,7 +145,7 @@ public class MutableSection extends Section public void setProperties(final Property[] properties) { this.properties = properties; - preprops = new LinkedList(); + preprops = new LinkedList(); for (int i = 0; i < properties.length; i++) preprops.add(properties[i]); dirty = true; @@ -276,8 +276,8 @@ public class MutableSection extends Section */ public void removeProperty(final long id) { - for (final Iterator i = preprops.iterator(); i.hasNext();) - if (((Property) i.next()).getID() == id) + for (final Iterator i = preprops.iterator(); i.hasNext();) + if (i.next().getID() == id) { i.remove(); break; @@ -423,12 +423,10 @@ public class MutableSection extends Section } /* Sort the property list by their property IDs: */ - Collections.sort(preprops, new Comparator() + Collections.sort(preprops, new Comparator() { - public int compare(final Object o1, final Object o2) + public int compare(final Property p1, final Property p2) { - final Property p1 = (Property) o1; - final Property p2 = (Property) o2; if (p1.getID() < p2.getID()) return -1; else if (p1.getID() == p2.getID()) @@ -440,7 +438,7 @@ public class MutableSection extends Section /* Write the properties and the property list into their respective * streams: */ - for (final ListIterator i = preprops.listIterator(); i.hasNext();) + for (final ListIterator i = preprops.listIterator(); i.hasNext();) { final MutableProperty p = (MutableProperty) i.next(); final long id = p.getID(); @@ -502,14 +500,14 @@ public class MutableSection extends Section * @exception IOException if an I/O exception occurs. */ private static int writeDictionary(final OutputStream out, - final Map dictionary, final int codepage) + final Map dictionary, final int codepage) throws IOException { int length = TypeWriter.writeUIntToStream(out, dictionary.size()); - for (final Iterator i = dictionary.keySet().iterator(); i.hasNext();) + for (final Iterator i = dictionary.keySet().iterator(); i.hasNext();) { - final Long key = (Long) i.next(); - final String value = (String) dictionary.get(key); + final Long key = i.next(); + final String value = dictionary.get(key); if (codepage == Constants.CP_UNICODE) { @@ -617,21 +615,11 @@ public class MutableSection extends Section * * @see Section#getDictionary() */ - public void setDictionary(final Map dictionary) + public void setDictionary(final Map dictionary) throws IllegalPropertySetDataException { if (dictionary != null) { - for (final Iterator i = dictionary.keySet().iterator(); - i.hasNext();) - if (!(i.next() instanceof Long)) - throw new IllegalPropertySetDataException - ("Dictionary keys must be of type Long."); - for (final Iterator i = dictionary.values().iterator(); - i.hasNext();) - if (!(i.next() instanceof String)) - throw new IllegalPropertySetDataException - ("Dictionary values must be of type String."); this.dictionary = dictionary; /* Set the dictionary property (ID 0). Please note that the second diff --git a/src/java/org/apache/poi/hpsf/Section.java b/src/java/org/apache/poi/hpsf/Section.java index 917929a64a..2c49f02231 100644 --- a/src/java/org/apache/poi/hpsf/Section.java +++ b/src/java/org/apache/poi/hpsf/Section.java @@ -42,7 +42,7 @@ public class Section *

Maps property IDs to section-private PID strings. These * strings can be found in the property with ID 0.

*/ - protected Map dictionary; + protected Map dictionary; /** *

The section's format ID, {@link #getFormatID}.

@@ -244,10 +244,10 @@ public class Section /* Look for the codepage. */ int codepage = -1; - for (final Iterator i = propertyList.iterator(); + for (final Iterator i = propertyList.iterator(); codepage == -1 && i.hasNext();) { - ple = (PropertyListEntry) i.next(); + ple = i.next(); /* Read the codepage if the property ID is 1. */ if (ple.id == PropertyIDMap.PID_CODEPAGE) @@ -271,9 +271,9 @@ public class Section /* Pass 2: Read all properties - including the codepage property, * if available. */ int i1 = 0; - for (final Iterator i = propertyList.iterator(); i.hasNext();) + for (final Iterator i = propertyList.iterator(); i.hasNext();) { - ple = (PropertyListEntry) i.next(); + ple = i.next(); Property p = new Property(ple.id, src, this.offset + ple.offset, ple.length, codepage); @@ -294,7 +294,7 @@ public class Section *

Represents an entry in the property list and holds a property's ID and * its offset from the section's beginning.

*/ - class PropertyListEntry implements Comparable + class PropertyListEntry implements Comparable { int id; int offset; @@ -307,11 +307,9 @@ public class Section * * @see Comparable#compareTo(java.lang.Object) */ - public int compareTo(final Object o) + public int compareTo(final PropertyListEntry o) { - if (!(o instanceof PropertyListEntry)) - throw new ClassCastException(o.toString()); - final int otherOffset = ((PropertyListEntry) o).offset; + final int otherOffset = o.offset; if (offset < otherOffset) return -1; else if (offset == otherOffset) @@ -630,7 +628,7 @@ public class Section * @return the dictionary or null if the section does not have * a dictionary. */ - public Map getDictionary() + public Map getDictionary() { return dictionary; } diff --git a/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java b/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java index 450e8d3b4b..14bb16887d 100644 --- a/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java +++ b/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java @@ -57,9 +57,9 @@ public class HPSFPropertiesExtractor extends POITextExtractor { // Now custom ones CustomProperties cps = dsi == null ? null : dsi.getCustomProperties(); if(cps != null) { - Iterator keys = cps.keySet().iterator(); + Iterator keys = cps.nameSet().iterator(); while(keys.hasNext()) { - String key = (String)keys.next(); + String key = keys.next(); String val = getPropertyValueText( cps.get(key) ); text.append(key + " = " + val + "\n"); } diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java b/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java index ad6e1237cf..8c95fdb7bf 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java @@ -348,8 +348,25 @@ public class TestWriteWellKnown extends TestCase { customProperties.put("min_Long", MIN_LONG); customProperties.put("max_Double", MAX_DOUBLE); customProperties.put("min_Double", MIN_DOUBLE); + + // Check the keys went in + assertTrue(customProperties.containsKey("Schl\u00fcssel \u00e4")); + assertTrue(customProperties.containsKey("Boolean")); + + // Check the values went in + assertEquals("Wert \u00e4", customProperties.get("Schl\u00fcssel \u00e4")); + assertEquals(Boolean.TRUE, customProperties.get("Boolean")); + assertTrue(customProperties.containsValue(Boolean.TRUE)); + assertTrue(customProperties.containsValue("Wert \u00e4")); + + // Check that things that aren't in aren't in + assertFalse(customProperties.containsKey("False Boolean")); + assertFalse(customProperties.containsValue(Boolean.FALSE)); + + // Save as our custom properties dsi.setCustomProperties(customProperties); + /* Write the summary information stream and the document summary * information stream to the POI filesystem. */ si.write(dir, siEntry.getName());