
net.vectorpublish.desktop.vp.ui.ImageKey Maven / Gradle / Ivy
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.ui;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* The key for an image.
*
* The imagekey is detached from the plug-in. That means that multiple plug-ins
* can have the same imagekey without the same image. This looks like a
* misdesign because you may think that if you change the icon of a imagekey you
* change the icon of other plug-ins too, this is not correct. The
* {@link ImageKey} only shall give a hint that two plug-ins who have different
* icons may share the same icon for a simmilar action.
*/
public class ImageKey {
private final static Set KEYS = new LinkedHashSet<>();
public static final String REGEX_PATTERN = "[a-z0-9\\._]+";
/**
* Get a image key.
*
* @param key
* A key, only a-z, 9-0, . and _ is allowed.
* @return The key.
*/
public static ImageKey get(String key) {
for (final ImageKey imageKey : KEYS) {
if (imageKey.key.equals(key)) {
return imageKey;
}
}
synchronized (KEYS) {
for (final ImageKey imageKey : KEYS) {
if (imageKey.key.equals(key)) {
return imageKey;
}
}
return new ImageKey(key);
}
}
private final String key;
private ImageKey(String key) {
this.key = key;
if (!key.matches("^" + REGEX_PATTERN + "$")) {
throw new IllegalArgumentException("The Key ('" + key
+ "') must be any lowcase alphanumeric value without spaces but dot and underscore is allowed.");
}
KEYS.add(this);
}
public final String getCanonical() {
return key;
}
/**
* Please use ==
instead.
*
* @see Object#equals(Object)
*/
@Override
@Deprecated
public boolean equals(Object obj) {
return super.equals(obj);
}
}