org.gradle.internal.classpath.DefaultClassPath Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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 org.gradle.internal.classpath;
import org.gradle.api.specs.NotSpec;
import org.gradle.api.specs.Spec;
import org.gradle.internal.Cast;
import org.gradle.internal.UncheckedException;
import org.gradle.util.internal.CollectionUtils;
import java.io.File;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* An immutable classpath.
*/
public class DefaultClassPath implements ClassPath, Serializable {
public static Builder builderWithExactSize(int size) {
return new Builder(size);
}
public static ClassPath of(Iterable files) {
if (files == null) {
return EMPTY;
} else if (files instanceof Collection) {
return of((Collection) files);
} else {
List list = new ArrayList();
for (File file : files) {
list.add(file);
}
return of(list);
}
}
public static ClassPath of(File... files) {
if (files == null || files.length == 0) {
return EMPTY;
} else {
return of(Arrays.asList(files));
}
}
/**
* Only here for the Kotlin DSL, use {@link #of(Iterable)} instead.
*/
public static ClassPath of(Collection files) {
if (files == null || files.isEmpty()) {
return EMPTY;
} else {
return new DefaultClassPath(ImmutableUniqueList.of(files));
}
}
private final ImmutableUniqueList files;
DefaultClassPath() {
this(ImmutableUniqueList.empty());
}
protected DefaultClassPath(ImmutableUniqueList files) {
this.files = files;
}
@Override
public String toString() {
return files.toString();
}
@Override
public boolean isEmpty() {
return files.isEmpty();
}
@Override
public List getAsURIs() {
List urls = new ArrayList(files.size());
for (File file : files) {
urls.add(file.toURI());
}
return urls;
}
@Override
public List getAsFiles() {
return files;
}
@Override
public URL[] getAsURLArray() {
URL[] urls = new URL[files.size()];
int i = 0;
for (File file : files) {
urls[i++] = toURL(file);
}
return urls;
}
@Override
public List getAsURLs() {
List urls = new ArrayList(files.size());
for (File file : files) {
urls.add(toURL(file));
}
return urls;
}
private static URL toURL(File file) {
try {
return file.toURI().toURL();
} catch (MalformedURLException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
@Override
public ClassPath plus(ClassPath other) {
if (isEmpty()) {
return other;
}
if (other.isEmpty()) {
return this;
}
return new DefaultClassPath(concat(files, other.getAsFiles()));
}
@Override
public ClassPath plus(Collection other) {
if (other.isEmpty()) {
return this;
}
return new DefaultClassPath(concat(files, other));
}
@Override
public ClassPath removeIf(Spec super File> filter) {
List remainingFiles = CollectionUtils.filter(files, new NotSpec(filter));
if (remainingFiles.size() == files.size()) {
return this;
}
return DefaultClassPath.of(remainingFiles);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != getClass()) {
return false;
}
DefaultClassPath other = (DefaultClassPath) obj;
return files.equals(other.files);
}
@Override
public int hashCode() {
return files.hashCode();
}
private static ImmutableUniqueList concat(Collection files1, Collection files2) {
Set result = new LinkedHashSet();
result.addAll(files1);
result.addAll(files2);
return new ImmutableUniqueList(result);
}
public static class Builder {
private final ImmutableUniqueList.Builder uniqueListBuilder;
public Builder(int exactSize) {
uniqueListBuilder = ImmutableUniqueList.builderWithExactSize(exactSize);
}
public void add(File file) {
uniqueListBuilder.add(file);
}
public ClassPath build() {
return new DefaultClassPath(uniqueListBuilder.buildWithExactSize());
}
}
protected static final class ImmutableUniqueList extends AbstractList implements Serializable {
private static final ImmutableUniqueList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy