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