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