--- old/src/java.desktop/share/classes/java/awt/image/IndexColorModel.java 2016-04-13 13:49:40.954553000 +0530 +++ new/src/java.desktop/share/classes/java/awt/image/IndexColorModel.java 2016-04-13 13:49:40.374263000 +0530 @@ -28,6 +28,8 @@ 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} @@ -1532,4 +1534,81 @@ + " isAlphaPre = "+isAlphaPremultiplied ); } + + /** + * Tests if the specified {@code Object} is an instance + * of {@code IndexColorModel} and equals this + * {@code IndexColorModel}. + * @param obj the {@code Object} to test for equality + * @return {@code true} if the specified {@code Object} + * is an instance of {@code IndexColorModel} and equals this + * {@code IndexColorModel}; {@code false} otherwise. + */ + @Override + public boolean equals(Object obj) { + + if (!(obj instanceof IndexColorModel)) { + return false; + } + + IndexColorModel cm = (IndexColorModel) obj; + if (this == cm) { + return true; + } + + if (!super.equals(obj)) { + return false; + } + + 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 hash = 7; + hash = 43 * hash + super.hashCode(); + hash = 43 * hash + Arrays.hashCode(this.rgb); + hash = 43 * hash + this.map_size; + hash = 43 * hash + this.transparent_index; + hash = 43 * hash + Objects.hashCode(this.validBits); + return hash; + } } --- old/src/java.desktop/share/classes/java/awt/image/ColorModel.java 2016-04-13 13:49:42.731441000 +0530 +++ new/src/java.desktop/share/classes/java/awt/image/ColorModel.java 2016-04-13 13:49:42.135143000 +0530 @@ -31,7 +31,7 @@ import sun.java2d.cmm.CMSManager; import sun.java2d.cmm.ColorTransform; import sun.java2d.cmm.PCMM; -import java.awt.Toolkit; +import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.WeakHashMap; @@ -1449,6 +1449,7 @@ * is an instance of {@code ColorModel} and equals this * {@code ColorModel}; {@code false} otherwise. */ + @Override public boolean equals(Object obj) { if (!(obj instanceof ColorModel)) { return false; @@ -1487,23 +1488,16 @@ * * @return a hash code for this ColorModel. */ + @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; + int hash = 7; + hash = 89 * hash + this.pixel_bits; + hash = 89 * hash + Arrays.hashCode(this.nBits); + hash = 89 * hash + this.transparency; + hash = 89 * hash + (this.supportsAlpha ? 1 : 0); + hash = 89 * hash + (this.isAlphaPremultiplied ? 1 : 0); + hash = 89 * hash + this.numComponents; + return hash; } /** --- /dev/null 2016-04-13 13:44:01.990663000 +0530 +++ new/test/java/awt/image/IndexColorModel/IndexColorModelEqualsTest.java 2016-04-13 13:49:43.932040999 +0530 @@ -0,0 +1,328 @@ +/* + * Copyright (c) 2016, 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. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 7107905 + * @summary Test verifies whether equals() and hashCode() methods in + * IndexColorModel works properly for IndexColorModel unique + * properties. + * @run main IndexColorModelEqualsTest + */ + +import java.awt.image.DataBuffer; +import java.awt.image.IndexColorModel; +import java.math.BigInteger; + +public class IndexColorModelEqualsTest { + + private static void testColorMapEquality() { + // test with different cmap values. + IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3}, + 0, true, -1, DataBuffer.TYPE_BYTE); + IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {4, 5, 6}, + 0, true, -1, DataBuffer.TYPE_BYTE); + if (model1.equals(model2)) { + throw new RuntimeException("equals() method is determining" + + " ColorMap equality improperly"); + } + if (model2.equals(model1)) { + throw new RuntimeException("equals() method is determining" + + " ColorMap equality improperly"); + } + } + + private static void testSizeEquality() { + // test with different size for cmap. + IndexColorModel model1 = new IndexColorModel(8, 4, + new int[] {1, 2, 3, 4}, + 0, true, -1, DataBuffer.TYPE_BYTE); + IndexColorModel model2 = new IndexColorModel(8, 3, + new int[] {1, 2, 3}, + 0, true, -1, DataBuffer.TYPE_BYTE); + if (model1.equals(model2)) { + throw new RuntimeException("equals() method is determining" + + " Map size equality improperly"); + } + if (model2.equals(model1)) { + throw new RuntimeException("equals() method is determining" + + " Map size equality improperly"); + } + } + + private static void testTransparentIndexEquality() { + // test with different values for transparent_index. + IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3}, + 0, true, 1, DataBuffer.TYPE_BYTE); + IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {1, 2, 3}, + 0, true, 2, DataBuffer.TYPE_BYTE); + if (model1.equals(model2)) { + throw new RuntimeException("equals() method is determining" + + " TransparentIndex equality improperly"); + } + if (model2.equals(model1)) { + throw new RuntimeException("equals() method is determining" + + " TransparentIndex equality improperly"); + } + } + + private static void testValidPixelsEquality() { + // test with different valid pixels. + /* + * In setRGBs() function of IndexColorModel we override + * transparent_index value to map to pixel value if alpha is 0x00 + * so we should have atleast minimum alpha value to verify + * equality of validBits thats why we have color value as + * 16777216(2 ^ 24). + */ + int color = 16777216; + IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color, + color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1")); + IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color, + color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("2")); + if (model1.equals(model2)) { + throw new RuntimeException("equals() method is determining" + + " Valid pixels equality improperly"); + } + if (model2.equals(model1)) { + throw new RuntimeException("equals() method is determining" + + " Valid pixels equality improperly"); + } + } + + private static void testConstructor1() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b) + */ + IndexColorModel model1 = new IndexColorModel(8, 2, + new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2}); + IndexColorModel model2 = new IndexColorModel(8, 2, + new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2}); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testConstructor2() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b, + * byte[] a) + */ + IndexColorModel model1 = new IndexColorModel(8, 2, new byte[] {1, 2}, + new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2}); + IndexColorModel model2 = new IndexColorModel(8, 2, new byte[] {1, 2}, + new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2}); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testConstructor3() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b, + * int trans) + */ + IndexColorModel model1 = new IndexColorModel(8, 2, new byte[] {1, 2}, + new byte[] {1, 2}, new byte[] {1, 2}, 1); + IndexColorModel model2 = new IndexColorModel(8, 2, new byte[] {1, 2}, + new byte[] {1, 2}, new byte[] {1, 2}, 1); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testConstructor4() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, byte[] cmap, int start, + * boolean hasalpha) + */ + IndexColorModel model1 = new IndexColorModel(8, 1, + new byte[] {1, 2, 3, 4}, 0, true); + IndexColorModel model2 = new IndexColorModel(8, 1, + new byte[] {1, 2, 3, 4}, 0, true); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testConstructor5() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, byte[] cmap, int start, + * boolean hasalpha, int trans) + */ + IndexColorModel model1 = new IndexColorModel(8, 1, + new byte[] {1, 2, 3, 4}, 0, true, 0); + IndexColorModel model2 = new IndexColorModel(8, 1, + new byte[] {1, 2, 3, 4}, 0, true, 0); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testConstructor6() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, int[] cmap, int start, + * boolean hasalpha, int trans, int transferType) + */ + IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3}, + 0, true, -1, DataBuffer.TYPE_BYTE); + IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {1, 2, 3}, + 0, true, -1, DataBuffer.TYPE_BYTE); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testConstructor7() { + /* + * verify equality with constructor + * IndexColorModel(int bits, int size, int[] cmap, int start, + * int transferType, BigInteger validBits) + */ + /* + * In setRGBs() function of IndexColorModel we override + * transparent_index value to map to pixel value if alpha is 0x00 + * so we should have atleast minimum alpha value to keep + * both model1 and model2 same. + */ + int color = 16777216; + IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color, + color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1")); + IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color, + color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1")); + if (model1.equals(null)) { + throw new RuntimeException("equals(null) returns true"); + } + if (!(model1.equals(model2))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (!(model2.equals(model1))) { + throw new RuntimeException("equals() method is not working" + + " properly"); + } + if (model1.hashCode() != model2.hashCode()) { + throw new RuntimeException("HashCode is not same for same" + + " IndexColorModels"); + } + } + + private static void testSameIndexColorModel() { + testConstructor1(); + testConstructor2(); + testConstructor3(); + testConstructor4(); + testConstructor5(); + testConstructor6(); + testConstructor7(); + } + public static void main(String[] args) { + /* test whether equals() method works properly for parameters + * unique to IndexColorModel. + */ + testColorMapEquality(); + testSizeEquality(); + testTransparentIndexEquality(); + testValidPixelsEquality(); + // verify same IndexColorModel equality using different constructors. + testSameIndexColorModel(); + } +}