33 * Copyright © 2024–2025 microBean™.
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6- * the License. You may obtain a copy of the License at
6+ * the License. You may obtain a copy of the License at
77 *
88 * http://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1212 * specific language governing permissions and limitations under the License.
1313 */
1414package org .microbean .bean ;
1515
1616import java .util .Collection ;
1717import java .util .Collections ;
18- import java .util .LinkedHashSet ;
19- import java .util .List ;
2018import java .util .SequencedSet ;
2119
20+ import java .util .function .Function ;
21+
22+ import static java .util .Collections .unmodifiableSequencedSet ;
23+
24+ import static java .util .LinkedHashSet .newLinkedHashSet ;
25+
2226/**
2327 * An object with {@linkplain AttributedElement dependencies}.
2428 *
25- * <p>By default, {@link Aggregate}s have no dependencies.</p>
29+ * <p>By default, {@link Aggregate}s have {@linkplain #EMPTY_DEPENDENCIES no dependencies} .</p>
2630 *
2731 * @author <a href="https://about.me/lairdnelson/" target="_top">Laird Nelson</a>
2832 *
@@ -39,12 +43,12 @@ public interface Aggregate {
3943 /**
4044 * An immutable, empty {@link SequencedSet} of {@link Assignment}s.
4145 */
42- public static final SequencedSet <Assignment <?>> EMPTY_ASSIGNMENTS = Collections . unmodifiableSequencedSet (new LinkedHashSet <> (0 ));
46+ public static final SequencedSet <Assignment <?>> EMPTY_ASSIGNMENTS = unmodifiableSequencedSet (newLinkedHashSet (0 ));
4347
4448 /**
4549 * An immutable, empty {@link SequencedSet} of {@link AttributedElement}s.
4650 */
47- public static final SequencedSet <AttributedElement > EMPTY_DEPENDENCIES = Collections . unmodifiableSequencedSet (new LinkedHashSet <> (0 ));
51+ public static final SequencedSet <AttributedElement > EMPTY_DEPENDENCIES = unmodifiableSequencedSet (newLinkedHashSet (0 ));
4852
4953
5054 /*
@@ -53,9 +57,9 @@ public interface Aggregate {
5357
5458
5559 /**
56- * Returns an unmodifiable {@link SequencedSet} of {@link AttributedElement} instances.
60+ * Returns an immutable {@link SequencedSet} of {@link AttributedElement} instances.
5761 *
58- * @return an unmodifiable {@link SequencedSet} of {@link AttributedElement} instances; never {@code null}
62+ * @return an immutable {@link SequencedSet} of {@link AttributedElement} instances; never {@code null}
5963 *
6064 * @see AttributedElement
6165 */
@@ -64,24 +68,26 @@ public default SequencedSet<AttributedElement> dependencies() {
6468 }
6569
6670 /**
67- * Assigns a contextual reference to each of this {@link Aggregate}'s {@link AttributedElement} instances and returns the
68- * resulting {@link List } of {@link Assignment}s.
71+ * A convenience method that assigns a contextual reference to each of this {@link Aggregate}'s {@link
72+ * AttributedElement} instances and returns the resulting {@link SequencedSet } of {@link Assignment}s.
6973 *
70- * @param r a {@link Request}; must not be {@code null}
74+ * <p>Typically there is no need to override this method.</p>
7175 *
72- * @return a {@link List} of {@link Assignment} instances; never {@code null}
76+ * @param r a {@link Function} that retrieves a contextual reference suitable for an {@link AttributedType}; if {@link
77+ * #dependencies()} returns a non-empty {@link SequencedSet} then this argument must not be {@code null}; normally a
78+ * reference to the {@link Request#reference(AttributedType)} method
79+ *
80+ * @return an immutable {@link SequencedSet} of {@link Assignment} instances; never {@code null}
7381 *
7482 * @exception NullPointerException if {@code r} is {@code null}
7583 */
76- public default SequencedSet <? extends Assignment <?>> assign (final Request < ?> r ) {
84+ public default SequencedSet <? extends Assignment <?>> assign (final Function <? super AttributedType , ?> r ) {
7785 final Collection <? extends AttributedElement > ds = this .dependencies ();
7886 if (ds == null || ds .isEmpty ()) {
7987 return EMPTY_ASSIGNMENTS ;
8088 }
81- final SequencedSet <Assignment <?>> assignments = new LinkedHashSet <>();
82- for (final AttributedElement d : ds ) {
83- assignments .add (new Assignment <>(d , r .reference (d .attributedType ())));
84- }
89+ final SequencedSet <Assignment <?>> assignments = newLinkedHashSet (ds .size ());
90+ ds .forEach (d -> assignments .add (new Assignment <>(d , r .apply (d .attributedType ()))));
8591 return Collections .unmodifiableSequencedSet (assignments );
8692 }
8793
0 commit comments