001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.metainf.model;
017
018import java.io.File;
019import java.util.Collections;
020import java.util.Comparator;
021import java.util.List;
022
023import org.kuali.common.util.Assert;
024import org.springframework.util.ResourceUtils;
025
026import com.google.common.base.Optional;
027import com.google.common.collect.ImmutableList;
028
029public final class MetaInfContext {
030
031        private final File outputFile;
032        private final File scanDir;
033        private final String encoding;
034        private final boolean sort;
035        private final boolean includePropertiesFile;
036        private final Optional<Comparator<MetaInfResource>> comparator;
037        private final boolean includeFileSizes;
038        private final boolean includeLineCounts;
039        private final List<String> includes;
040        private final List<String> excludes;
041        private final boolean relativePaths;
042        private final File relativeDir;
043        private final String urlPrefix;
044
045        public File getOutputFile() {
046                return outputFile;
047        }
048
049        public File getScanDir() {
050                return scanDir;
051        }
052
053        public String getEncoding() {
054                return encoding;
055        }
056
057        public boolean isSort() {
058                return sort;
059        }
060
061        public boolean isIncludePropertiesFile() {
062                return includePropertiesFile;
063        }
064
065        public boolean isIncludeFileSizes() {
066                return includeFileSizes;
067        }
068
069        public boolean isIncludeLineCounts() {
070                return includeLineCounts;
071        }
072
073        public List<String> getIncludes() {
074                return includes;
075        }
076
077        public List<String> getExcludes() {
078                return excludes;
079        }
080
081        public boolean isRelativePaths() {
082                return relativePaths;
083        }
084
085        public File getRelativeDir() {
086                return relativeDir;
087        }
088
089        public String getUrlPrefix() {
090                return urlPrefix;
091        }
092
093        public Optional<Comparator<MetaInfResource>> getComparator() {
094                return comparator;
095        }
096
097        public static Builder builder(File outputFile, String encoding, File scanDir) {
098                return new Builder(outputFile, encoding, scanDir);
099        }
100
101        public static class Builder {
102
103                // Required
104                private final File outputFile;
105                private final File scanDir;
106                private final String encoding; // Encoding is required both for reading line counts and for creating the .resources file
107
108                // Optional
109                private boolean sort = true;
110                private boolean includePropertiesFile = false;
111                private boolean includeFileSizes = true;
112                private boolean includeLineCounts = false; // Make them explicitly set this to true, since it can be quite expensive
113                private List<String> includes = Collections.emptyList();
114                private List<String> excludes = Collections.emptyList();
115                private boolean relativePaths = true;
116                private String urlPrefix = ResourceUtils.CLASSPATH_URL_PREFIX;
117                private Optional<Comparator<MetaInfResource>> comparator = Optional.absent();
118
119                // Defaults to scanDir
120                private File relativeDir;
121
122                public Builder(File outputFile, String encoding, File scanDir) {
123                        this.outputFile = outputFile;
124                        this.encoding = encoding;
125                        this.scanDir = scanDir;
126                        this.relativeDir = scanDir; // Paths are generated relative to the scanDir by default
127                }
128
129                public Builder comparator(Comparator<MetaInfResource> comparator) {
130                        this.comparator = Optional.of(comparator);
131                        return this;
132                }
133
134                public Builder sort(boolean sort) {
135                        this.sort = sort;
136                        return this;
137                }
138
139                public Builder includes(List<String> includes) {
140                        this.includes = includes;
141                        return this;
142                }
143
144                public Builder excludes(List<String> excludes) {
145                        this.excludes = excludes;
146                        return this;
147                }
148
149                public Builder relativePaths(boolean relativePaths) {
150                        this.relativePaths = relativePaths;
151                        return this;
152                }
153
154                public Builder includePropertiesFile(boolean includePropertiesFile) {
155                        this.includePropertiesFile = includePropertiesFile;
156                        return this;
157                }
158
159                public Builder includeFileSizes(boolean includeFileSizes) {
160                        this.includeFileSizes = includeFileSizes;
161                        return this;
162                }
163
164                public Builder includeLineCounts(boolean includeLineCounts) {
165                        this.includeLineCounts = includeLineCounts;
166                        return this;
167                }
168
169                public MetaInfContext build() {
170                        Assert.noNulls(outputFile, scanDir, includes, excludes, relativeDir, comparator);
171                        Assert.noBlanks(encoding, urlPrefix);
172                        this.includes = ImmutableList.copyOf(includes);
173                        this.excludes = ImmutableList.copyOf(excludes);
174                        return new MetaInfContext(this);
175                }
176
177        }
178
179        private MetaInfContext(Builder builder) {
180                this.sort = builder.sort;
181                this.encoding = builder.encoding;
182                this.urlPrefix = builder.urlPrefix;
183                this.includeFileSizes = builder.includeFileSizes;
184                this.includeLineCounts = builder.includeLineCounts;
185                this.includePropertiesFile = builder.includePropertiesFile;
186                this.includes = builder.includes;
187                this.excludes = builder.excludes;
188                this.scanDir = builder.scanDir;
189                this.relativeDir = builder.relativeDir;
190                this.outputFile = builder.outputFile;
191                this.relativePaths = builder.relativePaths;
192                this.comparator = builder.comparator;
193        }
194
195}