javaquery/canvas/src/main/java/net/java/html/canvas/Image.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Feb 2014 10:48:24 +0100
branchcanvas
changeset 1438 03cd1b3e2e70
parent 1308 e8429fba8cce
child 1445 493cae4fd458
permissions -rw-r--r--
Adding factory method javadoc
     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 int cacheHash;
    33     private Object cached;
    34 
    35     /** Creates a new image referencing a specified URL.
    36      * @param src the (relative) URL to the image
    37      * @return an object representing the image
    38      */
    39     public static Image create(String src) {
    40         return new Image(src);
    41     }
    42 
    43     private Image(String src) {
    44         this.src = src;
    45     }
    46 
    47     void cache(Object toCache) {
    48         this.cached = toCache;
    49         cacheHash = hashCode();
    50     }
    51 
    52     private boolean isCached() {
    53         return cacheHash == hashCode();
    54     }
    55 
    56     Object getCached() {
    57         return isCached() ? cached : null;
    58     }
    59 
    60     public String getSrc() {
    61         return src;
    62     }
    63 
    64     public int getWidth() {
    65         ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    66         GraphicsEnvironment ge = null;
    67         for (GraphicsEnvironment graphicsEnvironment : loader) {
    68             ge = graphicsEnvironment;
    69             break;
    70         }
    71         return ge.getWidth(this, cached);
    72     }
    73 
    74     
    75     public int getHeight() {
    76         ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    77         GraphicsEnvironment ge = null;
    78         for (GraphicsEnvironment graphicsEnvironment : loader) {
    79             ge = graphicsEnvironment;
    80             break;
    81         }
    82         return ge.getHeight(this, cached);
    83     }
    84 
    85     @Override
    86     public int hashCode() {
    87         int hash = 7;
    88         hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
    89         return hash;
    90     }
    91 
    92     @Override
    93     public boolean equals(Object obj) {
    94         if (obj == null) {
    95             return false;
    96         }
    97         if (getClass() != obj.getClass()) {
    98             return false;
    99         }
   100         final Image other = (Image) obj;
   101         if (!Objects.equals(this.src, other.src)) {
   102             return false;
   103         }
   104         if ((cached == null) != (other.getCached() == null)) {
   105             return false;
   106         }
   107         return true;
   108     }
   109 }