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