/*
 * #%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 java.util.concurrent.ExecutionException;

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

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import io.wcm.qa.glnm.exceptions.GaleniumException;


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

  private Matcher<M> internalMatcher;
  private CacheLoader<T, M> loader = new CacheLoader<T, M>() {
    @Override
    public M load(T key) throws Exception {
      return map(key);
    };
  };
  private LoadingCache<T, M> mappedItems = CacheBuilder.newBuilder().build(loader);

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

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

  private M mapped(T item) {
    try {
      return mappedItems.get(item);
    }
    catch (ExecutionException ex) {
      throw new GaleniumException("when getting mapped value", ex);
    }
  }

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

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

  protected abstract M map(T item);

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

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

}

@@ -666,49 +666,60 @@
 ort 
-java.util.concurrent.ExecutionException;%0A
+org.checkerframework.checker.nullness.qual.Nullable;
 %0Aimp
@@ -815,16 +815,56 @@
 tcher;%0A%0A
+import com.google.common.base.Optional;%0A
 import c
@@ -994,61 +994,8 @@
 e;%0A%0A
-import io.wcm.qa.glnm.exceptions.GaleniumException;%0A%0A
 %0A/**
@@ -1213,17 +1213,27 @@
 ader%3CT, 
-M
+Optional%3CM%3E
 %3E loader
@@ -1254,17 +1254,27 @@
 ader%3CT, 
-M
+Optional%3CM%3E
 %3E() %7B%0A  
@@ -1296,17 +1296,27 @@
  public 
-M
+Optional%3CM%3E
  load(T 
@@ -1356,16 +1356,38 @@
 urn 
+Optional.fromNullable(
 map(key)
 ;%0A  
@@ -1382,16 +1382,17 @@
 map(key)
+)
 ;%0A    %7D;
@@ -1423,17 +1423,27 @@
 ache%3CT, 
-M
+Optional%3CM%3E
 %3E mapped
@@ -1767,26 +1767,44 @@
     
-try %7B%0A      return
+@Nullable%0A    Optional%3CM%3E optional =
  map
@@ -1815,16 +1815,25 @@
 tems.get
+IfPresent
 (item);%0A
@@ -1840,119 +1840,107 @@
     
-%7D%0A    catch (ExecutionException ex) %7B%0A      throw new GaleniumException(%22when getting mapped value%22, ex);%0A    %7D
+if (optional != null && optional.isPresent()) %7B%0A      return optional.get();%0A    %7D%0A    return null;
 %0A  %7D

fix caching null values with Optional