author | Anton Epple <toni.epple@eppleton.de> |
Thu, 26 Sep 2013 16:33:04 -0700 | |
branch | canvas |
changeset 1308 | e8429fba8cce |
parent 1304 | 06c04db2e084 |
child 1438 | 03cd1b3e2e70 |
permissions | -rw-r--r-- |
toni@1144 | 1 |
/** |
toni@1302 | 2 |
* Back 2 Browser Bytecode Translator |
toni@1302 | 3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
toni@1144 | 4 |
* |
toni@1302 | 5 |
* This program is free software: you can redistribute it and/or modify |
toni@1302 | 6 |
* it under the terms of the GNU General Public License as published by |
toni@1302 | 7 |
* the Free Software Foundation, version 2 of the License. |
toni@1144 | 8 |
* |
toni@1302 | 9 |
* This program is distributed in the hope that it will be useful, |
toni@1302 | 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
toni@1302 | 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
toni@1302 | 12 |
* GNU General Public License for more details. |
toni@1144 | 13 |
* |
toni@1302 | 14 |
* You should have received a copy of the GNU General Public License |
toni@1302 | 15 |
* along with this program. Look for COPYING file in the top folder. |
toni@1302 | 16 |
* If not, see http://opensource.org/licenses/GPL-2.0. |
toni@1144 | 17 |
*/ |
toni@1144 | 18 |
package net.java.html.canvas; |
toni@1144 | 19 |
|
toni@1144 | 20 |
import java.util.Objects; |
toni@1263 | 21 |
import java.util.ServiceLoader; |
toni@1263 | 22 |
import net.java.html.canvas.spi.GraphicsEnvironment; |
toni@1144 | 23 |
|
toni@1144 | 24 |
/** |
toni@1154 | 25 |
* Image represents an Image Resource defined by a path. |
toni@1144 | 26 |
* |
toni@1144 | 27 |
* @author antonepple |
toni@1144 | 28 |
*/ |
toni@1304 | 29 |
public final class Image { |
toni@1144 | 30 |
|
toni@1144 | 31 |
private String src; |
toni@1308 | 32 |
private int cacheHash; |
toni@1144 | 33 |
private Object cached; |
toni@1144 | 34 |
|
toni@1263 | 35 |
public static Image create(String src) { |
toni@1263 | 36 |
return new Image(src); |
toni@1263 | 37 |
} |
toni@1263 | 38 |
|
toni@1263 | 39 |
private Image(String src) { |
toni@1144 | 40 |
this.src = src; |
toni@1144 | 41 |
} |
toni@1144 | 42 |
|
toni@1144 | 43 |
void cache(Object toCache) { |
toni@1263 | 44 |
this.cached = toCache; |
toni@1144 | 45 |
cacheHash = hashCode(); |
toni@1144 | 46 |
} |
toni@1144 | 47 |
|
toni@1144 | 48 |
private boolean isCached() { |
toni@1144 | 49 |
return cacheHash == hashCode(); |
toni@1144 | 50 |
} |
toni@1144 | 51 |
|
toni@1144 | 52 |
Object getCached() { |
toni@1144 | 53 |
return isCached() ? cached : null; |
toni@1144 | 54 |
} |
toni@1144 | 55 |
|
toni@1144 | 56 |
public String getSrc() { |
toni@1144 | 57 |
return src; |
toni@1144 | 58 |
} |
toni@1144 | 59 |
|
toni@1263 | 60 |
public int getWidth() { |
toni@1263 | 61 |
ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class); |
toni@1263 | 62 |
GraphicsEnvironment ge = null; |
toni@1263 | 63 |
for (GraphicsEnvironment graphicsEnvironment : loader) { |
toni@1263 | 64 |
ge = graphicsEnvironment; |
toni@1263 | 65 |
break; |
toni@1263 | 66 |
} |
toni@1263 | 67 |
return ge.getWidth(this, cached); |
toni@1263 | 68 |
} |
toni@1263 | 69 |
|
toni@1304 | 70 |
|
toni@1263 | 71 |
public int getHeight() { |
toni@1263 | 72 |
ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class); |
toni@1263 | 73 |
GraphicsEnvironment ge = null; |
toni@1263 | 74 |
for (GraphicsEnvironment graphicsEnvironment : loader) { |
toni@1263 | 75 |
ge = graphicsEnvironment; |
toni@1263 | 76 |
break; |
toni@1263 | 77 |
} |
toni@1263 | 78 |
return ge.getHeight(this, cached); |
toni@1263 | 79 |
} |
toni@1263 | 80 |
|
toni@1144 | 81 |
@Override |
toni@1144 | 82 |
public int hashCode() { |
toni@1144 | 83 |
int hash = 7; |
toni@1263 | 84 |
hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237); |
toni@1144 | 85 |
return hash; |
toni@1144 | 86 |
} |
toni@1144 | 87 |
|
toni@1144 | 88 |
@Override |
toni@1144 | 89 |
public boolean equals(Object obj) { |
toni@1144 | 90 |
if (obj == null) { |
toni@1144 | 91 |
return false; |
toni@1144 | 92 |
} |
toni@1144 | 93 |
if (getClass() != obj.getClass()) { |
toni@1144 | 94 |
return false; |
toni@1144 | 95 |
} |
toni@1144 | 96 |
final Image other = (Image) obj; |
toni@1144 | 97 |
if (!Objects.equals(this.src, other.src)) { |
toni@1144 | 98 |
return false; |
toni@1144 | 99 |
} |
toni@1263 | 100 |
if ((cached == null) != (other.getCached() == null)) { |
toni@1153 | 101 |
return false; |
toni@1153 | 102 |
} |
toni@1144 | 103 |
return true; |
toni@1144 | 104 |
} |
toni@1144 | 105 |
} |