This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 036c9fb30840330dbf22f84fd20b6964e74732f7 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Sat Dec 10 16:55:58 2022 +0100 Remove a hack for a Java 8 limitation fixed in Java 9. --- .../internal/coverage/j2d/ColorModelFactory.java | 31 ++---- .../sis/internal/coverage/j2d/ColorModelPatch.java | 121 --------------------- .../coverage/j2d/MultiBandsIndexColorModel.java | 43 +++++++- .../sis/internal/coverage/j2d/package-info.java | 2 +- 4 files changed, 47 insertions(+), 150 deletions(-) diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java index 9b4e076afc..fd6f9dfa21 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java +++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelFactory.java @@ -48,7 +48,7 @@ import org.apache.sis.util.Debug; * @author Martin Desruisseaux (IRD, Geomatys) * @author Johann Sorel (Geomatys) * @author Alexis Manin (Geomatys) - * @version 1.3 + * @version 1.4 * @since 1.0 * @module */ @@ -66,8 +66,7 @@ public final class ColorModelFactory { * * @see #unique(ColorModel) */ - @SuppressWarnings("rawtypes") // TODO: Remove after we removed ColorModelPatch. - private static final WeakHashSet<ColorModelPatch> CACHE = new WeakHashSet<>(ColorModelPatch.class); + private static final WeakHashSet<ColorModel> CACHE = new WeakHashSet<>(ColorModel.class); /** * A pool of color models previously created by {@link #createColorModel()}. @@ -245,7 +244,7 @@ public final class ColorModelFactory { final int[] nBits = { DataBuffer.getDataTypeSize(dataType) }; - return unique(new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, dataType)); + return CACHE.unique(new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, dataType)); } /* * Interpolates the colors in the color palette. Colors that do not fall @@ -391,7 +390,7 @@ public final class ColorModelFactory { cm = new MultiBandsIndexColorModel(bits, length, ARGB, 0, hasAlpha, transparent, dataType, numBands, visibleBand); } - return unique(cm); + return CACHE.unique(cm); } /** @@ -442,7 +441,7 @@ public final class ColorModelFactory { final ScaledColorSpace cs = new ScaledColorSpace(numComponents, visibleBand, minimum, maximum); cm = new ScaledColorModel(cs, dataType); } - return unique(cm); + return CACHE.unique(cm); } /** @@ -571,7 +570,7 @@ public final class ColorModelFactory { cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), numBits, hasAlpha, false, hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, DataBuffer.TYPE_BYTE); } - return unique(cm); + return CACHE.unique(cm); } /** @@ -620,7 +619,7 @@ public final class ColorModelFactory { // TODO: handle other color models. return Optional.empty(); } - return Optional.of(unique(subset)); + return Optional.of(CACHE.unique(subset)); } /** @@ -641,22 +640,6 @@ public final class ColorModelFactory { } } - /** - * Returns a unique instance of the given color model. - * This method is automatically invoked by {@code create(…)} methods in this class. - * - * @param <T> the type of the color model to share. - * @param cm the color model for which to get a unique instance. - * @return a unique (shared) instance of the given color model. - */ - private static <T extends ColorModel> T unique(T cm) { - // `CACHE` is null-safe and it is sometimes okay to return a null color model. - // ColorModelPatch is not null-safe, but it will be removed in a future version. - ColorModelPatch<T> c = new ColorModelPatch<>(cm); - c = CACHE.unique(c); - return c.cm; - } - /** * Returns a suggested type for an {@link IndexColorModel} of {@code mapSize} colors. * This method returns {@link DataBuffer#TYPE_BYTE} or {@link DataBuffer#TYPE_USHORT}. diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelPatch.java b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelPatch.java deleted file mode 100644 index 8fecb2a314..0000000000 --- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ColorModelPatch.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.sis.internal.coverage.j2d; - -import java.util.Arrays; -import java.util.Objects; -import java.awt.image.ColorModel; -import java.awt.image.IndexColorModel; -import org.apache.sis.util.Workaround; - - -/** - * Workaround for broken {@link ColorModel#equals(Object)} in Java 8 and before. - * This workaround will be removed after upgrade to Java 9. - * - * @author Martin Desruisseaux (Geomatys) - * @version 1.0 - * - * @see <a href="https://bugs.openjdk.java.net/browse/JDK-7107905"></a> - * @todo Delete after migration to JDK9. - */ -@Workaround(library = "JDK", version = "8") -final class ColorModelPatch<T extends ColorModel> { - /** - * The color model to share. - */ - final T cm; - - /** - * For internal use only. - */ - ColorModelPatch(final T cm) { - this.cm = cm; - } - - /** - * Returns {@code true} if the given color models are equal. The {@link ColorModel} class - * defines an {@code equals} method, but as of Java 6 that method does not compare every - * attributes. For example, it does not compare the color space and the transfer type, so - * we have to compare them here. - * - * @param cm1 the first color model. - * @param cm2 the second color model. - * @return {@code true} if the two color models are equal. - */ - private static boolean equals(final ColorModel cm1, final ColorModel cm2) { - if (cm1 == cm2) { - return true; - } - if (cm1 != null && cm1.equals(cm2) && - cm1.getClass().equals(cm2.getClass()) && - cm1.getTransferType() == cm2.getTransferType() && - Objects.equals(cm1.getColorSpace(), cm2.getColorSpace())) - { - if (cm1 instanceof IndexColorModel) { - final IndexColorModel icm1 = (IndexColorModel) cm1; - final IndexColorModel icm2 = (IndexColorModel) cm2; - final int size = icm1.getMapSize(); - if (icm2.getMapSize() == size && - icm1.getTransparentPixel() == icm2.getTransparentPixel() && - Objects.equals(icm1.getValidPixels(), icm2.getValidPixels())) - { - for (int i=0; i<size; i++) { - if (icm1.getRGB(i) != icm2.getRGB(i)) { - return false; - } - } - } - if (cm1 instanceof MultiBandsIndexColorModel) { - final MultiBandsIndexColorModel micm1 = (MultiBandsIndexColorModel) cm1; - final MultiBandsIndexColorModel micm2 = (MultiBandsIndexColorModel) cm2; - if (micm1.numBands != micm2.numBands || micm1.visibleBand != micm2.visibleBand) { - return false; - } - } - } - return true; - } - return false; - } - - /** - * For internal use only. - * - * @param object object The object to compare to. - * @return {@code true} if both object are equal. - */ - @Override - public boolean equals(final Object object) { - return (object instanceof ColorModelPatch<?>) && equals(cm, ((ColorModelPatch<?>) object).cm); - } - - /** - * For internal use only. - */ - @Override - public int hashCode() { - int code = cm.hashCode() ^ cm.getClass().hashCode(); - if (cm instanceof IndexColorModel) { - final IndexColorModel icm = (IndexColorModel) cm; - final int[] ARGB = new int[icm.getMapSize()]; - icm.getRGBs(ARGB); - code ^= Arrays.hashCode(ARGB); - } - return code; - } -} diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/MultiBandsIndexColorModel.java b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/MultiBandsIndexColorModel.java index 43eb231687..ed7531bab8 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/MultiBandsIndexColorModel.java +++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/MultiBandsIndexColorModel.java @@ -40,7 +40,7 @@ import java.awt.image.SinglePixelPackedSampleModel; * * @author Martin Desruisseaux (IRD, Geomatys) * @author Andrea Aime (TOPP) - * @version 1.1 + * @version 1.4 * @since 1.0 * @module */ @@ -95,12 +95,11 @@ final class MultiBandsIndexColorModel extends IndexColorModel { * There is no way to share the {@code int[]} array of ARGB values between two {@link IndexColorModel}s.</p> */ final IndexColorModel createSubsetColorModel(final int[] bands) { - final int bits = getPixelSize(); final int[] cmap = getARGB(); final boolean hasAlpha = hasAlpha(); final int transparent = getTransparentPixel(); if (bands.length == 1) { - return new IndexColorModel(bits, cmap.length, cmap, 0, hasAlpha, transparent, transferType); + return new IndexColorModel(pixel_bits, cmap.length, cmap, 0, hasAlpha, transparent, transferType); } int vb = 0; for (int i=0; i<bands.length; i++) { @@ -109,7 +108,7 @@ final class MultiBandsIndexColorModel extends IndexColorModel { break; } } - return new MultiBandsIndexColorModel(bits, cmap.length, cmap, 0, hasAlpha, transparent, transferType, bands.length, vb); + return new MultiBandsIndexColorModel(pixel_bits, cmap.length, cmap, 0, hasAlpha, transparent, transferType, bands.length, vb); } /** @@ -181,6 +180,17 @@ final class MultiBandsIndexColorModel extends IndexColorModel { } } + /** + * Returns the number of bits per pixel described by this color model. + * Must return a value different then {@code super.getPixelSize()} for avoiding that + * {@link IndexColorModel#equals(Object)} consider this color model equal to a standard + * {@code IndexColorModel} with the same color palette. + */ + @Override + public int getPixelSize() { + return pixel_bits * numBands; + } + /** * Returns an array of unnormalized color/alpha components for a specified pixel in this color model. * This method is the converse of {@link #getDataElements(int, Object)}. @@ -278,4 +288,29 @@ final class MultiBandsIndexColorModel extends IndexColorModel { sm.getNumBands() == numBands && (1 << sm.getSampleSize(visibleBand)) >= getMapSize(); } + + /** + * Compares this color model with the given object for equality. + * + * @param obj the other object to compare with this color model. + * @return whether both object are equal. + */ + @Override + public boolean equals(final Object obj) { + if (obj instanceof MultiBandsIndexColorModel) { + final MultiBandsIndexColorModel other = (MultiBandsIndexColorModel) obj; + return numBands == other.numBands && visibleBand == other.visibleBand && super.equals(other); + } + return false; + } + + /** + * Returns a hash code value for this color model. + * + * @return a hash code value. + */ + @Override + public int hashCode() { + return super.hashCode() + 31 * numBands + 37 * visibleBand; + } } diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/package-info.java b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/package-info.java index b3a3a342e2..ec09f80080 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/package-info.java +++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/package-info.java @@ -27,7 +27,7 @@ * may change in incompatible ways in any future version without notice. * * @author Johann Sorel (Geomatys) - * @version 1.3 + * @version 1.4 * @since 1.0 * @module */