toni@1144: /** toni@1302: * Back 2 Browser Bytecode Translator toni@1302: * Copyright (C) 2012 Jaroslav Tulach toni@1144: * toni@1302: * This program is free software: you can redistribute it and/or modify toni@1302: * it under the terms of the GNU General Public License as published by toni@1302: * the Free Software Foundation, version 2 of the License. toni@1144: * toni@1302: * This program is distributed in the hope that it will be useful, toni@1302: * but WITHOUT ANY WARRANTY; without even the implied warranty of toni@1302: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the toni@1302: * GNU General Public License for more details. toni@1144: * toni@1302: * You should have received a copy of the GNU General Public License toni@1302: * along with this program. Look for COPYING file in the top folder. toni@1302: * If not, see http://opensource.org/licenses/GPL-2.0. toni@1144: */ toni@1144: package net.java.html.canvas; toni@1144: toni@1144: import java.util.Objects; toni@1263: import java.util.ServiceLoader; toni@1263: import net.java.html.canvas.spi.GraphicsEnvironment; toni@1144: toni@1144: /** toni@1154: * Image represents an Image Resource defined by a path. toni@1144: * toni@1144: * @author antonepple toni@1144: */ toni@1304: public final class Image { toni@1144: toni@1144: private String src; toni@1308: private int cacheHash; toni@1144: private Object cached; toni@1144: toni@1263: public static Image create(String src) { toni@1263: return new Image(src); toni@1263: } toni@1263: toni@1263: private Image(String src) { toni@1144: this.src = src; toni@1144: } toni@1144: toni@1144: void cache(Object toCache) { toni@1263: this.cached = toCache; toni@1144: cacheHash = hashCode(); toni@1144: } toni@1144: toni@1144: private boolean isCached() { toni@1144: return cacheHash == hashCode(); toni@1144: } toni@1144: toni@1144: Object getCached() { toni@1144: return isCached() ? cached : null; toni@1144: } toni@1144: toni@1144: public String getSrc() { toni@1144: return src; toni@1144: } toni@1144: toni@1263: public int getWidth() { toni@1263: ServiceLoader loader = ServiceLoader.load(GraphicsEnvironment.class); toni@1263: GraphicsEnvironment ge = null; toni@1263: for (GraphicsEnvironment graphicsEnvironment : loader) { toni@1263: ge = graphicsEnvironment; toni@1263: break; toni@1263: } toni@1263: return ge.getWidth(this, cached); toni@1263: } toni@1263: toni@1304: toni@1263: public int getHeight() { toni@1263: ServiceLoader loader = ServiceLoader.load(GraphicsEnvironment.class); toni@1263: GraphicsEnvironment ge = null; toni@1263: for (GraphicsEnvironment graphicsEnvironment : loader) { toni@1263: ge = graphicsEnvironment; toni@1263: break; toni@1263: } toni@1263: return ge.getHeight(this, cached); toni@1263: } toni@1263: toni@1144: @Override toni@1144: public int hashCode() { toni@1144: int hash = 7; toni@1263: hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237); toni@1144: return hash; toni@1144: } toni@1144: toni@1144: @Override toni@1144: public boolean equals(Object obj) { toni@1144: if (obj == null) { toni@1144: return false; toni@1144: } toni@1144: if (getClass() != obj.getClass()) { toni@1144: return false; toni@1144: } toni@1144: final Image other = (Image) obj; toni@1144: if (!Objects.equals(this.src, other.src)) { toni@1144: return false; toni@1144: } toni@1263: if ((cached == null) != (other.getCached() == null)) { toni@1153: return false; toni@1153: } toni@1144: return true; toni@1144: } toni@1144: }