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