001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.origin;
018
019import java.io.File;
020
021/**
022 * Interface that uniquely represents the origin of an item. For example, an item loaded
023 * from a {@link File} may have an origin made up of the file name along with line/column
024 * numbers.
025 * <p>
026 * Implementations must provide sensible {@code hashCode()}, {@code equals(...)} and
027 * {@code #toString()} implementations.
028 *
029 * @author Madhura Bhave
030 * @author Phillip Webb
031 * @since 2.0.0
032 * @see OriginProvider
033 */
034public interface Origin {
035
036        /**
037         * Find the {@link Origin} that an object originated from. Checks if the source object
038         * is an {@link OriginProvider} and also searches exception stacks.
039         * @param source the source object or {@code null}
040         * @return an optional {@link Origin}
041         */
042        static Origin from(Object source) {
043                if (source instanceof Origin) {
044                        return (Origin) source;
045                }
046                Origin origin = null;
047                if (source != null && source instanceof OriginProvider) {
048                        origin = ((OriginProvider) source).getOrigin();
049                }
050                if (origin == null && source != null && source instanceof Throwable) {
051                        return from(((Throwable) source).getCause());
052                }
053                return origin;
054        }
055
056}