< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -25,10 +25,12 @@
 
 package java.awt.image;
 
 import java.awt.color.ColorSpace;
 import java.awt.color.ICC_ColorSpace;
+import java.util.Arrays;
+import java.util.Objects;
 
 /**
  * A {@code ColorModel} class that works with pixel values that
  * represent color and alpha information as separate samples and that
  * store each sample in a separate data element.  This class can be

@@ -198,10 +200,11 @@
     private boolean nonStdScale;
     private float[] min;
     private float[] diffMinMax;
     private float[] compOffset;
     private float[] compScale;
+    private volatile int hashCode;
 
     /**
      * Constructs a {@code ComponentColorModel} from the specified
      * parameters. Color components will be in the specified
      * {@code ColorSpace}.  The supported transfer types are

@@ -2925,24 +2928,69 @@
                                           raster.getHeight(), x, y,
                                           band);
     }
 
     /**
-     * Compares this color model with another for equality.
-     *
-     * @param obj The object to compare with this color model.
-     * @return {@code true} if the color model objects are equal,
-     * {@code false} if they are not.
+     * Tests if the specified {@code Object} is an instance
+     * of {@code ComponentColorModel} and equals this
+     * {@code ComponentColorModel}.
+     * @param obj the {@code Object} to test for equality
+     * @return {@code true} if the specified {@code Object}
+     * is an instance of {@code ComponentColorModel} and equals this
+     * {@code ComponentColorModel}; {@code false} otherwise.
      */
+    @Override
     public boolean equals(Object obj) {
-        if (!super.equals(obj)) {
+        if (!(obj instanceof ComponentColorModel)) {
             return false;
         }
 
-        if (obj.getClass() !=  getClass()) {
+        ComponentColorModel cm = (ComponentColorModel) obj;
+        if (supportsAlpha != cm.hasAlpha() ||
+            isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
+            pixel_bits != cm.getPixelSize() ||
+            transparency != cm.getTransparency() ||
+            numComponents != cm.getNumComponents() ||
+            (!(colorSpace.equals(cm.colorSpace))) ||
+            transferType != cm.transferType)
+        {
             return false;
         }
 
+        int[] nb = cm.getComponentSize();
+
+        if ((nBits != null) && (nb != null)) {
+            for (int i = 0; i < numComponents; i++) {
+                if (nBits[i] != nb[i]) {
+                    return false;
+                }
+            }
+        } else {
+            return ((nBits == null) && (nb == null));
+        }
+
         return true;
     }
 
+    /**
+     * Returns the hash code for this ComponentColorModel.
+     *
+     * @return    a hash code for this ComponentColorModel.
+     */
+    @Override
+    public int hashCode() {
+        int result = hashCode;
+        if (result == 0) {
+            result = 7;
+            result = 89 * result + this.pixel_bits;
+            result = 89 * result + Arrays.hashCode(this.nBits);
+            result = 89 * result + this.transparency;
+            result = 89 * result + (this.supportsAlpha ? 1 : 0);
+            result = 89 * result + (this.isAlphaPremultiplied ? 1 : 0);
+            result = 89 * result + this.numComponents;
+            result = 89 * result + Objects.hashCode(this.colorSpace);
+            result = 89 * result + this.transferType;
+            hashCode = result;
+        }
+        return result;
+    }
 }
< prev index next >