Skip to content

Commit

Permalink
draft proposal for factory methods , , , class
Browse files Browse the repository at this point in the history
  • Loading branch information
noodlze committed Feb 17, 2023
1 parent bda3bec commit 3e4eb71
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static com.linecorp.centraldogma.common.DefaultPathPattern.allPattern;
import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;

Expand All @@ -36,6 +39,13 @@
*/
public interface PathPattern {

/**
*Returns a newly created {@link PathPatternBuilder}.
*/
static PathPatternBuilder builder() {
return new PathPatternBuilder();
}

/**
* Returns the path pattern that represents all files.
*/
Expand All @@ -50,6 +60,16 @@ static PathPattern of(String... patterns) {
return of(ImmutableSet.copyOf(requireNonNull(patterns, "patterns")));
}

/**
* Creates a path pattern with the {@code pathPatterns}.
*/
static PathPattern of(PathPattern... pathPatterns) {
requireNonNull(pathPatterns, "pathPatterns");
return of(ImmutableSet.copyOf(Arrays.stream(pathPatterns)
.map(PathPattern::patternString)
.collect(Collectors.toList())));
}

/**
* Creates a path pattern with the {@code patterns}.
*/
Expand All @@ -62,6 +82,30 @@ static PathPattern of(Iterable<String> patterns) {
return new DefaultPathPattern(ImmutableSet.copyOf(patterns));
}

/**
* Returns the path pattern for matching file(s) ending in {@code pattern}.
*/
static PathPattern endsWith(String pattern) {
requireNonNull(pattern, "pattern");
final int extSeparatorPos = pattern.lastIndexOf('.');
if (extSeparatorPos > 0) { // filename + extension
return new DefaultPathPattern(ImmutableSet.of(pattern));
} else if (extSeparatorPos == 0) { // extension with separator
return new DefaultPathPattern(ImmutableSet.of("/**/*" + pattern));
} else { // extension without separator
return new DefaultPathPattern(ImmutableSet.of("/**/*." + pattern));
}
}

/**
* Returns the path pattern for file(s) under {@code dirPattern}.
*/
static PathPattern under(String dirPattern) {
requireNonNull(dirPattern, "dirPattern");
return dirPattern.endsWith("/") ? new DefaultPathPattern(ImmutableSet.of(dirPattern + "**"))
: new DefaultPathPattern(ImmutableSet.of(dirPattern + "/**"));
}

/**
* Returns the path pattern that concatenates the {@code patterns} using ','.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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 com.linecorp.centraldogma.common;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

import java.util.Collections;
import java.util.List;

import com.google.common.collect.ImmutableSet;

/**
* Builds a new {@link PathPattern}.
*
* <h2>Example</h2>
* <pre>{@code
* final PathPattern factory =
* PathPattern.builder()
* .under("/foo/bar")
* .endsWith("json")
* .build();
* }</pre>
*/
public final class PathPatternBuilder {

private List<String> patternsList = Collections.emptyList();

/**
* Add {@link PathPattern#endsWith(String)} patternString.
*/
public PathPatternBuilder endsWith(String pattern) {
patternsList.add(PathPattern.endsWith(pattern).patternString());
return this;
}

/**
* Add {@link PathPattern#under(String)} patternString.
*/
public PathPatternBuilder under(String dirPattern) {
patternsList.add(PathPattern.under(dirPattern).patternString());
return this;
}

/**
* Returns combined under patternString and endsWith patternString.
*/
private static String combineUnderAndEndsWith(String under, String endsWith) {
checkArgument(!under.endsWith("/**"), "under patternString should end with \"/**\"");
checkArgument(!endsWith.startsWith("/**/"), "endsWith should start with \"/**/\"");
return under + endsWith.substring(2);
}

/**
* Combine 2 patternStrings in {@code patternsList} into one path pattern.
*/
public PathPattern build() {
checkArgument(patternsList.size() >= 2, "Need 2 or more patternStrings to build in PathPatternBuilder");
final String startsWithPattern = patternsList.stream()
.filter(pattern -> pattern.startsWith("/**/"))
.findAny()
.get();
final String endsWithPattern = patternsList.stream()
.filter(pattern -> pattern.endsWith("/**"))
.findAny()
.get();
return new DefaultPathPattern(ImmutableSet.of(
combineUnderAndEndsWith(requireNonNull(startsWithPattern), requireNonNull(endsWithPattern))));
}
}

0 comments on commit 3e4eb71

Please sign in to comment.