001/*
002 * Copyright 2002-2012 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 *      https://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.core.io;
018
019import java.io.FileNotFoundException;
020import java.io.IOException;
021import java.io.InputStream;
022
023/**
024 * Simple {@link Resource} implementation that holds a resource description
025 * but does not point to an actually readable resource.
026 *
027 * <p>To be used as placeholder if a {@code Resource} argument is
028 * expected by an API but not necessarily used for actual reading.
029 *
030 * @author Juergen Hoeller
031 * @since 1.2.6
032 */
033public class DescriptiveResource extends AbstractResource {
034
035        private final String description;
036
037
038        /**
039         * Create a new DescriptiveResource.
040         * @param description the resource description
041         */
042        public DescriptiveResource(String description) {
043                this.description = (description != null ? description : "");
044        }
045
046
047        @Override
048        public boolean exists() {
049                return false;
050        }
051
052        @Override
053        public boolean isReadable() {
054                return false;
055        }
056
057        @Override
058        public InputStream getInputStream() throws IOException {
059                throw new FileNotFoundException(
060                                getDescription() + " cannot be opened because it does not point to a readable resource");
061        }
062
063        @Override
064        public String getDescription() {
065                return this.description;
066        }
067
068
069        /**
070         * This implementation compares the underlying description String.
071         */
072        @Override
073        public boolean equals(Object obj) {
074                return (obj == this ||
075                        (obj instanceof DescriptiveResource && ((DescriptiveResource) obj).description.equals(this.description)));
076        }
077
078        /**
079         * This implementation returns the hash code of the underlying description String.
080         */
081        @Override
082        public int hashCode() {
083                return this.description.hashCode();
084        }
085
086}