
com.github.joekerouac.common.tools.resource.impl.ClassPathResource Maven / Gradle / Ivy
The newest version!
// Generated by delombok at Fri Mar 14 11:41:38 CST 2025
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.github.joekerouac.common.tools.resource.impl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import com.github.joekerouac.common.tools.constant.ExceptionProviderConst;
import com.github.joekerouac.common.tools.reflect.ClassUtils;
import com.github.joekerouac.common.tools.resource.Resource;
import com.github.joekerouac.common.tools.enums.ResourceType;
import com.github.joekerouac.common.tools.resource.exception.ResourceLoadException;
import com.github.joekerouac.common.tools.resource.exception.ResourceNotExistException;
import com.github.joekerouac.common.tools.util.Assert;
/**
* ClassPathResource
*
* @author JoeKerouac
* @date 2022-10-14 14:37:00
* @since 1.0.0
*/
public class ClassPathResource implements Resource {
/**
* {@link #path}字段名
*/
public static final String PATH_FIELD_NAME = "path";
/**
* 路径,例如com/github/joekerouac/common/tools/resource/impl/ClassPathResource
*/
private final String path;
private final transient InputStream stream;
private final transient URL url;
public ClassPathResource(Map map) {
this(map.get(PATH_FIELD_NAME));
}
public ClassPathResource(String path) {
Assert.notBlank(path, "path must not be blank", ExceptionProviderConst.IllegalArgumentExceptionProvider);
this.path = path;
ClassLoader loader = ClassUtils.getDefaultClassLoader();
try {
Enumeration resources = loader.getResources(path);
List urls = new ArrayList<>();
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
urls.add(url);
}
if (urls.size() <= 0) {
throw new ResourceNotExistException(path);
}
this.url = urls.get(urls.size() - 1);
this.stream = url.openStream();
} catch (IOException e) {
throw new ResourceLoadException(e);
}
}
@Override
public Map toMap() {
return Collections.singletonMap(PATH_FIELD_NAME, path);
}
@Override
public ResourceType type() {
return ResourceType.CLASS_PATH;
}
@Override
public InputStream getInputStream() throws IOException {
return stream;
}
@Override
public String getName() {
return path;
}
@Override
public URL getUrl() throws IOException {
return url;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof ClassPathResource)) return false;
final ClassPathResource other = (ClassPathResource) o;
if (!other.canEqual((java.lang.Object) this)) return false;
final java.lang.Object this$path = this.getPath();
final java.lang.Object other$path = other.getPath();
if (this$path == null ? other$path != null : !this$path.equals(other$path)) return false;
return true;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
protected boolean canEqual(final java.lang.Object other) {
return other instanceof ClassPathResource;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public int hashCode() {
final int PRIME = 59;
int result = 1;
final java.lang.Object $path = this.getPath();
result = result * PRIME + ($path == null ? 43 : $path.hashCode());
return result;
}
/**
* 路径,例如com/github/joekerouac/common/tools/resource/impl/ClassPathResource
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public String getPath() {
return this.path;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy