javaquery/canvas/src/main/java/net/java/html/canvas/Image.java
author Anton Epple <toni.epple@eppleton.de>
Wed, 12 Feb 2014 08:43:07 +0100
branchcanvas
changeset 1445 493cae4fd458
parent 1438 03cd1b3e2e70
permissions -rw-r--r--
More JavaDoc improvements.
     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     /**
    65      * get the width of this Image. Might be an expensive operation, depending on the 
    66      * GraphicsEnvironment, it might need to be rendered.
    67      * @return the width of this Image.
    68      */
    69     public int getWidth() {
    70         ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    71         GraphicsEnvironment ge = null;
    72         for (GraphicsEnvironment graphicsEnvironment : loader) {
    73             ge = graphicsEnvironment;
    74             break;
    75         }
    76         return ge.getWidth(this, cached);
    77     }
    78 
    79     /**
    80      * get the height of this Image. Might be an expensive operation, depending on the 
    81      * GraphicsEnvironment, it might need to be rendered.
    82      * @return the height of this Image.
    83      */
    84     public int getHeight() {
    85         ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    86         GraphicsEnvironment ge = null;
    87         for (GraphicsEnvironment graphicsEnvironment : loader) {
    88             ge = graphicsEnvironment;
    89             break;
    90         }
    91         return ge.getHeight(this, cached);
    92     }
    93 
    94     @Override
    95     public int hashCode() {
    96         int hash = 7;
    97         hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
    98         return hash;
    99     }
   100 
   101     @Override
   102     public boolean equals(Object obj) {
   103         if (obj == null) {
   104             return false;
   105         }
   106         if (getClass() != obj.getClass()) {
   107             return false;
   108         }
   109         final Image other = (Image) obj;
   110         if (!Objects.equals(this.src, other.src)) {
   111             return false;
   112         }
   113         if ((cached == null) != (other.getCached() == null)) {
   114             return false;
   115         }
   116         return true;
   117     }
   118 }