001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2024 microBean™.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
011 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
012 * specific language governing permissions and limitations under the License.
013 */
014package org.microbean.bean;
015
016import java.util.Collection;
017import java.util.List;
018
019import org.microbean.qualifier.NamedAttributeMap;
020
021import static org.microbean.bean.InterceptorBindings.anyInterceptorBinding;
022import static org.microbean.bean.InterceptorBindings.interceptorBindings;
023
024public final class InterceptorBindingsMatcher implements Matcher<Collection<? extends NamedAttributeMap<?>>, Collection<? extends NamedAttributeMap<?>>> {
025
026  public InterceptorBindingsMatcher() {
027    super();
028  }
029
030  @Override // Matcher<Collection<? extends NamedAttributeMap<?>>, Collection<? extends NamedAttributeMap<?>>>
031  public final boolean test(final Collection<? extends NamedAttributeMap<?>> receiverAttributes,
032                            final Collection<? extends NamedAttributeMap<?>> payloadAttributes) {
033    final List<? extends NamedAttributeMap<?>> payloadBindings = interceptorBindings(payloadAttributes);
034    if (payloadBindings.isEmpty()) {
035      return interceptorBindings(receiverAttributes).isEmpty();
036    } else if (payloadBindings.size() == 1 && anyInterceptorBinding(payloadBindings.get(0))) {
037      return true;
038    }
039    final List<? extends NamedAttributeMap<?>> receiverBindings = interceptorBindings(receiverAttributes);
040    return
041      receiverBindings.size() == payloadBindings.size() &&
042      receiverBindings.containsAll(payloadBindings) &&
043      payloadBindings.containsAll(receiverBindings);
044  }
045
046}