< prev index next >

src/java.desktop/share/classes/java/awt/image/IndexColorModel.java

Print this page

        

@@ -26,10 +26,12 @@
 package java.awt.image;
 
 import java.awt.Transparency;
 import java.awt.color.ColorSpace;
 import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Objects;
 
 /**
  * The {@code IndexColorModel} class is a {@code ColorModel}
  * class that works with pixel values consisting of a
  * single sample that is an index into a fixed colormap in the default

@@ -127,10 +129,11 @@
     private int map_size;
     private int pixel_mask;
     private int transparent_index = -1;
     private boolean allgrayopaque;
     private BigInteger validBits;
+    private volatile int hashCode;
 
     private sun.awt.image.BufImgSurfaceData.ICMColorData colorData = null;
 
     private static int[] opaqueBits = {8, 8, 8};
     private static int[] alphaBits = {8, 8, 8, 8};

@@ -1530,6 +1533,95 @@
                          + " transIndex   = "+transparent_index
                          + " has alpha = "+supportsAlpha
                          + " isAlphaPre = "+isAlphaPremultiplied
                          );
     }
+
+    /**
+     * Tests if the specified {@code Object} equals this
+     * {@code IndexColorModel}.
+     * In order to protect the symmetry property of
+     * {@code (a.equals(b) == b.equals(a))},
+     * the target object must be the same class (and not a subclass)
+     * as this object to evaluate as {equals}.
+     * <p>
+     * The target object and this object will be equal if all of the
+     * properties checked by the {equals} method of {ColorModel} are
+     * valid, additionally we verify the following properties for
+     * IndexColorModel:
+     *
+     * <ul>
+     * <li>ColorMap size.
+     * <li>Index of a transparent pixel in {@code IndexColorModel}
+     * <li>ColorMap values.
+     * <li>The list of valid pixel indices in ColorMap.
+     * </ul>
+     * <p>
+     * @param obj the {@code Object} to test for equality
+     * @return {@code true} if the specified {@code Object}
+     * equals this {@code IndexColorModel}; {@code false} otherwise.
+     */
+    @Override
+    public boolean equals(Object obj) {
+        /*
+         * We verify the type of argument obj in super.equals() where we check
+         * for equality of class name.
+         */
+        if (!super.equals(obj)) {
+            return false;
+        }
+
+        IndexColorModel cm = (IndexColorModel) obj;
+        if (map_size != cm.map_size ||
+            transparent_index != cm.transparent_index)
+        {
+            return false;
+        }
+
+        // verify whether we have to check equality of all bits in validBits
+        boolean testValidBits;
+        if (validBits == cm.validBits) {
+            testValidBits = false;
+        } else if (validBits == null || cm.validBits == null) {
+            return false;
+        } else if (validBits.equals(cm.validBits)) {
+            testValidBits = false;
+        } else {
+            testValidBits = true;
+        }
+
+        if (testValidBits) {
+            for (int i = 0; i < map_size; i++) {
+                if (rgb[i] != cm.rgb[i] ||
+                    validBits.testBit(i) != cm.validBits.testBit(i))
+                {
+                    return false;
+                }
+            }
+        } else {
+            for (int i = 0; i < map_size; i++) {
+                if (rgb[i] != cm.rgb[i]) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Returns the hash code for IndexColorModel.
+     *
+     * @return    a hash code for IndexColorModel
+     */
+    @Override
+    public int hashCode() {
+        int result = hashCode;
+        if (result == 0) {
+            result = super.hashCode();
+            result = 43 * result + Arrays.hashCode(this.rgb);
+            result = 43 * result + this.map_size;
+            result = 43 * result + this.transparent_index;
+            hashCode = result;
+        }
+        return result;
+    }
 }
< prev index next >