javaquery/canvas/src/main/java/net/java/html/canvas/Image.java
author Anton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 10:39:09 +0200
branchcanvas
changeset 1153 edda08aba469
parent 1144 5bf850c5b7f1
child 1154 2bdd1eba1880
permissions -rw-r--r--
added cached object to image hashCode/equals
     1 /**
     2  * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     3  * <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify it under
     6  * the terms of the GNU General Public License as published by the Free Software
     7  * Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12  * details.
    13  *
    14  * You should have received a copy of the GNU General Public License along with
    15  * this program. Look for COPYING file in the top folder. If not, see
    16  * http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package net.java.html.canvas;
    19 
    20 import java.util.Objects;
    21 
    22 /**
    23  * Image represents an Image defined by a Path.
    24  *
    25  *
    26  * @author antonepple
    27  */
    28 public class Image {
    29 
    30     private String src;
    31     private Object cached;
    32     private int cacheHash;
    33 
    34     void Image(String src) {
    35         this.src = src;
    36     }
    37 
    38     void cache(Object toCache) {
    39         cacheHash = hashCode();
    40         this.cached = toCache;
    41     }
    42 
    43     private boolean isCached() {
    44         return cacheHash == hashCode();
    45     }
    46 
    47     Object getCached() {
    48         return isCached() ? cached : null;
    49     }
    50 
    51     public String getSrc() {
    52         return src;
    53     }
    54 
    55     @Override
    56     public int hashCode() {
    57         int hash = 7;
    58         hash = 59 * hash + Objects.hashCode(this.src) ^ (cached==null? 1231 : 1237);
    59         return hash;
    60     }
    61 
    62     @Override
    63     public boolean equals(Object obj) {
    64         if (obj == null) {
    65             return false;
    66         }
    67         if (getClass() != obj.getClass()) {
    68             return false;
    69         }
    70         final Image other = (Image) obj;
    71         if (!Objects.equals(this.src, other.src)) {
    72             return false;
    73         }
    74         if ((cached==null) != (other.getCached()==null)){
    75             return false;
    76         }
    77         return true;
    78     }
    79  
    80 }