guru.nidi.codeassert.model.Scope Maven / Gradle / Ivy
/*
* Copyright © 2015 Stefan Niederhauser ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package guru.nidi.codeassert.model;
import guru.nidi.codeassert.AnalyzerException;
import guru.nidi.codeassert.config.LocationMatcher;
import java.lang.reflect.Constructor;
import java.util.*;
public abstract class Scope> implements Iterable {
public static final Packages PACKAGES = new Packages(null);
public static final Classes CLASSES = new Classes(null);
protected final Model model;
protected Scope(Model model) {
this.model = model;
}
public static Scope packages(final Model model) {
return new Packages(model);
}
public static Scope classes(final Model model) {
return new Classes(model);
}
public Scope in(Model model) {
try {
final Constructor extends Scope> c = getClass().getDeclaredConstructor(Model.class);
return c.newInstance(model);
} catch (ReflectiveOperationException e) {
throw new AnalyzerException("Could not create new Scope", e);
}
}
public List matchingElements(LocationMatcher matcher) {
final List res = new ArrayList<>();
for (final T elem : this) {
if (elem.isMatchedBy(matcher)) {
res.add(elem);
}
}
return res;
}
public static class Packages extends Scope {
protected Packages(Model model) {
super(model);
}
@Override
public Iterator iterator() {
return model.packages.values().iterator();
}
}
public static class Classes extends Scope {
public Classes(Model model) {
super(model);
}
@Override
public Iterator iterator() {
return model.classes.values().iterator();
}
}
}