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.core.util.objects.checker;
018
019import java.util.List;
020
021import org.apache.wicket.util.lang.Args;
022import org.apache.wicket.util.lang.Generics;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * A base class for IObjectChecker implementations which handles the logic
028 * for checking type exclusions.
029 */
030public abstract class AbstractObjectChecker implements IObjectChecker
031{
032        private static final Logger LOGGER = LoggerFactory.getLogger(AbstractObjectChecker.class);
033
034        private final List<Class<?>> exclusions;
035
036        protected AbstractObjectChecker()
037        {
038                this(Generics.<Class<?>>newArrayList());
039        }
040
041        protected AbstractObjectChecker(List<Class<?>> exclusions)
042        {
043                this.exclusions = Args.notNull(exclusions, "exclusions");
044        }
045
046        @Override
047        public Result check(Object object)
048        {
049                Result result = Result.SUCCESS;
050
051                if (object != null && getExclusions().isEmpty() == false)
052                {
053                        Class<?> objectType = object.getClass();
054                        for (Class<?> excludedType : getExclusions())
055                        {
056                                if (excludedType.isAssignableFrom(objectType))
057                                {
058                                        LOGGER.debug("Object with type '{}' wont be checked because its type is excluded ({})",
059                                                        objectType, excludedType);
060                                        return result;
061                                }
062                        }
063                }
064
065                result = doCheck(object);
066
067                return result;
068        }
069
070        /**
071         * The implementations should make the specific check on the object.
072         * @param object
073         *      the object to check
074         * @return the {@link Result result} of the specific check
075         */
076        protected Result doCheck(Object object)
077        {
078                return Result.SUCCESS;
079        }
080
081        @Override
082        public List<Class<?>> getExclusions()
083        {
084                return exclusions;
085        }
086}