From fd6d67e69ff9b93e8055bd2af39b490cb14957bb Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 8 Nov 2016 13:45:13 +0300 Subject: [PATCH] cleanup --- .../util/text/CharArrayExternalizable.java | 4 +- .../com/intellij/util/text/CharArrayUtil.java | 73 +++++-------------- .../util/text/CharSequenceBackedByArray.java | 6 +- .../util/text/ImmutableCharSequence.java | 4 + .../com/intellij/util/text/TextRangeUtil.java | 6 +- .../com/intellij/util/text/TextRanges.java | 6 +- .../util/text/UnsyncCharArrayReader.java | 3 +- .../vcs/impl/ProjectLevelVcsManagerImpl.java | 56 +++++--------- 8 files changed, 56 insertions(+), 102 deletions(-) diff --git a/platform/util/src/com/intellij/util/text/CharArrayExternalizable.java b/platform/util/src/com/intellij/util/text/CharArrayExternalizable.java index 49a9a897e10d..41a9d552dce3 100644 --- a/platform/util/src/com/intellij/util/text/CharArrayExternalizable.java +++ b/platform/util/src/com/intellij/util/text/CharArrayExternalizable.java @@ -19,8 +19,8 @@ import org.jetbrains.annotations.NotNull; /** * A char sequence that supports fast copying of its full or partial contents to a char array. May be useful for performance optimizations - * @see com.intellij.util.text.CharSequenceBackedByArray - * @see com.intellij.util.text.CharArrayUtil#getChars(CharSequence, char[], int) + * @see CharSequenceBackedByArray + * @see CharArrayUtil#getChars(CharSequence, char[], int) */ public interface CharArrayExternalizable extends CharSequence { diff --git a/platform/util/src/com/intellij/util/text/CharArrayUtil.java b/platform/util/src/com/intellij/util/text/CharArrayUtil.java index 9ee6d632aeb8..9238d850b8cc 100644 --- a/platform/util/src/com/intellij/util/text/CharArrayUtil.java +++ b/platform/util/src/com/intellij/util/text/CharArrayUtil.java @@ -19,9 +19,6 @@ import com.intellij.openapi.util.TextRange; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.Reader; import java.nio.CharBuffer; import java.util.ArrayList; @@ -152,7 +149,7 @@ public class CharArrayUtil { } /** - * Tries to find an offset from the [startOffset; endOffset) interval such that a char from the given buffer is + * Tries to find an offset from the {@code [startOffset; endOffset)} interval such that a char from the given buffer is * not contained at the given 'chars' string. *

* Example: @@ -162,9 +159,9 @@ public class CharArrayUtil { * @param startOffset start offset to use within the given buffer (inclusive) * @param endOffset end offset to use within the given buffer (exclusive) * @param chars pass-through symbols - * @return offset from the [startOffset; endOffset) which points to a symbol at the given buffer such + * @return offset from the {@code [startOffset; endOffset)} which points to a symbol at the given buffer such * as that that symbol is not contained at the given 'chars'; - * endOffset otherwise + * {@code endOffset} otherwise */ public static int shiftForward(@NotNull CharSequence buffer, final int startOffset, final int endOffset, @NotNull String chars) { for (int offset = startOffset, limit = Math.min(endOffset, buffer.length()); offset < limit; offset++) { @@ -254,24 +251,24 @@ public class CharArrayUtil { *

*

- * Example: suppose that this method is called with buffer that holds 'test data' symbols, offset that points - * to the last symbols and 'sf' as a chars to exclude. Offset that points to 's' symbol + * Example: suppose that this method is called with buffer that holds {@code 'test data'} symbols, offset that points + * to the last symbols and {@code 'sf'} as a chars to exclude. Offset that points to {@code 's'} symbol * is returned then, i.e. all symbols of the given buffer that are located after it and not after given offset - * ('t data') are guaranteed to not contain given chars ('sf'). + * ({@code 't data'}) are guaranteed to not contain given chars ({@code 'sf'}). * * @param buffer symbols buffer to check * @param offset initial symbols buffer offset to use * @param chars chars to exclude - * @return offset of the given buffer that guarantees that all symbols at (returned offset; given offset] - * interval of the given buffer differ from symbols of given 'chars' arguments; + * @return offset of the given buffer that guarantees that all symbols at {@code (returned offset; given offset]} + * interval of the given buffer differ from symbols of given {@code 'chars'} arguments; * given offset is returned if it is outside of given buffer bounds; - * '-1' is returned if all document symbols that precede given offset differ from symbols - * of the given 'chars to exclude' + * {@code '-1'} is returned if all document symbols that precede given offset differ from symbols + * of the given {@code 'chars to exclude'} */ public static int shiftBackwardUntil(@NotNull CharSequence buffer, int offset, @NotNull String chars) { if (offset >= buffer.length()) return offset; @@ -366,14 +363,14 @@ public class CharArrayUtil { * @param pattern target characters sequence to find at the given buffer * @param fromIndex start index (inclusive). Zero is used if given index is negative * @param toIndex end index (exclusive) - * @return index of the given pattern at the given buffer if the match is found; -1 otherwise + * @return index of the given pattern at the given buffer if the match is found; {@code -1} otherwise */ public static int indexOf(@NotNull CharSequence buffer, @NotNull CharSequence pattern, int fromIndex, final int toIndex) { final int patternLength = pattern.length(); - int limit = toIndex - patternLength + 1; if (fromIndex < 0) { fromIndex = 0; } + int limit = toIndex - patternLength + 1; SearchLoop: for (int i = fromIndex; i < limit; i++) { for (int j = 0; j < patternLength; j++) { @@ -385,15 +382,15 @@ public class CharArrayUtil { } /** - * Tries to find index that points to the first location of the given symbol at the given char array at range [from; to). + * Tries to find index that points to the first location of the given symbol at the given char array at range {@code [from; to)}. * * @param buffer target symbols holder to check * @param symbol target symbol which offset should be found * @param fromIndex start index to search (inclusive) * @param toIndex end index to search (exclusive) * @return index that points to the first location of the given symbol at the given char array at range - * [from; to) if target symbol is found; - * -1 otherwise + * {@code [from; to)} if target symbol is found; + * {@code -1} otherwise */ public static int indexOf(@NotNull char[] buffer, final char symbol, int fromIndex, final int toIndex) { if (fromIndex < 0) { @@ -408,15 +405,15 @@ public class CharArrayUtil { } /** - * Tries to find index that points to the last location of the given symbol at the given char array at range [from; to). + * Tries to find index that points to the last location of the given symbol at the given char array at range {@code [from; to)}. * * @param buffer target symbols holder to check * @param symbol target symbol which offset should be found * @param fromIndex start index to search (inclusive) * @param toIndex end index to search (exclusive) * @return index that points to the last location of the given symbol at the given char array at range - * [from; to) if target symbol is found; - * -1 otherwise + * {@code [from; to)} if target symbol is found; + * {@code -1} otherwise */ public static int lastIndexOf(@NotNull char[] buffer, final char symbol, int fromIndex, final int toIndex) { if (fromIndex < 0) { @@ -462,24 +459,6 @@ public class CharArrayUtil { return -1; } - @NotNull - public static byte[] toByteArray(@NotNull char[] chars) throws IOException { - return toByteArray(chars, chars.length); - } - - @NotNull - public static byte[] toByteArray(@NotNull char[] chars, int size) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - OutputStreamWriter writer = new OutputStreamWriter(out); - try { - writer.write(chars, 0, size); - } - finally { - writer.close(); - } - return out.toByteArray(); - } - public static boolean containsOnlyWhiteSpaces(@Nullable CharSequence chars) { if (chars == null) return true; for (int i = 0; i < chars.length(); i++) { @@ -490,18 +469,6 @@ public class CharArrayUtil { return true; } - //Commented in order to apply to green code policy as the method is unused. - // - //public static boolean subArraysEqual(char[] ca1, int startOffset1, int endOffset1,char[] ca2, int startOffset2, int endOffset2) { - // if (endOffset1 - startOffset1 != endOffset2 - startOffset2) return false; - // for (int i = startOffset1; i < endOffset1; i++) { - // char c1 = ca1[i]; - // char c2 = ca2[i - startOffset1 + startOffset2]; - // if (c1 != c2) return false; - // } - // return true; - //} - @NotNull public static TextRange[] getIndents(@NotNull CharSequence charsSequence, int shift) { List result = new ArrayList(); @@ -553,7 +520,7 @@ public class CharArrayUtil { * @param text text to check * @param start start offset within the given text to check (inclusive) * @param end end offset within the given text to check (exclusive) - * @return true if target region of the given text contains white space symbols only; false otherwise + * @return {@code true} if target region of the given text contains white space symbols only; {@code false} otherwise */ public static boolean isEmptyOrSpaces(@NotNull CharSequence text, int start, int end) { for (int i = start; i < end; i++) { diff --git a/platform/util/src/com/intellij/util/text/CharSequenceBackedByArray.java b/platform/util/src/com/intellij/util/text/CharSequenceBackedByArray.java index 4a88c1e4271e..312df12c87e0 100644 --- a/platform/util/src/com/intellij/util/text/CharSequenceBackedByArray.java +++ b/platform/util/src/com/intellij/util/text/CharSequenceBackedByArray.java @@ -22,9 +22,9 @@ import org.jetbrains.annotations.NotNull; * A char sequence based on a char array. May be used for performance optimizations. * * @author Maxim.Mossienko - * @see com.intellij.util.text.CharArrayExternalizable - * @see com.intellij.util.text.CharArrayUtil#getChars(CharSequence, char[], int) - * @see com.intellij.util.text.CharArrayUtil#fromSequenceWithoutCopying(CharSequence) + * @see CharArrayExternalizable + * @see CharArrayUtil#getChars(CharSequence, char[], int) + * @see CharArrayUtil#fromSequenceWithoutCopying(CharSequence) */ public interface CharSequenceBackedByArray extends CharSequence { // NOT guaranteed to return the array of the length of the original charSequence.length() - may be more for performance reasons. diff --git a/platform/util/src/com/intellij/util/text/ImmutableCharSequence.java b/platform/util/src/com/intellij/util/text/ImmutableCharSequence.java index 92e29972463c..1e7b3fd929fd 100644 --- a/platform/util/src/com/intellij/util/text/ImmutableCharSequence.java +++ b/platform/util/src/com/intellij/util/text/ImmutableCharSequence.java @@ -35,4 +35,8 @@ public abstract class ImmutableCharSequence implements CharSequence { public abstract ImmutableCharSequence delete(int start, int end); public abstract ImmutableCharSequence subtext(int start, int end); + + @NotNull + @Override + public abstract String toString(); } diff --git a/platform/util/src/com/intellij/util/text/TextRangeUtil.java b/platform/util/src/com/intellij/util/text/TextRangeUtil.java index 2ac3890a1fee..2bcfbc93ed25 100644 --- a/platform/util/src/com/intellij/util/text/TextRangeUtil.java +++ b/platform/util/src/com/intellij/util/text/TextRangeUtil.java @@ -25,7 +25,7 @@ import java.util.*; */ public class TextRangeUtil { - private final static Comparator RANGE_COMPARATOR = new Comparator() { + private static final Comparator RANGE_COMPARATOR = new Comparator() { @Override public int compare(TextRange range1, TextRange range2) { int startOffsetDiff = range1.getStartOffset() - range2.getStartOffset(); @@ -47,11 +47,11 @@ public class TextRangeUtil { */ public static Iterable excludeRanges(@NotNull TextRange original, @NotNull List excludedRanges) { if (!excludedRanges.isEmpty()) { - List enabledRanges = new ArrayList(); if (excludedRanges.size() > 1) { Collections.sort(excludedRanges, RANGE_COMPARATOR); } int enabledRangeStart = original.getStartOffset(); + List enabledRanges = new ArrayList(); for (TextRange excludedRange : excludedRanges) { if (excludedRange.getEndOffset() < enabledRangeStart) continue; int excludedRangeStart = excludedRange.getStartOffset(); @@ -66,6 +66,6 @@ public class TextRangeUtil { } return enabledRanges; } - return Arrays.asList(original); + return Collections.singletonList(original); } } diff --git a/platform/util/src/com/intellij/util/text/TextRanges.java b/platform/util/src/com/intellij/util/text/TextRanges.java index f9268cce0d67..8c3ddc5b55a3 100644 --- a/platform/util/src/com/intellij/util/text/TextRanges.java +++ b/platform/util/src/com/intellij/util/text/TextRanges.java @@ -24,13 +24,13 @@ import org.jetbrains.annotations.Nullable; import java.util.*; public class TextRanges implements Iterable { - private final static Comparator START_COMP = new Comparator() { + private static final Comparator START_COMP = new Comparator() { @Override public int compare(TextRange o1, TextRange o2) { return Comparing.compare(o1.getStartOffset(), o2.getStartOffset()); } }; - private final static Comparator END_COMP = new Comparator() { + private static final Comparator END_COMP = new Comparator() { @Override public int compare(TextRange o1, TextRange o2) { return Comparing.compare(o1.getEndOffset(), o2.getEndOffset()); @@ -67,7 +67,7 @@ public class TextRanges implements Iterable { public Iterator revIterator() { return new Iterator() { - private ListIterator it = myRanges.listIterator(myRanges.size()); + private final ListIterator it = myRanges.listIterator(myRanges.size()); @Override public boolean hasNext() { diff --git a/platform/util/src/com/intellij/util/text/UnsyncCharArrayReader.java b/platform/util/src/com/intellij/util/text/UnsyncCharArrayReader.java index e9c65ecec2ab..67d0c1d5855f 100644 --- a/platform/util/src/com/intellij/util/text/UnsyncCharArrayReader.java +++ b/platform/util/src/com/intellij/util/text/UnsyncCharArrayReader.java @@ -40,7 +40,8 @@ public class UnsyncCharArrayReader extends Reader { public int read(@NotNull char[] cbuf, int off, int len) { if (off < 0 || off > cbuf.length || len < 0 || off + len > cbuf.length || off + len < 0) { throw new IndexOutOfBoundsException(); - } else if (len == 0) { + } + if (len == 0) { return 0; } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/impl/ProjectLevelVcsManagerImpl.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/impl/ProjectLevelVcsManagerImpl.java index fe00e96e063b..f8b4a9be2996 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/impl/ProjectLevelVcsManagerImpl.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/impl/ProjectLevelVcsManagerImpl.java @@ -66,7 +66,6 @@ import com.intellij.ui.content.ContentManager; import com.intellij.util.ContentUtilEx; import com.intellij.util.Processor; import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.Convertor; import com.intellij.util.messages.MessageBusConnection; import com.intellij.util.text.DateFormatUtil; import org.jdom.Attribute; @@ -83,7 +82,7 @@ import java.util.List; @State(name = "ProjectLevelVcsManager", storages = @Storage(StoragePathMacros.WORKSPACE_FILE)) public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx implements ProjectComponent, PersistentStateComponent { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl"); - @NonNls public static final String SETTINGS_EDITED_MANUALLY = "settingsEditedManually"; + @NonNls private static final String SETTINGS_EDITED_MANUALLY = "settingsEditedManually"; private final ProjectLevelVcsManagerSerialization mySerialization; private final OptionsAndConfirmations myOptionsAndConfirmations; @@ -94,7 +93,7 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme private ContentManager myContentManager; private ConsoleView myConsole; - private Disposable myConsoleDisposer = new Disposable() { + private final Disposable myConsoleDisposer = new Disposable() { @Override public void dispose() { if (myConsole != null) { @@ -113,11 +112,11 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme @NonNls private static final String ELEMENT_ROOT_SETTINGS = "rootSettings"; @NonNls private static final String ATTRIBUTE_CLASS = "class"; - private boolean myMappingsLoaded = false; - private boolean myHaveLegacyVcsConfiguration = false; + private boolean myMappingsLoaded; + private boolean myHaveLegacyVcsConfiguration; private final DefaultVcsRootPolicy myDefaultVcsRootPolicy; - private volatile int myBackgroundOperationCounter = 0; + private volatile int myBackgroundOperationCounter; private final Set myBackgroundRunningTasks = ContainerUtil.newHashSet(); @@ -165,34 +164,21 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme myVcsHistoryCache = new VcsHistoryCache(); myContentRevisionCache = new ContentRevisionCache(); myVcsFileListenerContextHelper = vcsFileListenerContextHelper; - VcsListener vcsListener = new VcsListener() { - @Override - public void directoryMappingChanged() { - myVcsHistoryCache.clear(); - myVcsFileListenerContextHelper.possiblySwitchActivation(hasActiveVcss()); - } + VcsListener vcsListener = () -> { + myVcsHistoryCache.clear(); + myVcsFileListenerContextHelper.possiblySwitchActivation(hasActiveVcss()); }; myExcludedIndex = excludedFileIndex; MessageBusConnection connection = myProject.getMessageBus().connect(); connection.subscribe(ProjectLevelVcsManager.VCS_CONFIGURATION_CHANGED, vcsListener); connection.subscribe(ProjectLevelVcsManager.VCS_CONFIGURATION_CHANGED_IN_PLUGIN, vcsListener); - connection.subscribe(UpdatedFilesListener.UPDATED_FILES, new UpdatedFilesListener() { - @Override - public void consume(Set strings) { - myContentRevisionCache.clearCurrent(strings); - } - }); + connection.subscribe(UpdatedFilesListener.UPDATED_FILES, myContentRevisionCache::clearCurrent); myAnnotationLocalChangesListener = new VcsAnnotationLocalChangesListenerImpl(myProject, this); } @Override public void initComponent() { - myOptionsAndConfirmations.init(new Convertor() { - @Override - public VcsShowConfirmationOption.Value convert(String o) { - return mySerialization.getInitOptionValue(o); - } - }); + myOptionsAndConfirmations.init(mySerialization::getInitOptionValue); } public void registerVcs(AbstractVcs vcs) { @@ -534,7 +520,7 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme return null; } - public boolean hasExplicitMapping(final VirtualFile vFile) { + private boolean hasExplicitMapping(final VirtualFile vFile) { final VcsDirectoryMapping mapping = myMappings.getMappingFor(vFile); return mapping != null && !mapping.isDefaultMapping(); } @@ -707,7 +693,7 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme myProject.getMessageBus().syncPublisher(VCS_CONFIGURATION_CHANGED).directoryMappingChanged(); } - public void readDirectoryMappings(final Element element) { + void readDirectoryMappings(final Element element) { myMappings.clear(); final List mappingsList = new ArrayList<>(); @@ -746,7 +732,7 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme myMappings.setDirectoryMappings(mappingsList); } - public void writeDirectoryMappings(@NotNull Element element) { + void writeDirectoryMappings(@NotNull Element element) { if (myProject.isDefault()) { element.setAttribute(ATTRIBUTE_DEFAULT_PROJECT, Boolean.TRUE.toString()); } @@ -853,15 +839,11 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme @Override public boolean isFileInContent(@Nullable final VirtualFile vf) { - return ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public Boolean compute() { - return vf != null && (myExcludedIndex.isInContent(vf) || isFileInBaseDir(vf) || vf.equals(myProject.getBaseDir()) || - hasExplicitMapping(vf) || isInDirectoryBasedRoot(vf) - || !Registry.is("ide.hide.excluded.files") && myExcludedIndex.isExcludedFile(vf)) - && !isIgnored(vf); - } - }); + return ApplicationManager.getApplication().runReadAction((Computable)() -> + vf != null && (myExcludedIndex.isInContent(vf) || isFileInBaseDir(vf) || vf.equals(myProject.getBaseDir()) || + hasExplicitMapping(vf) || isInDirectoryBasedRoot(vf) + || !Registry.is("ide.hide.excluded.files") && myExcludedIndex.isExcludedFile(vf)) + && !isIgnored(vf)); } @Override @@ -904,7 +886,7 @@ public class ProjectLevelVcsManagerImpl extends ProjectLevelVcsManagerEx impleme private static class ActionKey { private final Object[] myObjects; - public ActionKey(@NotNull Object... objects) { + ActionKey(@NotNull Object... objects) { myObjects = objects; }