001package 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.
002
003import org.apache.wicket.model.*;
004import votorola.g.lang.*;
005
006
007/** A model wrapper that enforces a maximum length of character sequences.  For use only
008  * with models that hold character sequences.  (Otherwise, the associated IConverter
009  * should eagerly enforce its own constraints, at conversion time.)
010  *
011  *     @see CharSequence
012  */
013public final @ThreadSafe class ModelLengthLimiter<S extends CharSequence>
014  extends AbstractWrapModel<S>
015{
016
017
018    /** Constructs a ModelLengthLimiter, or throws an IllegalArgumentException
019      * if the current value of the wrapped model exceeds the constraint.
020      *
021      *     @throws ClassCastException if the current value is neither a CharSequence, nor null
022      *
023      *     @see #getWrappedModel()
024      *     @see #getMaxLength()
025      */
026    public ModelLengthLimiter( IModel<S> _wrappedModel, int _maxLength ) throws ClassCastException
027    {
028        wrappedModel = _wrappedModel;
029        maxLength = _maxLength;
030
031        lengthConstrained( wrappedModel.getObject(), maxLength );
032    }
033
034
035
036    /** The length constraint.
037      */
038    public final int getMaxLength() { return maxLength; }
039
040
041        private final int maxLength;
042
043
044     // /** Sets the length constraint.
045     //   *
046     //   *     @see #getMaxLength()
047     //   */
048     // public final void setMaxLength( int newMaxLength ) { maxLength = newMaxLength; }
049
050
051
052    /** Ensures that character sequence s does not exceed the specified maximum length.
053      *
054      *     @return the same sequence s
055      *     @throws IllegalArgumentException if sequence s exceeds maxLength
056      */
057    public static <S extends CharSequence> S lengthConstrained( final S s, final int maxLength )
058    {
059        if( s == null || s.length() <= maxLength ) return s;
060
061        throw new IllegalArgumentException( "exceeds " + maxLength + " characters: \""
062          + s.subSequence(0,16) + "...\""  );
063    }
064
065
066
067   // - I - M o d e l --------------------------------------------------------------------
068
069
070    /** Sets the model object; or throws an IllegalArgumentException if the object's
071      * string value is longer than the {@linkplain #getMaxLength maximum length}.
072      *
073      *     @throws ClassCastException if the object is neither a CharSequence, nor null
074      */
075    public @Override void setObject( S o ) throws ClassCastException
076    {
077        wrappedModel.setObject( lengthConstrained( o, maxLength ));
078    }
079
080
081
082    public @Override S getObject() { return wrappedModel.getObject(); }
083
084
085
086   // - I - W r a p - M o d e l ----------------------------------------------------------
087
088
089    public IModel<S> getWrappedModel() { return wrappedModel; }
090
091
092        private final IModel<S> wrappedModel;
093
094
095
096}