001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.weaver.utils;
020
021import java.io.File;
022import java.net.MalformedURLException;
023import java.net.URL;
024import java.util.ArrayList;
025import java.util.Iterator;
026
027/**
028 * {@link URL} Array utilities.
029 */
030public final class URLArray {
031    private URLArray() {
032    }
033
034    /**
035     * Convert an {@link Iterable} of filesystem paths.
036     * @param files to convert
037     * @return URL[]
038     */
039    public static URL[] fromPaths(final Iterable<String> files) {
040        return fromFiles(new Iterable<File>() {
041
042            @Override
043            public Iterator<File> iterator() {
044                final Iterator<String> path = files.iterator();
045                return new Iterator<File>() {
046
047                    @Override
048                    public boolean hasNext() {
049                        return path.hasNext();
050                    }
051
052                    @Override
053                    public File next() {
054                        final String element = path.next();
055                        return element == null ? null : new File(element);
056                    }
057
058                    @Override
059                    public void remove() {
060                        throw new UnsupportedOperationException();
061                    }
062                };
063            }
064        });
065    }
066
067    /**
068     * Convert an {@link Iterable} of {@link File}s.
069     * @param files to convert
070     * @return URL[]
071     */
072    public static URL[] fromFiles(final Iterable<File> files) {
073        final ArrayList<URL> result = new ArrayList<URL>();
074        for (final File file : files) {
075            if (file == null) {
076                result.add(null);
077                continue;
078            }
079            try {
080                result.add(file.toURI().toURL());
081            } catch (final MalformedURLException e) {
082                // this shouldn't happen
083                throw new RuntimeException(e);
084            }
085        }
086        return result.toArray(new URL[result.size()]);
087    }
088}