< prev index next >
src/java.desktop/share/classes/java/awt/image/ColorModel.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 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
@@ -29,14 +29,14 @@
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import sun.java2d.cmm.CMSManager;
import sun.java2d.cmm.ColorTransform;
import sun.java2d.cmm.PCMM;
-import java.awt.Toolkit;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
+import java.util.Arrays;
/**
* The {@code ColorModel} abstract class encapsulates the
* methods for translating a pixel value to color components
* (for example, red, green, and blue) and an alpha component.
@@ -360,11 +360,18 @@
else {
this.isAlphaPremultiplied = isAlphaPremultiplied;
this.transparency = transparency;
}
- nBits = bits.clone();
+ /*
+ * We need significant bits value only for the length
+ * of number of components, so we truncate remaining part.
+ * It also helps in hashCode calculation since bits[] can contain
+ * different values after the length of number of components between
+ * two ColorModels.
+ */
+ nBits = Arrays.copyOf(bits, numComponents);
this.pixel_bits = pixel_bits;
if (pixel_bits <= 0) {
throw new IllegalArgumentException("Number of pixel bits must "+
"be > 0");
}
@@ -1439,73 +1446,57 @@
return getNormalizedComponents(components, 0,
normComponents, normOffset);
}
/**
- * Tests if the specified {@code Object} is an instance of
- * {@code ColorModel} and if it equals this
- * {@code ColorModel}.
- * @param obj the {@code Object} to test for equality
- * @return {@code true} if the specified {@code Object}
- * is an instance of {@code ColorModel} and equals this
- * {@code ColorModel}; {@code false} otherwise.
+ * This method simply delegates to the default implementation in {@code Object}
+ * which is identical to an {@code ==} test since this class cannot enforce the
+ * issues of a proper equality test among multiple independent subclass
+ * branches.
+ * Subclasses are encouraged to override this method and provide equality
+ * testing for their own properties in addition to equality tests for the
+ * following common base properties of {@code ColorModel}:
+ * <ul>
+ * <li>Support for alpha component.</li>
+ * <li>Is alpha premultiplied.</li>
+ * <li>Number of bits per pixel.</li>
+ * <li>Type of transparency like Opaque, Bitmask or Translucent.</li>
+ * <li>Number of components in a pixel.</li>
+ * <li>{@code ColorSpace} type.</li>
+ * <li>Type of the array used to represent pixel values.</li>
+ * <li>Number of significant bits per color and alpha component.</li>
+ * </ul>
+ * @param obj the reference object with which to compare.
+ * @return {@code true} if this object is the same as the obj
+ * argument; {@code false} otherwise.
*/
+ @Override
public boolean equals(Object obj) {
- if (!(obj instanceof ColorModel)) {
- return false;
- }
- ColorModel cm = (ColorModel) obj;
-
- if (this == cm) {
- return true;
- }
- if (supportsAlpha != cm.hasAlpha() ||
- isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
- pixel_bits != cm.getPixelSize() ||
- transparency != cm.getTransparency() ||
- numComponents != cm.getNumComponents())
- {
- 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;
+ return super.equals(obj);
}
/**
- * Returns the hash code for this ColorModel.
- *
- * @return a hash code for this ColorModel.
+ * This method simply delegates to the default implementation in {@code Object}
+ * which returns the system ID for the class.
+ * Subclasses are encouraged to override this method and provide a hash
+ * for their own properties in addition to hashing the values of the
+ * following common base properties of {@code ColorModel}:
+ * <ul>
+ * <li>Support for alpha component.</li>
+ * <li>Is alpha premultiplied.</li>
+ * <li>Number of bits per pixel.</li>
+ * <li>Type of transparency like Opaque, Bitmask or Translucent.</li>
+ * <li>Number of components in a pixel.</li>
+ * <li>{@code ColorSpace} type.</li>
+ * <li>Type of the array used to represent pixel values.</li>
+ * <li>Number of significant bits per color and alpha component.</li>
+ * </ul>
+ * @return a hash code value for this object.
*/
+ @Override
public int hashCode() {
-
- int result = 0;
-
- result = (supportsAlpha ? 2 : 3) +
- (isAlphaPremultiplied ? 4 : 5) +
- pixel_bits * 6 +
- transparency * 7 +
- numComponents * 8;
-
- if (nBits != null) {
- for (int i = 0; i < numComponents; i++) {
- result = result + nBits[i] * (i + 9);
- }
- }
-
- return result;
+ return super.hashCode();
}
/**
* Returns the {@code ColorSpace} associated with this
* {@code ColorModel}.
< prev index next >