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;
022
023/**
024 * IObjectChecker can be used to check whether an object has/has not given state
025 * before serializing it. The serialization will be stopped if the object doesn't pass
026 * the {@code #check(Object) check}.
027 */
028public interface IObjectChecker
029{
030        /**
031         * Represents the result of a check.
032         */
033        class Result
034        {
035                public enum Status
036                {
037                        /**
038                         * The check is successful
039                         */
040                        SUCCESS,
041
042                        /**
043                         * The check failed for some reason
044                         */
045                        FAILURE
046                }
047
048                /**
049                 * A singleton that can be used for successful checks
050                 */
051                public static final Result SUCCESS = new Result(Status.SUCCESS, "");
052
053                /**
054                 * The status of the check.
055                 */
056                public final Status status;
057
058                /**
059                 * The reason why a check succeeded/failed. Mandatory in failure case.
060                 */
061                public final String reason;
062
063                /**
064                 * An optional cause of a failure.
065                 */
066                public final Throwable cause;
067
068                /**
069                 * Constructor.
070                 *
071                 * @param status
072                 *      the status of the result
073                 * @param reason
074                 *      the reason of successful/failed check
075                 */
076                public Result(Status status, String reason)
077                {
078                        this(status, reason, null);
079                }
080
081
082                /**
083                 * Constructor.
084                 *
085                 * @param status
086                 *      the status of the result
087                 * @param reason
088                 *      the reason of successful/failed check
089                 * @param cause
090                 *      the cause of a failure. Optional.
091                 */
092                public Result(Status status, String reason, Throwable cause)
093                {
094                        if (status == Status.FAILURE)
095                        {
096                                Args.notEmpty(reason, "reason");
097                        }
098                        this.status = status;
099                        this.reason = reason;
100                        this.cause = cause;
101                }
102
103                @Override
104                public String toString()
105                {
106                        return "Result{" +
107                                        "reason='" + reason + '\'' +
108                                        ", status=" + status +
109                                        '}';
110                }
111        }
112
113        /**
114         * Checks an object that it meets some requirements before serializing it
115         *
116         * @param object
117         *      the object to check
118         * @return a Result object describing whether the check is successful or not
119         */
120        Result check(Object object);
121
122        /**
123         * @return A list of types which should not be checked by this checker
124         */
125        List<Class<?>> getExclusions();
126}