aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/jarjar/jarjar-core/src/main/java/com/tonicsystems/jarjar/classpath/ClassPath.java
blob: c12929b3d2e609c9e069bf0a62a365c376a68795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tonicsystems.jarjar.classpath;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import javax.annotation.Nonnull;

/**
 *
 * @author shevek
 */
public class ClassPath implements Iterable<ClassPathArchive> {

    private final File root;
    private final Iterable<? extends File> entries;

    public ClassPath(@Nonnull File root, @Nonnull Iterable<? extends File> entries) {
        this.root = root;
        this.entries = entries;
    }

    public ClassPath(@Nonnull File root, @Nonnull File[] entries) {
        this(root, Arrays.asList(entries));
    }

    @Nonnull
    public File getRoot() {
        return root;
    }

    @Override
    public Iterator<ClassPathArchive> iterator() {
        return new PathIterator();
    }

    private class PathIterator implements Iterator<ClassPathArchive> {

        private final Iterator<? extends File> entryIterator;

        public PathIterator() {
            this.entryIterator = entries.iterator();
        }

        @Override
        public boolean hasNext() {
            return entryIterator.hasNext();
        }

        @Override
        public ClassPathArchive next() {
            File entryFile = entryIterator.next();
            if (!entryFile.isAbsolute())
                entryFile = new File(root, entryFile.getPath());
            if (entryFile.isDirectory())
                return new ClassPathArchive.DirectoryArchive(entryFile);
            else
                return new ClassPathArchive.ZipArchive(entryFile);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }
}