public class Images { private static final Image[] images = new Image[ImageType.values().length]; public static void loadImageResources() throws IOException { final ClassLoader cl = Images.class.getClassLoader(); for (ImageType imageType : ImageType.values()) { images[imageType.ordinal()] = readFromImageDir(imageType.getFileName()); } } public static Image get(ImageType imageType) { assertNotNull(imageType, "iamgeType"); final Image image = images[imageType.ordinal()]; assertNotNull(image, imageType.toString()); return image; } private static void assertNotNull(Object o, String msg) { if (o == null) { throw new AssertionError("object was null: " + msg); } } private static Image readFromImageDir(String fileName) { final String relativePath = "image/" + fileName; try { InputStream imageStream = ResourceFinder.getResourceStream(relativePath); final Image img = ImageIO.read(imageStream); return img; } catch (Exception e) { throw new IllegalStateException("couldn't find image: " + relativePath); } } }
private Map<Album, Map<ImageType, Resource>> collectAlbumTargets( Library library, final ResourceLocator resourceLocator, Collection<Album> changedAlbums) { Map<Album, Map<ImageType, Resource>> result = new HashMap<>(); for (Album album : library.getAlbums()) { Resource artworkAssetResource = album.artworkAssetResource(); if (artworkAssetResource != null) { Map<ImageType, Resource> targets = new HashMap<ImageType, Resource>(); for (ImageType type : ImageType.values()) { String imagePath = resourceLocator.getAlbumImagePath(album, type); if (imagePath != null) { Resource resource = resourceLocator.getResource(imagePath); try { if (changedAlbums.contains(album) || !resource.exists()) { resource.getParent().mkdirs(); targets.put(type, resource); } } catch (IOException e) { LOGGER.warning("Could not write image file: " + resource.getPath().toAbsolutePath()); } } } if (!targets.isEmpty()) { result.put(album, targets); } } } return result; }
public static ImageType getTypeFromString(String val) { for (ImageType t : ImageType.values()) { if (t.getTypeId().equals(val)) { return t; } } return null; }
@XmlType @XmlEnum(String.class) public static enum ImageType { @XmlEnumValue("iso") ISO("iso"), @XmlEnumValue("floppy") FLOPPY("floppy"), UNRECOGNIZED("unrecognized"); public static final List<ImageType> ALL = ImmutableList.of(ISO, FLOPPY); protected final String stringValue; ImageType(String stringValue) { this.stringValue = stringValue; } public String value() { return stringValue; } protected static final Map<String, ImageType> STATUS_BY_ID = Maps.uniqueIndex( ImmutableSet.copyOf(ImageType.values()), new Function<ImageType, String>() { @Override public String apply(ImageType input) { return input.stringValue; } }); public static ImageType fromValue(String value) { ImageType type = STATUS_BY_ID.get(checkNotNull(value, "stringValue")); return type == null ? UNRECOGNIZED : type; } }
public static void loadImageResources() throws IOException { final ClassLoader cl = Images.class.getClassLoader(); for (ImageType imageType : ImageType.values()) { images[imageType.ordinal()] = readFromImageDir(imageType.getFileName()); } }