一、前言
关于最近在解读aether-spi包中时候org.sonatype.aether.spi.connector.ArtifactDownload源码中发现其中实现大量java.util.Collections的方法Collections.emptySet()、Collections.emptyList()、Collections.singleton(返回一个不可变的集合只包含指定对象)等,于是找到相关资料和部分源码,对于代码逻辑严谨、性能的考虑使用Collections的emtypList、emptySet的处理比较合理(对于避免出现非初始引起的空指针异常NullPointerException等未可预见问题)。
二、源码部分
1.org.sonatype.aether.spi.connector.ArtifactDownload应用Collections源码示例
package org.sonatype.aether.spi.connector;@b@@b@import java.io.File;@b@import java.util.Collection;@b@import java.util.Collections;@b@import java.util.List;@b@import org.sonatype.aether.artifact.Artifact;@b@import org.sonatype.aether.repository.RemoteRepository;@b@import org.sonatype.aether.transfer.ArtifactTransferException;@b@@b@public class ArtifactDownload extends ArtifactTransfer@b@{@b@ private boolean existenceCheck;@b@ private String checksumPolicy = "";@b@ private String context = "";@b@ private Collection<String> contexts = Collections.emptySet();@b@ private List<RemoteRepository> repositories = Collections.emptyList();@b@@b@ public ArtifactDownload()@b@ {@b@ }@b@@b@ public ArtifactDownload(Artifact artifact, String context, File file, String checksumPolicy)@b@ {@b@ setArtifact(artifact);@b@ setRequestContext(context);@b@ setFile(file);@b@ setChecksumPolicy(checksumPolicy);@b@ }@b@@b@ public ArtifactDownload setArtifact(Artifact artifact)@b@ {@b@ super.setArtifact(artifact);@b@ return this;@b@ }@b@@b@ public File getFile()@b@ {@b@ return super.getFile();@b@ }@b@@b@ public ArtifactDownload setFile(File file)@b@ {@b@ super.setFile(file);@b@ return this;@b@ }@b@@b@ public boolean isExistenceCheck()@b@ {@b@ return this.existenceCheck;@b@ }@b@@b@ public ArtifactDownload setExistenceCheck(boolean existenceCheck)@b@ {@b@ this.existenceCheck = existenceCheck;@b@ return this;@b@ }@b@@b@ public String getChecksumPolicy()@b@ {@b@ return this.checksumPolicy;@b@ }@b@@b@ public ArtifactDownload setChecksumPolicy(String checksumPolicy)@b@ {@b@ this.checksumPolicy = ((checksumPolicy != null) ? checksumPolicy : "");@b@ return this;@b@ }@b@@b@ public String getRequestContext()@b@ {@b@ return this.context;@b@ }@b@@b@ public ArtifactDownload setRequestContext(String context)@b@ {@b@ this.context = ((context != null) ? context : "");@b@ if (Transfer.State.NEW.equals(getState()))@b@ {@b@ this.contexts = Collections.singleton(context);@b@ }@b@ return this;@b@ }@b@@b@ public Collection<String> getSupportedContexts()@b@ {@b@ return this.contexts;@b@ }@b@@b@ public ArtifactDownload setSupportedContexts(Collection<String> contexts)@b@ {@b@ if ((contexts == null) || (contexts.isEmpty()))@b@ {@b@ this.contexts = Collections.singleton(this.context);@b@ }@b@ else@b@ {@b@ this.contexts = contexts;@b@ }@b@ return this;@b@ }@b@@b@ public List<RemoteRepository> getRepositories()@b@ {@b@ return this.repositories;@b@ }@b@@b@ public ArtifactDownload setRepositories(List<RemoteRepository> repositories)@b@ {@b@ if (repositories == null)@b@ {@b@ this.repositories = Collections.emptyList();@b@ }@b@ else@b@ {@b@ this.repositories = repositories;@b@ }@b@ return this;@b@ }@b@@b@ public ArtifactDownload setException(ArtifactTransferException exception)@b@ {@b@ super.setException(exception);@b@ return this;@b@ }@b@@b@ public String toString()@b@ {@b@ return getState() + " " + getArtifact() + " - " + ((isExistenceCheck()) ? "?" : "") + getFile();@b@ }@b@}
2.java.util.Collections的源码关键部分,当然对于还有很对其他功能Collections.sort、Collections.binarySearch及SynchronizedList、min、max等常用方法
public class Collections {@b@ @b@ ...@b@ @b@ public static final <T> List<T> emptyList() {@b@ return (List<T>) EMPTY_LIST;@b@ }@b@@b@ /**@b@ * @serial include@b@ */@b@ private static class EmptyList@b@ extends AbstractList<Object>@b@ implements RandomAccess, Serializable {@b@ // use serialVersionUID from JDK 1.2.2 for interoperability@b@ private static final long serialVersionUID = 8842843931221139166L;@b@@b@ public int size() {return 0;}@b@@b@ public boolean contains(Object obj) {return false;}@b@@b@ public Object get(int index) {@b@ throw new IndexOutOfBoundsException("Index: "+index);@b@ }@b@@b@ // Preserves singleton property@b@ private Object readResolve() {@b@ return EMPTY_LIST;@b@ }@b@ }@b@ ...@b@ @b@ public static final <T> Set<T> emptySet() {@b@ return (Set<T>) EMPTY_SET;@b@ }@b@@b@ /**@b@ * @serial include@b@ */@b@ private static class EmptySet extends AbstractSet<Object> implements Serializable {@b@ // use serialVersionUID from JDK 1.2.2 for interoperability@b@ private static final long serialVersionUID = 1582296315990362920L;@b@@b@ public Iterator<Object> iterator() {@b@ return new Iterator<Object>() {@b@ public boolean hasNext() {@b@ return false;@b@ }@b@ public Object next() {@b@ throw new NoSuchElementException();@b@ }@b@ public void remove() {@b@ throw new UnsupportedOperationException();@b@ }@b@ };@b@ }@b@@b@ public int size() {return 0;}@b@@b@ public boolean contains(Object obj) {return false;}@b@@b@ // Preserves singleton property@b@ private Object readResolve() {@b@ return EMPTY_SET;@b@ }@b@ }@b@ @b@ ...@b@ @b@ public static <T> Set<T> singleton(T o) {@b@ return new SingletonSet<T>(o);@b@ }@b@@b@ @b@ private static class SingletonSet<E>@b@ extends AbstractSet<E>@b@ implements Serializable@b@ {@b@ // use serialVersionUID from JDK 1.2.2 for interoperability@b@ private static final long serialVersionUID = 3193687207550431679L;@b@@b@ final private E element;@b@@b@ SingletonSet(E e) {element = e;}@b@@b@ public Iterator<E> iterator() {@b@ return new Iterator<E>() {@b@ private boolean hasNext = true;@b@ public boolean hasNext() {@b@ return hasNext;@b@ }@b@ public E next() {@b@ if (hasNext) {@b@ hasNext = false;@b@ return element;@b@ }@b@ throw new NoSuchElementException();@b@ }@b@ public void remove() {@b@ throw new UnsupportedOperationException();@b@ }@b@ };@b@ }@b@@b@ public int size() {return 1;}@b@@b@ public boolean contains(Object o) {return eq(o, element);}@b@ }