001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.validation;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.wicket.model.IModel;
024
025/**
026 * This implementation of {@link IValidatable} is meant to be used outside of Wicket. It allows
027 * other parts of the application to utilize {@link IValidator}s for validation.
028 * <p>
029 * Example: <code><pre>
030 * class WebService
031 * {
032 *      public void addUser(String firstName, String lastName)
033 *      {
034 *              Validatable standin = new Validatable();
035 *              standin.setValue(firstName);
036 *              new FirstNameValidator().validate(standin);
037 *              standing.setValue(lastName);
038 *              new LastNameValidator().validate(standin);
039 *              if (!standin.isValid())
040 *              {
041 *                      // roll your own ValidationException
042 *                      throw new ValidationException(standin.getErrors());
043 *              }
044 *              else
045 *              {
046 *                      // add user here
047 *              }
048 *      }
049 * }
050 * </pre></code>
051 * 
052 * @author Igor Vaynberg (ivaynberg)
053 * @param <T>
054 *            type of validatable
055 * @since 1.2.6
056 */
057public class Validatable<T> implements IValidatable<T>
058{
059        /** the value object */
060        private T value;
061
062        /** the list of errors */
063        private ArrayList<IValidationError> errors;
064
065        private IModel<T> model;
066
067        /**
068         * Constructor.
069         */
070        public Validatable()
071        {
072        }
073
074        /**
075         * Constructor.
076         * 
077         * @param value
078         *            The value that will be tested
079         */
080        public Validatable(T value)
081        {
082                this.value = value;
083        }
084
085        /**
086         * Sets model
087         * 
088         * @param model
089         */
090        public void setModel(IModel<T> model)
091        {
092                this.model = model;
093        }
094
095        /**
096         * Sets the value object that will be returned by {@link #getValue()}.
097         * 
098         * @param value
099         *            the value object
100         */
101        public void setValue(T value)
102        {
103                this.value = value;
104        }
105
106        /**
107         * @see IValidatable#getValue()
108         */
109        @Override
110        public T getValue()
111        {
112                return value;
113        }
114
115        /**
116         * @see IValidatable#error(IValidationError)
117         */
118        @Override
119        public void error(IValidationError error)
120        {
121                if (errors == null)
122                {
123                        errors = new ArrayList<IValidationError>();
124                }
125                errors.add(error);
126        }
127
128        /**
129         * Retrieves an unmodifiable list of any errors reported against this <code>IValidatable</code>
130         * instance.
131         * 
132         * @return an unmodifiable list of errors
133         */
134        public List<IValidationError> getErrors()
135        {
136                if (errors == null)
137                {
138                        return Collections.emptyList();
139                }
140                else
141                {
142                        return Collections.unmodifiableList(errors);
143                }
144        }
145
146        /**
147         * @see IValidatable#isValid()
148         */
149        @Override
150        public boolean isValid()
151        {
152                return errors == null;
153        }
154
155        @Override
156        public IModel<T> getModel()
157        {
158                return model;
159        }
160
161}