001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.xbean.finder; 018 019import org.apache.xbean.finder.filter.Filter; 020 021import java.io.File; 022import java.net.URI; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.Collection; 026import java.util.HashMap; 027import java.util.Iterator; 028import java.util.List; 029import java.util.Map; 030 031import static org.apache.xbean.finder.filter.Filters.invert; 032import static org.apache.xbean.finder.filter.Filters.patterns; 033 034/** 035 * @version $Rev$ $Date$ 036 */ 037public class UriSet implements Iterable<URI> { 038 039 private final Map<String, URI> URIs; 040 041 public UriSet(URI... URIs) { 042 this(Arrays.asList(URIs)); 043 } 044 045 public UriSet(Collection<URI> URIs) { 046 this.URIs = new HashMap<String, URI>(); 047 for (URI location : URIs) { 048 this.URIs.put(location.toASCIIString(), location); 049 } 050 } 051 052 private UriSet(Map<String, URI> URIs) { 053 this.URIs = URIs; 054 } 055 056 public UriSet include(String pattern) { 057 return filter(patterns(pattern)); 058 } 059 060 public UriSet include(UriSet URISet) { 061 Map<String, URI> URIs = new HashMap<String, URI>(this.URIs); 062 URIs.putAll(URISet.URIs); 063 return new UriSet(URIs); 064 } 065 066 067 public UriSet include(URI URI) { 068 Map<String, URI> URIs = new HashMap<String, URI>(this.URIs); 069 URIs.put(URI.toASCIIString(), URI); 070 return new UriSet(URIs); 071 } 072 073 public UriSet exclude(UriSet URISet) { 074 Map<String, URI> URIs = new HashMap<String, URI>(this.URIs); 075 Map<String, URI> parentURIs = URISet.URIs; 076 for (String URI : parentURIs.keySet()) { 077 URIs.remove(URI); 078 } 079 return new UriSet(URIs); 080 } 081 082 public UriSet exclude(URI URI) { 083 Map<String, URI> URIs = new HashMap<String, URI>(this.URIs); 084 URIs.remove(URI.toASCIIString()); 085 return new UriSet(URIs); 086 } 087 088 public UriSet exclude(File file) { 089 return exclude(relative(file)); 090 } 091 092 public UriSet exclude(String pattern) { 093 return filter(invert(patterns(pattern))); 094 } 095 096 public UriSet excludePaths(String pathString) { 097 String[] paths = pathString.split(File.pathSeparator); 098 UriSet URISet = this; 099 for (String path : paths) { 100 File file = new File(path); 101 URISet = URISet.exclude(file); 102 } 103 return URISet; 104 } 105 106 public UriSet filter(Filter filter) { 107 Map<String, URI> URIs = new HashMap<String, URI>(); 108 for (Map.Entry<String, URI> entry : this.URIs.entrySet()) { 109 String URI = entry.getKey(); 110 if (filter.accept(URI)) { 111 URIs.put(URI, entry.getValue()); 112 } 113 } 114 return new UriSet(URIs); 115 } 116 117 public UriSet matching(String pattern) { 118 return filter(patterns(pattern)); 119 } 120 121 public UriSet relative(File file) { 122 String URIPath = file.toURI().toASCIIString(); 123 Map<String, URI> URIs = new HashMap<String, URI>(); 124 for (Map.Entry<String, URI> entry : this.URIs.entrySet()) { 125 String URI = entry.getKey(); 126 if (URI.startsWith(URIPath) || URI.startsWith("jar:" + URIPath)) { 127 URIs.put(URI, entry.getValue()); 128 } 129 } 130 return new UriSet(URIs); 131 } 132 133 public List<URI> getURIs() { 134 return new ArrayList<URI>(URIs.values()); 135 } 136 137 public int size() { 138 return URIs.size(); 139 } 140 141 public Iterator<URI> iterator() { 142 return getURIs().iterator(); 143 } 144 145 @Override 146 public String toString() { 147 return super.toString() + "[" + URIs.size() + "]"; 148 } 149}