/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
public abstract class MaxLengthFacetAbstract 
    extends SingleIntValueFacetAbstract implements MaxLengthFacet {

    public String invalidates(
            final ValidityContext<? extends ValidityEvent> context) {
        if (!(context instanceof ProposedHolder)) {
            return null;
        }
        final ProposedHolder proposedHolder = (ProposedHolder) context;
        final NakedObject proposedArgument = proposedHolder.getProposed();
        if (!exceeds(proposedArgument)) {
            return null;
        }
        return "Proposed value '" + proposedArgument.titleString() + 
            "' exceeds the maximum length of " + value();
    }

    public boolean exceeds(final NakedObject nakedObject) {
        final String str = unwrapString(nakedObject);
        if (str == null) {
            return false;
        }
        final int maxLength = value();
        return maxLength != 0 && str.length() > maxLength;
    }

    ...
}
