/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2020 wcm.io
 * %%
 * 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
 *
 *      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 specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.qa.glnm.hamcrest;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;


/**
 * <p>Abstract TypeSafeWrappingMatcher class.</p>
 *
 * @since 5.0.0
 */
public abstract class TypeSafeWrappingMatcher<T, M> extends TypeSafeMatcher<T> {

  private Matcher<M> internalMatcher;

  protected TypeSafeWrappingMatcher(Matcher<M> matcher) {
    setInternalMatcher(matcher);
  }

  /** {@inheritDoc} */
  @Override
  public void describeTo(Description description) {
    getInternalMatcher().describeTo(description);
  }

  @Override
  protected void describeMismatchSafely(T item, Description mismatchDescription) {
    getInternalMatcher().describeMismatch(map(item), mismatchDescription);
  }

  protected Matcher<M> getInternalMatcher() {
    return internalMatcher;
  }

  protected abstract M map(T item);

  @Override
  protected boolean matchesSafely(T item) {
    return getInternalMatcher().matches(map(item));
  }

  protected void setInternalMatcher(Matcher<M> internalMatcher) {
    this.internalMatcher = internalMatcher;
  }

}

@@ -655,16 +655,65 @@
 crest;%0A%0A
+import java.util.concurrent.ExecutionException;%0A%0A
 import o
@@ -804,16 +804,204 @@
 tcher;%0A%0A
+import com.google.common.cache.CacheBuilder;%0Aimport com.google.common.cache.CacheLoader;%0Aimport com.google.common.cache.LoadingCache;%0A%0Aimport io.wcm.qa.glnm.exceptions.GaleniumException;%0A%0A
 %0A/**%0A * 
@@ -1189,16 +1189,256 @@
 Matcher;
+%0A  private CacheLoader%3CT, M%3E loader = new CacheLoader%3CT, M%3E() %7B%0A    @Override%0A    public M load(T key) throws Exception %7B%0A      return map(key);%0A    %7D;%0A  %7D;%0A  private LoadingCache%3CT, M%3E mappedItems = CacheBuilder.newBuilder().build(loader);
 %0A%0A  prot
@@ -1669,16 +1669,212 @@
 );%0A  %7D%0A%0A
+  private M mapped(T item) %7B%0A    try %7B%0A      return mappedItems.get(item);%0A    %7D%0A    catch (ExecutionException ex) %7B%0A      throw new GaleniumException(%22when getting mapped value%22, ex);%0A    %7D%0A  %7D%0A%0A
   @Overr
@@ -2001,24 +2001,27 @@
 Mismatch(map
+ped
 (item), mism
@@ -2258,16 +2258,19 @@
 ches(map
+ped
 (item));

add caching to avoid multiple mapping actions