com.oracle.tools.deferred.DeferredMatch Maven / Gradle / Ivy
/*
* File: DeferredMatch.java
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* The contents of this file are subject to the terms and conditions of
* the Common Development and Distribution License 1.0 (the "License").
*
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the License by consulting the LICENSE.txt file
* distributed with this file, or by consulting https://oss.oracle.com/licenses/CDDL
*
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file LICENSE.txt.
*
* MODIFICATIONS:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*/
package com.oracle.tools.deferred;
import org.hamcrest.Matcher;
/**
* A {@link DeferredMatch} is a {@link Deferred} that represents
* a Hamcrest {@link Matcher}, that of which must be satisfied for a
* specified {@link Deferred} object at some point in the future.
*
* A {@link DeferredMatch} will only return true
when the match
* is successful, otherwise null
will be returned.
*
* Copyright (c) 2012. All Rights Reserved. Oracle Corporation.
* Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
*
* @author Brian Oliver
*/
public class DeferredMatch implements Deferred
{
/**
* The {@link Deferred} that we want to match with a {@link Matcher}.
*/
private Deferred m_deferred;
/**
* The {@link Matcher} to use against the {@link Deferred}.
*/
private Matcher> m_matcher;
/**
* The last {@link Deferred} value use when attempting the match.
*
* This is useful for reporting why a {@link DeferredMatch} may have
* failed.
*/
private T m_lastUsedMatchValue;
/**
* Constructs a {@link DeferredMatch} for the {@link Deferred}
* with the specified {@link Matcher}.
*
* @param deferred the {@link Deferred} to match against
* @param matcher the {@link Matcher}
*/
public DeferredMatch(Deferred deferred,
Matcher> matcher)
{
m_deferred = deferred;
m_matcher = matcher;
m_lastUsedMatchValue = null;
}
/**
* Obtains the adapted {@link Deferred}.
*
* @return the adapted {@link Deferred}
*/
public Deferred getDeferred()
{
return m_deferred;
}
/**
* Obtains the {@link Matcher} to apply.
*
* @return the {@link Matcher}
*/
public Matcher> getMatcher()
{
return m_matcher;
}
/**
* Obtains the value used when attempting to perform a match.
* This is useful for determining why a match may have failed.
*
* @return the last value (obtained from {@link #getDeferred()}) that
* was used for matching
*/
public T getLastUsedMatchValue()
{
return m_lastUsedMatchValue;
}
/**
* {@inheritDoc}
*/
@Override
public Boolean get() throws UnresolvableInstanceException, InstanceUnavailableException
{
try
{
m_lastUsedMatchValue = m_deferred.get();
if (m_matcher.matches(m_lastUsedMatchValue))
{
return true;
}
else
{
throw new InstanceUnavailableException(this);
}
}
catch (InstanceUnavailableException e)
{
throw e;
}
catch (UnresolvableInstanceException e)
{
throw e;
}
catch (Exception e)
{
throw new UnresolvableInstanceException(this, e);
}
}
/**
* {@inheritDoc}
*/
@Override
public Class getDeferredClass()
{
return Boolean.class;
}
/**
* {@inheritDoc}
*/
@Override
public String toString()
{
return String.format("DeferredMatch{deferred=%s, matcher=%s, lastUsedMatchValue=%s}",
m_deferred,
m_matcher,
m_lastUsedMatchValue);
}
}