From dabe123f4e8ecac1afa89b8babe20de1e4d4b887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 22 Jul 2019 19:14:36 +0200 Subject: [PATCH] Remove RichCharSeq and impl --- .../pmd/lang/ast/impl/RichCharSequence.java | 137 ------------------ .../pmd/lang/ast/impl/SharingCharSeq.java | 105 -------------- .../pmd/lang/java/ast/ASTCompilationUnit.java | 1 - 3 files changed, 243 deletions(-) delete mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/RichCharSequence.java delete mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/RichCharSequence.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/RichCharSequence.java deleted file mode 100644 index 7ae53c88b5..0000000000 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/RichCharSequence.java +++ /dev/null @@ -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}. - * - *

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)}. - * - *

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); - } - - -} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java deleted file mode 100644 index 1eccae7ded..0000000000 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java +++ /dev/null @@ -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. - * - *

{@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); - } -} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java index 568b9cbb63..078e35ab3b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java @@ -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;