From d42be6246bbc1a2be6fcf7fed51dee85aee8cae0 Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Sat, 1 Mar 2025 14:39:50 -0800 Subject: [PATCH] Uptakes microbean-attributes 0.0.2. Refactors Qualifiers. Signed-off-by: Laird Nelson --- README.md | 2 +- pom.xml | 37 +-- src/main/java/module-info.java | 2 +- .../java/org/microbean/assign/Qualifiers.java | 289 ++++++++++-------- 4 files changed, 182 insertions(+), 148 deletions(-) diff --git a/README.md b/README.md index 444c6b4..25b78ea 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ dependency: org.microbean microbean-assign - 0.0.4 + 0.0.5 ``` diff --git a/pom.xml b/pom.xml index 524cd1e..47df787 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ ${project.organization.name}. All rights reserved.]]> <a href="${project.url}" target="_top"><span style="font-family:Lobster, cursive;">µb</span> ${project.artifactId}</a> ${project.version} - https://microbean.github.io/microbean-construct/apidocs/,https://microbean.github.io/microbean-qualifier/apidocs/ + https://microbean.github.io/microbean-attributes/apidocs/,https://microbean.github.io/microbean-construct/apidocs/ 2 @@ -134,14 +134,14 @@ org.microbean - microbean-construct - 0.0.9 + microbean-attributes + 0.0.2 org.microbean - microbean-qualifier - 0.2.4 + microbean-construct + 0.0.10 @@ -151,13 +151,13 @@ org.microbean - microbean-construct + microbean-attributes compile - + org.microbean - microbean-qualifier + microbean-construct compile @@ -308,7 +308,7 @@ maven-clean-plugin - 3.4.0 + 3.4.1 @@ -323,20 +323,13 @@ maven-compiler-plugin - 3.13.0 + 3.14.0 -Xlint:all -parameters - - - org.codehaus.plexus - plexus-java - 1.3.0 - - maven-dependency-plugin @@ -344,7 +337,7 @@ maven-deploy-plugin - 3.1.3 + 3.1.4 maven-enforcer-plugin @@ -357,7 +350,7 @@ maven-install-plugin - 3.1.3 + 3.1.4 maven-jar-plugin @@ -393,7 +386,7 @@ maven-project-info-reports-plugin - 3.8.0 + 3.9.0 maven-release-plugin @@ -446,7 +439,7 @@ com.github.spotbugs spotbugs-maven-plugin - 4.8.6.6 + 4.9.1.0 org.codehaus.mojo @@ -456,7 +449,7 @@ io.smallrye jandex-maven-plugin - 3.2.3 + 3.2.7 org.sonatype.plugins diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index f7f5b09..c419619 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -22,7 +22,7 @@ exports org.microbean.assign; requires transitive java.compiler; + requires transitive org.microbean.attributes; requires transitive org.microbean.construct; - requires transitive org.microbean.qualifier; } diff --git a/src/main/java/org/microbean/assign/Qualifiers.java b/src/main/java/org/microbean/assign/Qualifiers.java index 007ba7b..e827ef0 100644 --- a/src/main/java/org/microbean/assign/Qualifiers.java +++ b/src/main/java/org/microbean/assign/Qualifiers.java @@ -3,12 +3,12 @@ * Copyright © 2025 microBean™. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ package org.microbean.assign; @@ -18,86 +18,93 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Map.Entry; -import org.microbean.qualifier.NamedAttributeMap; +import org.microbean.attributes.Attributes; + +import static java.util.HashMap.newHashMap; /** * A utility class for working with commonly-used qualifiers. * * @author Laird Nelson * - * @see NamedAttributeMap + * @see Attributes */ public final class Qualifiers { - private static final NamedAttributeMap QUALIFIER = new NamedAttributeMap<>("Qualifier"); + private static final Attributes QUALIFIER = Attributes.of("Qualifier"); + + private static final List QUALIFIERS = List.of(QUALIFIER); + + private static final Attributes ANY_QUALIFIER = Attributes.of("Any", QUALIFIERS); - private static final List> QUALIFIER_LIST = List.of(QUALIFIER); + private static final List ANY_QUALIFIERS = List.of(ANY_QUALIFIER); - private static final NamedAttributeMap ANY_QUALIFIER = new NamedAttributeMap<>("Any", Map.of(), Map.of(), QUALIFIER_LIST); + private static final Attributes DEFAULT_QUALIFIER = Attributes.of("Default", QUALIFIERS); - private static final List> ANY_QUALIFIERS = List.of(ANY_QUALIFIER); + private static final List DEFAULT_QUALIFIERS = List.of(DEFAULT_QUALIFIER); - private static final NamedAttributeMap DEFAULT_QUALIFIER = new NamedAttributeMap<>("Default", Map.of(), Map.of(), QUALIFIER_LIST); + private static final List ANY_AND_DEFAULT_QUALIFIERS = List.of(ANY_QUALIFIER, DEFAULT_QUALIFIER); - private static final List> DEFAULT_QUALIFIERS = List.of(DEFAULT_QUALIFIER); + private static final Attributes PRIMORDIAL_QUALIFIER = Attributes.of("Primordial", QUALIFIERS); - private static final List> ANY_AND_DEFAULT_QUALIFIERS = List.of(ANY_QUALIFIER, DEFAULT_QUALIFIER); + private static final List PRIMORDIAL_QUALIFIERS = List.of(PRIMORDIAL_QUALIFIER); private Qualifiers() { super(); } /** - * Returns the any qualifier. + * Returns an unmodifiable {@link List} consisting solely of the unattributed any qualifier and the + * default qualifier. * - * @return the any qualifier; never {@code null} + * @return an unmodifiable {@link List} consisting solely of the unattributed any qualifier and the default qualifier; + * never {@code null} * - * @see #anyQualifiers() + * @see #anyQualifier() + * + * @see #defaultQualifier() */ - public static final NamedAttributeMap anyQualifier() { - return ANY_QUALIFIER; + public static final List anyAndDefaultQualifiers() { + return ANY_AND_DEFAULT_QUALIFIERS; } /** - * Returns {@code true} if and only if the supplied {@link NamedAttributeMap} {@linkplain - * NamedAttributeMap#equals(Object) is equal to} the {@linkplain #anyQualifier() any qualifier}. - * - * @param nam a {@link NamedAttributeMap}; must not be {@code null} + * Returns the unattributed any qualifier. * - * @return {@code true} if and only if the supplied {@link NamedAttributeMap} {@linkplain - * NamedAttributeMap#equals(Object) is equal to} the {@linkplain #defaultQualifier() default qualifier} + * @return the any qualifier; never {@code null} * - * @exception NullPointerException if {@code nam} is {@code null} + * @see #anyQualifiers() */ - public static final boolean anyQualifier(final NamedAttributeMap nam) { - return ANY_QUALIFIER.equals(nam) && qualifier(nam); + public static final Attributes anyQualifier() { + return ANY_QUALIFIER; } /** - * Returns an immutable {@link List} consisting solely of the any qualifier. + * Returns {@code true} if and only if the supplied {@link Attributes} {@linkplain Attributes#equals(Object) is equal + * to} the unattributed {@linkplain #anyQualifier() any qualifier}. * - * @return an immutable {@link List}; never {@code null} + * @param a an {@link Attributes}; must not be {@code null} * - * @see #anyQualifier() + * @return {@code true} if and only if the supplied {@link Attributes} {@linkplain Attributes#equals(Object) is equal + * to} the unattributed {@linkplain #anyQualifier() any qualifier} + * + * @exception NullPointerException if {@code a} is {@code null} */ - public static final List> anyQualifiers() { - return ANY_QUALIFIERS; + public static final boolean anyQualifier(final Attributes a) { + return ANY_QUALIFIER == a || anyQualifier().equals(a) && qualifier(a); } /** - * Returns an unmodifiable {@link List} consisting solely of the any qualifier and the default - * qualifier. + * Returns an immutable {@link List} consisting solely of the unattributed any qualifier. * - * @return an unmodifiable {@link List} consisting solely of the any qualifier and the default qualifier; never {@code - * null} + * @return an immutable {@link List}; never {@code null} * * @see #anyQualifier() - * - * @see #defaultQualifier() */ - public static final List> anyAndDefaultQualifiers() { - return ANY_AND_DEFAULT_QUALIFIERS; + public static final List anyQualifiers() { + return ANY_QUALIFIERS; } /** @@ -107,23 +114,23 @@ public static final List> anyAndDefaultQualifiers() { * * @see #defaultQualifiers() */ - public static final NamedAttributeMap defaultQualifier() { + public static final Attributes defaultQualifier() { return DEFAULT_QUALIFIER; } /** - * Returns {@code true} if and only if the supplied {@link NamedAttributeMap} {@linkplain - * NamedAttributeMap#equals(Object) is equal to} the {@linkplain #defaultQualifier() default qualifier}. + * Returns {@code true} if and only if the supplied {@link Attributes} {@linkplain + * Attributes#equals(Object) is equal to} the {@linkplain #defaultQualifier() default qualifier}. * - * @param nam a {@link NamedAttributeMap}; must not be {@code null} + * @param a an {@link Attributes}; must not be {@code null} * - * @return {@code true} if and only if the supplied {@link NamedAttributeMap} {@linkplain - * NamedAttributeMap#equals(Object) is equal to} the {@linkplain #defaultQualifier() default qualifier} + * @return {@code true} if and only if the supplied {@link Attributes} {@linkplain + * Attributes#equals(Object) is equal to} the {@linkplain #defaultQualifier() default qualifier} * - * @exception NullPointerException if {@code nam} is {@code null} + * @exception NullPointerException if {@code a} is {@code null} */ - public static final boolean defaultQualifier(final NamedAttributeMap nam) { - return DEFAULT_QUALIFIER.equals(nam) && qualifier(nam); + public static final boolean defaultQualifier(final Attributes a) { + return DEFAULT_QUALIFIER == a || defaultQualifier().equals(a) && qualifier(a); } /** @@ -133,123 +140,157 @@ public static final boolean defaultQualifier(final NamedAttributeMap nam) { * * @see #defaultQualifier() */ - public static final List> defaultQualifiers() { + public static final List defaultQualifiers() { return DEFAULT_QUALIFIERS; } /** - * Returns the qualifier (meta-) qualifier. + * Returns an {@link Attributes} that is {@linkplain Attributes#equals(Object) equal to} the supplied {@link + * Attributes}. * - * @return the qualifier (meta-) qualifier; never {@code null} + *

The returned {@link Attributes} may be the supplied {@link Attributes} or a different instance.

+ * + * @param a an {@link Attributes}; must not be {@code null} + * + * @return an {@link Attributes} that is {@linkplain Attributes#equals(Object) equal to} the supplied {@link + * Attributes}; never {@code null} + * + * @exception NullPointerException if {@code a} is {@code null} */ - public static final NamedAttributeMap qualifier() { - return QUALIFIER; + public static final Attributes normalize(final Attributes a) { + return switch (a) { + case null -> throw new NullPointerException("a"); + case Attributes q when defaultQualifier(q) -> defaultQualifier(); + case Attributes q when QUALIFIER.equals(q) -> qualifier(); + default -> a; + }; } /** - * Returns {@code true} if and only if the supplied {@link NamedAttributeMap} is itself a {@link NamedAttributeMap} - * that can be used to designate other {@link NamedAttributeMap}s as qualifiers, or a {@link NamedAttributeMap} so - * designated. + * Returns an immutable {@link List} of {@link Attributes}s that is {@linkplain List#equals(Object) equal to} the + * supplied {@link List}. * - *

A {@link NamedAttributeMap} whose {@linkplain NamedAttributeMap#name() name} is {@code Qualifier} and whose - * {@linkplain NamedAttributeMap#metadata() metadata} is empty is an example of the former.

+ *

The returned {@link List} may be the supplied {@link List} or a different instance.

* - *

A {@link NamedAttributeMap} whose {@linkplain NamedAttributeMap#metadata() metadata} contains a {@link - * NamedAttributeMap} whose {@linkplain NamedAttributeMap#name() name} is {@code Qualifier} is an example of the - * latter.

+ * @param list a {@link List} of {@link Attributes}s; must not be {@code null} * - * @param q a {@link NamedAttributeMap}; must not be {@code null} + * @return an immutable {@link List} of {@link Attributes}s that is {@linkplain List#equals(Object) equal to} the + * supplied {@link List}; never {@code null} * - * @return {@code true} if and only if the supplied {@link NamedAttributeMap} is itself a {@link NamedAttributeMap} - * that can be used to designate other {@link NamedAttributeMap}s as qualifiers, or a {@link NamedAttributeMap} so - * designated + * @exception NullPointerException if {@code list} is {@code null} + */ + public static final List normalize(final List list) { + return switch (list.size()) { + case 0 -> List.of(); + case 1 -> list.equals(defaultQualifiers()) ? defaultQualifiers() : List.copyOf(list); + default -> { + final List l = new ArrayList<>(list.size()); + for (final Attributes a : list) { + l.add(normalize(a)); + } + yield Collections.unmodifiableList(l); + } + }; + } + + /** + * Returns the primordial qualifier. * - * @exception NullPointerException if {@code nam} is {@code null} + * @return the primordial qualifier; never {@code null} * - * @see NamedAttributeMap#metadata() + * @see #primordialQualifiers() */ - public static final boolean qualifier(final NamedAttributeMap q) { - return q != null && qualifier(q.metadata()); + public static final Attributes primordialQualifier() { + return PRIMORDIAL_QUALIFIER; } - private static final boolean qualifier(final Iterable> mds) { - for (final NamedAttributeMap md : mds) { - if (QUALIFIER.equals(md) && md.metadata().isEmpty() || qualifier(md)) { - return true; - } - } - return false; + /** + * Returns {@code true} if and only if the supplied {@link Attributes} {@linkplain + * Attributes#equals(Object) is equal to} the {@linkplain #primordialQualifier() primordial qualifier}. + * + * @param a an {@link Attributes}; must not be {@code null} + * + * @return {@code true} if and only if the supplied {@link Attributes} {@linkplain + * Attributes#equals(Object) is equal to} the {@linkplain #primordialQualifier() primordial qualifier} + * + * @exception NullPointerException if {@code a} is {@code null} + */ + public static final boolean primordialQualifier(final Attributes a) { + return PRIMORDIAL_QUALIFIER == a || primordialQualifier().equals(a) && qualifier(a); } /** - * Returns an unmodifiable {@link List} consisting only of those {@link NamedAttributeMap}s in the supplied {@link - * Collection} that {@linkplain #qualifier(NamedAttributeMap) are qualifiers}. - * - * @param c a {@link Collection} of {@link NamedAttributeMap}s; must not be {@code null} + * Returns an immutable {@link List} consisting solely of the primordial qualifier. * - * @return an unmodifiable {@link List} consisting only of those {@link NamedAttributeMap}s in the supplied {@link - * Collection} that {@linkplain #qualifier(NamedAttributeMap) are qualifiers}; never {@code null} + * @return an immutable {@link List}; never {@code null} * - * @exception NullPointerException if {@code c} is {@code null} + * @see #primordialQualifier() */ - public static final List> qualifiers(final Collection> c) { - if (c == null || c.isEmpty()) { - return List.of(); - } - final ArrayList> list = new ArrayList<>(c.size()); - for (final NamedAttributeMap a : c) { - if (qualifier(a)) { - list.add(normalize(a)); - } - } - list.trimToSize(); - return Collections.unmodifiableList(list); + public static final List primordialQualifiers() { + return PRIMORDIAL_QUALIFIERS; } /** - * Returns a {@link NamedAttributeMap} that is {@linkplain NamedAttributeMap#equals(Object) equal to} the supplied - * {@link NamedAttributeMap}. + * Returns the qualifier (meta-) qualifier. * - *

The returned {@link NamedAttributeMap} may be the supplied {@link NamedAttributeMap} or a different - * instance.

+ * @return the qualifier (meta-) qualifier; never {@code null} + */ + public static final Attributes qualifier() { + return QUALIFIER; + } + + /** + * Returns {@code true} if and only if the supplied {@link Attributes} is an {@link Attributes} that can be used to + * designate other {@link Attributes} as qualifiers. * - * @param nam a {@link NamedAttributeMap}; must not be {@code null} + * @param q an {@link Attributes}; must not be {@code null} * - * @return a {@link NamedAttributeMap} that is {@linkplain NamedAttributeMap#equals(Object) equal to} the supplied - * {@link NamedAttributeMap}; never {@code null} + * @return {@code true} if and only if the supplied {@link Attributes} is an {@link Attributes} that can be used to + * designate other {@link Attributes} as qualifiers * - * @exception NullPointerException if {@code nam} is {@code null} + * @exception NullPointerException if {@code q} is {@code null} */ - public static final NamedAttributeMap normalize(final NamedAttributeMap nam) { - return switch (nam) { - case null -> throw new NullPointerException("nam"); - case NamedAttributeMap q when anyQualifier(q) -> anyQualifier(); - case NamedAttributeMap q when defaultQualifier(q) -> defaultQualifier(); - case NamedAttributeMap q when QUALIFIER.equals(q) && q.metadata().isEmpty() -> qualifier(); - default -> nam; - }; + public static final boolean qualifier(final Attributes q) { + return q.attributes().contains(qualifier()); } /** - * Returns an immutable {@link List} of {@link NamedAttributeMap}s that is {@linkplain List#equals(Object) equal to} the supplied - * {@link List}. + * Returns an immutable {@link List} consisting solely of the qualifier (meta-) qualifier. * - *

The returned {@link List} may be the supplied {@link List} or a different instance.

+ * @return an immutable {@link List}; never {@code null} * - * @param list a {@link List} of {@link NamedAttributeMap}s; must not be {@code null} + * @see #qualifier() + */ + public static final List qualifiers() { + return QUALIFIERS; + } + + /** + * Returns an unmodifiable {@link List} consisting only of those {@link Attributes} in the supplied {@link + * Collection} that {@linkplain #qualifier(Attributes) are qualifiers}. * - * @return an immutable {@link List} of {@link NamedAttributeMap}s that is {@linkplain List#equals(Object) equal to} the supplied - * {@link List}; never {@code null} + * @param c a {@link Collection} of {@link Attributes}s; must not be {@code null} * - * @exception NullPointerException if {@code list} is {@code null} + * @return an unmodifiable {@link List} consisting only of those {@link Attributes}s in the supplied {@link + * Collection} that {@linkplain #qualifier(Attributes) are qualifiers}; never {@code null} + * + * @exception NullPointerException if {@code c} is {@code null} */ - public static final List> normalize(final List> list) { - return switch (list) { - case null -> throw new NullPointerException("list"); - case List> l when l.size() == 1 && anyQualifier(l.get(0)) -> anyQualifiers(); - case List> l when l.size() == 1 && defaultQualifier(l.get(0)) -> defaultQualifiers(); - case List> l when l.size() == 2 && anyQualifier(l.get(0)) && defaultQualifier(l.get(1)) -> anyAndDefaultQualifiers(); - default -> List.copyOf(list); + public static final List qualifiers(final Collection c) { + return switch (c) { + case Collection c0 when c0.isEmpty() -> List.of(); + case Collection c0 when c0.equals(defaultQualifiers()) -> defaultQualifiers(); + case Collection c0 when c0.equals(anyAndDefaultQualifiers()) -> anyAndDefaultQualifiers(); + default ->{ + final ArrayList list = new ArrayList<>(c.size()); + for (final Attributes a : c) { + if (qualifier(a)) { + list.add(normalize(a)); + } + } + list.trimToSize(); + yield Collections.unmodifiableList(list); + } }; }