com.varmateo.yawg.core.BakerMatcher Maven / Gradle / Ivy
/**************************************************************************
*
* Copyright (c) 2016-2020 Yawg project contributors.
*
**************************************************************************/
package com.varmateo.yawg.core;
import java.nio.file.Path;
import io.vavr.Tuple;
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import io.vavr.collection.Seq;
import io.vavr.control.Option;
import com.varmateo.yawg.util.GlobMatcher;
/**
*
*/
/* package private */ final class BakerMatcher {
private final Seq _bakerTypes;
/**
*
*/
private BakerMatcher(final Builder builder) {
_bakerTypes = builder._bakerTypes
.map(x -> new Entry(x._1, x._2));
}
/**
* Creates a new builder with no initializations.
*
* @return A newly created Builder
instance.
*/
public static Builder builder() {
return new Builder();
}
/**
* Creates a new builder initialized with the data from the given
* BakerMatcher
.
*
* @param data Used for initializing the builder state.
*
* @return A newly created Builder
instance.
*/
public static Builder builder(final BakerMatcher data) {
return new Builder(data);
}
/**
*
*/
public Option bakerTypeFor(final Path path) {
return _bakerTypes
.filter(entry -> entry.matcher().test(path))
.map(Entry::mapType)
.headOption();
}
/**
* Intended for logging or debugging.
*
* @return The string representation of our matchers.
*/
@Override
public String toString() {
return _bakerTypes
.sorted((o1, o2) -> o1.mapType().compareTo(o2.mapType()))
.map(e -> e.mapType() + ":" + e.matcher().toString())
.mkString(";");
}
/**
*
*/
private static final class Entry {
private final String _mapType;
private final GlobMatcher _matcher;
/**
*
*/
/* default */ Entry(
final String mapType,
final GlobMatcher matcher) {
_mapType = mapType;
_matcher = matcher;
}
/**
*
*/
public String mapType() {
return _mapType;
}
/**
*
*/
public GlobMatcher matcher() {
return _matcher;
}
}
/**
*
*/
public static final class Builder {
private Map _bakerTypes;
/**
*
*/
/* default */ Builder() {
_bakerTypes = HashMap.empty();
}
/**
*
*/
/* default */ Builder(final BakerMatcher data) {
_bakerTypes = HashMap.ofEntries(
data._bakerTypes.map(e -> Tuple.of(e.mapType(), e.matcher())));
}
/**
*
*/
public Builder addBakerType(
final String bakerType,
final GlobMatcher matcher) {
_bakerTypes = _bakerTypes.merge(
HashMap.of(bakerType, matcher),
Builder::extendGlobMatcher);
return this;
}
private static GlobMatcher extendGlobMatcher(
final GlobMatcher oldMatcher,
final GlobMatcher newMatcher) {
return GlobMatcher.builder(oldMatcher)
.addGlobMatcher(newMatcher)
.build();
}
/**
*
*/
public Builder addBakerType(
final String bakerType,
final String... fileNames) {
final GlobMatcher matcher = GlobMatcher.create(fileNames);
addBakerType(bakerType, matcher);
return this;
}
/**
*
*/
public Builder addBakerTypes(final BakerMatcher that) {
that._bakerTypes.forEach(e -> addBakerType(e.mapType(), e.matcher()));
return this;
}
/**
*
*/
public BakerMatcher build() {
return new BakerMatcher(this);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy