Debian Codegraph @

src/main/java/org/jfree/pdf/util/GradientPaintKey.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf.util;
33
34 import java.awt.GradientPaint;
35
36 /**
37 * A wrapper for a {@code GradientPaint} that can be used as the key for
38 * a {@code HashMap}. This class is used internally by
39 * {@code PDFGraphics2D} to track and re-use gradient definitions.
40 * {@code GradientPaint} itself does not implement the {@code equals()} and
41 * {@code hashCode()} methods, so it doesn't make a good key for a {@code Map}.
42 */
43 public final class GradientPaintKey {
44
45 private GradientPaint paint;
46
47 /**
48 * Creates a new instance based on the specified {@code paint}.
49 *
50 * @param paint the paint ({@code null} not permitted).
51 */
52 public GradientPaintKey(GradientPaint paint) {
53 Args.nullNotPermitted(paint, "paint");
54 this.paint = paint;
55 }
56
57 /**
58 * Returns the {@code GradientPaint} that was supplied to the
59 * constructor.
60 *
61 * @return The {@code GradientPaint} (never {@code null}).
62 */
63 public GradientPaint getPaint() {
64 return this.paint;
65 }
66
67 /**
68 * Checks this instance for equality with an arbitrary object.
69 *
70 * @param obj the object to test against ({@code null} permitted).
71 *
72 * @return A boolean.
73 */
74 @Override
75 public boolean equals(Object obj) {
76 if (obj == this) {
77 return true;
78 }
79 if (!(obj instanceof GradientPaintKey)) {
80 return false;
81 }
82 GradientPaintKey that = (GradientPaintKey) obj;
83 GradientPaint thisGP = this.paint;
84 GradientPaint thatGP = that.getPaint();
85 if (!thisGP.getColor1().equals(thatGP.getColor1())) {
86 return false;
87 }
88 if (!thisGP.getColor2().equals(thatGP.getColor2())) {
89 return false;
90 }
91 if (!thisGP.getPoint1().equals(thatGP.getPoint1())) {
92 return false;
93 }
94 if (!thisGP.getPoint2().equals(thatGP.getPoint2())) {
95 return false;
96 }
97 if (thisGP.getTransparency() != thatGP.getTransparency()) {
98 return false;
99 }
100 if (thisGP.isCyclic() != thatGP.isCyclic()) {
101 return false;
102 }
103 return true;
104 }
105
106 /**
107 * Returns a hash code for this instance.
108 *
109 * @return A hash code.
110 */
111 @Override
112 public int hashCode() {
113 int hash = 5;
114 hash = 47 * hash + this.paint.getPoint1().hashCode();
115 hash = 47 * hash + this.paint.getPoint2().hashCode();
116 hash = 47 * hash + this.paint.getColor1().hashCode();
117 hash = 47 * hash + this.paint.getColor2().hashCode();
118 return hash;
119 }
120
121 }