com.netflix.spectator.impl.matcher.ZeroOrOneMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spectator-api Show documentation
Show all versions of spectator-api Show documentation
spectator-api developed by Netflix
/*
* Copyright 2014-2023 Netflix, Inc.
*
* 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 com.netflix.spectator.impl.matcher;
import com.netflix.spectator.impl.Preconditions;
import java.io.Serializable;
import java.util.Objects;
import java.util.SortedSet;
import java.util.function.Function;
/**
* Matcher that looks for a pattern zero or one time followed by another pattern.
*/
final class ZeroOrOneMatcher implements GreedyMatcher, Serializable {
private static final long serialVersionUID = 1L;
private final Matcher repeated;
private final Matcher next;
/** Create a new instance. */
ZeroOrOneMatcher(Matcher repeated, Matcher next) {
this.repeated = Preconditions.checkNotNull(repeated, "repeated");
this.next = Preconditions.checkNotNull(next, "next");
}
/** Return the matcher for the repeated portion. */
Matcher repeated() {
return repeated;
}
/** Return the matcher for the portion that follows. */
Matcher next() {
return next;
}
@Override
public String containedString() {
return next.containedString();
}
@Override
public SortedSet trigrams() {
return next.trigrams();
}
@Override
public int matches(String str, int start, int length) {
final int end = start + length;
int pos = repeated.matches(str, start, end - start);
if (pos >= start) {
pos = next.matches(str, pos, end - pos);
if (pos >= start)
return pos;
}
return next.matches(str, start, end - start);
}
@Override
public int minLength() {
return next.minLength();
}
@Override
public boolean isEndAnchored() {
return next.isEndAnchored();
}
@Override
public boolean alwaysMatches() {
return repeated instanceof AnyMatcher && next instanceof TrueMatcher;
}
@Override
public Matcher mergeNext(Matcher after) {
if (after instanceof TrueMatcher) {
return this;
}
Matcher m = (next instanceof TrueMatcher) ? after : SeqMatcher.create(next, after);
return new ZeroOrOneMatcher(repeated, m);
}
@Override
public Matcher rewrite(Function f) {
return f.apply(new ZeroOrOneMatcher(repeated.rewrite(f), next.rewrite(f)));
}
@Override
public Matcher rewriteEnd(Function f) {
return f.apply(new ZeroOrOneMatcher(repeated, next.rewriteEnd(f)));
}
@Override
public String toString() {
return "(?:" + repeated + ")?" + (next instanceof TrueMatcher ? "" : next.toString());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ZeroOrOneMatcher that = (ZeroOrOneMatcher) o;
return Objects.equals(repeated, that.repeated) && Objects.equals(next, that.next);
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + repeated.hashCode();
result = 31 * result + next.hashCode();
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy