Remove RichCharSeq and impl

This commit is contained in:
Clément Fournier
2019-07-22 19:14:36 +02:00
parent 3f14a88282
commit dabe123f4e
3 changed files with 0 additions and 243 deletions

View File

@ -1,137 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ast.impl;
import java.util.regex.Pattern;
import org.apache.commons.lang3.CharSequenceUtils;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.util.StringUtil;
/**
* A {@link CharSequence} with convenience methods to make the interface
* similar to a {@link String}. This is incentive to not convert the
* sequence to a string when equivalent methods exist which would not
* force the creation of a {@link String}.
*
* <p>Also, contrary to {@link CharSequence}, the contract of {@link Object#equals(Object)}
* is refined to mean that any {@link RichCharSequence} must be equatable
* to another {@link RichCharSequence} implementation, making this type
* suitable for use as keys of a map for example.
*
*/
public interface RichCharSequence extends CharSequence {
@Override
RichCharSequence subSequence(int start, int end);
/** @see CharSequenceUtils#subSequence(CharSequence, int) */
default RichCharSequence subSequence(int start) {
return subSequence(start, length());
}
/** @see StringUtils#indexOf(CharSequence, int) */
default int indexOf(int searchChar) {
return StringUtils.indexOf(this, searchChar);
}
/** @see StringUtils#indexOf(CharSequence, int, int) */
default int indexOf(int searchChar, int startPos) {
return StringUtils.indexOf(this, searchChar, startPos);
}
/** @see StringUtils#lastIndexOf(CharSequence, int) */
default int lastIndexOf(int searchChar) {
return StringUtils.lastIndexOf(this, searchChar);
}
/** @see StringUtils#lastIndexOf(CharSequence, int, int) */
default int lastIndexOf(int searchChar, int startPos) {
return StringUtils.lastIndexOf(this, searchChar, startPos);
}
default boolean isEmpty() {
return length() == 0;
}
/** @see StringUtils#isWhitespace(CharSequence) */
default boolean isWhitespace() {
return StringUtils.isWhitespace(toString());
}
/** @see StringUtils#contains(CharSequence, CharSequence) */
default boolean contains(CharSequence sequence) {
return StringUtils.contains(this, sequence);
}
/** @see StringUtils#contains(CharSequence, int) */
default boolean contains(int searchChar) {
return StringUtils.contains(this, searchChar);
}
/** @see StringUtils#containsIgnoreCase(CharSequence, CharSequence) */
default boolean containsIgnoreCase(CharSequence sequence) {
return StringUtils.containsIgnoreCase(this, sequence);
}
/** @see StringUtils#endsWith(CharSequence, CharSequence) */
default boolean endsWith(CharSequence suffix) {
return StringUtils.endsWith(this, suffix);
}
/** @see StringUtils#startsWith(CharSequence, CharSequence) */
default boolean startsWith(CharSequence prefix) {
return StringUtils.startsWith(this, prefix);
}
/** @see Pattern#matches(String, CharSequence) */
default boolean matches(String regex) {
return Pattern.matches(regex, this);
}
/**
* Compares this sequence to the argument, following the contract
* of {@link StringUtils#equals(CharSequence, CharSequence)}.
*
* <p>Note that {@link Object#equals(Object)} only considers {@link RichCharSequence}
* of the same type, for symmetry, which for example would fail with
* {@link String}.
*/
default boolean equalsSeq(CharSequence other) {
return StringUtils.equals(this, other);
}
/** @see StringUtils#equalsIgnoreCase(CharSequence, CharSequence)*/
default boolean equalsIgnoreCase(CharSequence other) {
return StringUtils.equalsIgnoreCase(this, other);
}
/** Returns the column number at the given position. */
default int getColumnNumberAt(int posInclusive) {
return StringUtil.columnNumberAt(this, posInclusive);
}
/** Returns the line number at the given position. */
default int getLineNumberAt(int posInclusive) {
return StringUtil.lineNumberAt(this, posInclusive);
}
}

View File

@ -1,105 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ast.impl;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A {@link CharSequence} implemented with the (value, offset, count) representation,
* meaning it shares the underlying char array to allocate sub-sequences.
* This is advantageous to represent a tokenized file, as we share the
* char array for the whole file and only change the bounds for each token.
*
* <p>{@link Object#toString()} may be called to get a reference to a string
* that doesn't hold a strong reference to the underlying char array,
* hence not preventing its garbage collection.
*
* @author Clément Fournier
*/
public final class SharingCharSeq implements RichCharSequence {
private static final SharingCharSeq EMPTY = new SharingCharSeq(new char[0], 0, 0);
/** If this is null, then {@link #myStr} is never null. */
private final char[] value;
private final int offset;
private final int count;
@Nullable
private String myStr;
public SharingCharSeq(@NonNull String str) {
this.myStr = Objects.requireNonNull(str, "String value cannot be null!");
this.offset = 0;
this.count = str.length();
this.value = str.toCharArray();
}
// this is for a subsequence
private SharingCharSeq(char[] value, int offset, int count) {
this.value = value;
this.offset = offset;
this.count = count;
this.myStr = null;
}
@Override
public int length() {
return count;
}
@Override
public char charAt(int index) {
if (index < 0 || index >= count) {
throw new IndexOutOfBoundsException("Index out of bounds: " + index + " not in [0," + count + "[");
}
return value[offset + index];
}
@Override
public RichCharSequence subSequence(int start, int end) {
if (start < 0 || end > count || start > end) {
throw new IndexOutOfBoundsException("Invalid range: [" + start + "," + end + "[ not in [0," + count + "[");
}
int len = end - start;
return len == 0 ? EMPTY : new SharingCharSeq(value, offset + start, len);
}
@NonNull
@Override
public String toString() {
String str = myStr;
if (str == null) {
str = new String(value, offset, count);
myStr = str;
}
return str;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RichCharSequence)) {
return false;
}
return StringUtils.equals(this, (RichCharSequence) o);
}
}

View File

@ -14,7 +14,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.ast.impl.RichCharSequence;
import net.sourceforge.pmd.lang.ast.impl.TokenDocument;
import net.sourceforge.pmd.lang.java.typeresolution.ClassTypeResolver;