package votorola.g.web.wic; // Copyright 2008-2009, 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import org.apache.wicket.model.*; import votorola.g.lang.*; /** A model wrapper that enforces a maximum length of character sequences. For use only * with models that hold character sequences. (Otherwise, the associated IConverter * should eagerly enforce its own constraints, at conversion time.) * * @see CharSequence */ public final @ThreadSafe class ModelLengthLimiter extends AbstractWrapModel { /** Constructs a ModelLengthLimiter, or throws an IllegalArgumentException * if the current value of the wrapped model exceeds the constraint. * * @throws ClassCastException if the current value is neither a CharSequence, nor null * * @see #getWrappedModel() * @see #getMaxLength() */ public ModelLengthLimiter( IModel _wrappedModel, int _maxLength ) throws ClassCastException { wrappedModel = _wrappedModel; maxLength = _maxLength; lengthConstrained( wrappedModel.getObject(), maxLength ); } /** The length constraint. */ public final int getMaxLength() { return maxLength; } private final int maxLength; // /** Sets the length constraint. // * // * @see #getMaxLength() // */ // public final void setMaxLength( int newMaxLength ) { maxLength = newMaxLength; } /** Ensures that character sequence s does not exceed the specified maximum length. * * @return the same sequence s * @throws IllegalArgumentException if sequence s exceeds maxLength */ public static S lengthConstrained( final S s, final int maxLength ) { if( s == null || s.length() <= maxLength ) return s; throw new IllegalArgumentException( "exceeds " + maxLength + " characters: \"" + s.subSequence(0,16) + "...\"" ); } // - I - M o d e l -------------------------------------------------------------------- /** Sets the model object; or throws an IllegalArgumentException if the object's * string value is longer than the {@linkplain #getMaxLength maximum length}. * * @throws ClassCastException if the object is neither a CharSequence, nor null */ public @Override void setObject( S o ) throws ClassCastException { wrappedModel.setObject( lengthConstrained( o, maxLength )); } public @Override S getObject() { return wrappedModel.getObject(); } // - I - W r a p - M o d e l ---------------------------------------------------------- public IModel getWrappedModel() { return wrappedModel; } private final IModel wrappedModel; }