summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/consts/Ident.java
diff options
context:
space:
mode:
authorJ08nY2018-01-23 17:31:15 +0100
committerJ08nY2018-01-23 17:31:15 +0100
commitcb6c6b8b1274fe5a340c4317a4b015ea0ef15396 (patch)
tree864a54dcdf07da33cd139312c8b0ee693e1a0eff /src/cz/crcs/ectester/standalone/consts/Ident.java
parent6c46a27a52854aee24f7a37e74002bd6f4485723 (diff)
parentc581e39e539e6dadb49d9f83f563ab2b375f6e0b (diff)
downloadECTester-0.2.0.tar.gz
ECTester-0.2.0.tar.zst
ECTester-0.2.0.zip
Diffstat (limited to 'src/cz/crcs/ectester/standalone/consts/Ident.java')
-rw-r--r--src/cz/crcs/ectester/standalone/consts/Ident.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/standalone/consts/Ident.java b/src/cz/crcs/ectester/standalone/consts/Ident.java
new file mode 100644
index 0000000..40a44ac
--- /dev/null
+++ b/src/cz/crcs/ectester/standalone/consts/Ident.java
@@ -0,0 +1,80 @@
+package cz.crcs.ectester.standalone.consts;
+
+import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.function.BiFunction;
+
+public abstract class Ident {
+ Set<String> idents;
+ String name;
+
+ public Ident(String name, String... aliases) {
+ this.name = name;
+ this.idents = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ this.idents.add(name);
+ this.idents.addAll(Arrays.asList(aliases));
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Set<String> getIdents() {
+ return Collections.unmodifiableSet(idents);
+ }
+
+ public boolean contains(String other) {
+ return name.equals(other) || idents.contains(other);
+ }
+
+ <T> T getInstance(BiFunction<String, Provider, T> getter, Provider provider) throws NoSuchAlgorithmException {
+ T instance = null;
+ try {
+ instance = getter.apply(name, provider);
+ } catch (Exception ignored) {
+ }
+
+ if (instance == null) {
+ for (String alias : idents) {
+ try {
+ instance = getter.apply(alias, provider);
+ if (instance != null) {
+ break;
+ }
+ } catch (Exception ignored) {
+ }
+ }
+ }
+
+ if (instance == null) {
+ throw new NoSuchAlgorithmException(name);
+ }
+ return instance;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof Ident)) {
+ return false;
+ }
+ Ident other = (Ident) obj;
+ return idents.equals(other.getIdents());
+ }
+
+ @Override
+ public int hashCode() {
+ return idents.hashCode() + 37;
+ }
+
+ @Override
+ public String toString() {
+ return "(" + String.join("|", idents) + ")";
+ }
+}