From 194e8dfe986d84f338f5d2eb669ad8201608dd5c Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 26 Jul 2016 15:28:23 +0300 Subject: [PATCH] make SingleCharSequence available to util --- .../jps/incremental/LineOutputWriter.java | 42 +++++++------------ .../util/text/SingleCharSequence.java | 42 +++++++++++++++++++ 2 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 platform/util/src/com/intellij/util/text/SingleCharSequence.java diff --git a/jps/jps-builders/src/org/jetbrains/jps/incremental/LineOutputWriter.java b/jps/jps-builders/src/org/jetbrains/jps/incremental/LineOutputWriter.java index 01a8c790d5b7..1e7db0374758 100644 --- a/jps/jps-builders/src/org/jetbrains/jps/incremental/LineOutputWriter.java +++ b/jps/jps-builders/src/org/jetbrains/jps/incremental/LineOutputWriter.java @@ -16,6 +16,7 @@ package org.jetbrains.jps.incremental; import com.intellij.util.text.CharArrayCharSequence; +import com.intellij.util.text.SingleCharSequence; import java.io.IOException; import java.io.Writer; @@ -27,37 +28,45 @@ import java.io.Writer; public abstract class LineOutputWriter extends Writer { private final LineParser myLineParser = new LineParser(); + @Override public void write(int c) { processData(new CharSequenceIterator(c)); } + @Override public void write(char[] cbuf) { processData(new CharSequenceIterator(cbuf)); } + @Override public void write(String str) { processData(new CharSequenceIterator(str)); } + @Override public void write(String str, int off, int len) { processData(new CharSequenceIterator(str.subSequence(off, off + len))); } + @Override public Writer append(CharSequence csq) { processData(new CharSequenceIterator(csq)); return this; } + @Override public Writer append(CharSequence csq, int start, int end) { processData(new CharSequenceIterator(csq.subSequence(start, end))); return this; } + @Override public Writer append(char c) { processData(new CharSequenceIterator(c)); return this; } + @Override public void write(char[] cbuf, int off, int len) { processData(new CharSequenceIterator(cbuf, off, len)); } @@ -71,9 +80,11 @@ public abstract class LineOutputWriter extends Writer { } + @Override public void flush() throws IOException { } + @Override public void close() throws IOException { try { if (myLineParser.hasData()) { @@ -87,14 +98,14 @@ public abstract class LineOutputWriter extends Writer { protected abstract void lineAvailable(String line); - private static interface CharIterator { + private interface CharIterator { char nextChar(); boolean hasData(); } private static class LineParser { private final StringBuilder myData = new StringBuilder(); - private boolean myFoundCR = false; + private boolean myFoundCR; public boolean parse(CharIterator it) { while (it.hasData()) { @@ -133,7 +144,7 @@ public abstract class LineOutputWriter extends Writer { private static class CharSequenceIterator implements CharIterator { private final CharSequence myChars; - private int myCursor = 0; + private int myCursor; CharSequenceIterator(final int ch) { this((char)ch); @@ -155,35 +166,14 @@ public abstract class LineOutputWriter extends Writer { myChars = sequence; } + @Override public char nextChar() { return myChars.charAt(myCursor++); } + @Override public boolean hasData() { return myCursor < myChars.length(); } } - - private static class SingleCharSequence implements CharSequence { - private final char myCh; - - public SingleCharSequence(char ch) { - myCh = ch; - } - - public int length() { - return 1; - } - - public char charAt(int index) { - if (index != 0) { - throw new IndexOutOfBoundsException("Index out of bounds: " + index); - } - return myCh; - } - - public CharSequence subSequence(int start, int end) { - throw new RuntimeException("Method subSequence not implemented"); - } - } } diff --git a/platform/util/src/com/intellij/util/text/SingleCharSequence.java b/platform/util/src/com/intellij/util/text/SingleCharSequence.java new file mode 100644 index 000000000000..d29fe18a5af1 --- /dev/null +++ b/platform/util/src/com/intellij/util/text/SingleCharSequence.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * 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.intellij.util.text; + +public class SingleCharSequence implements CharSequence { + private final char myCh; + + public SingleCharSequence(char ch) { + myCh = ch; + } + + @Override + public int length() { + return 1; + } + + @Override + public char charAt(int index) { + if (index != 0) { + throw new IndexOutOfBoundsException("Index out of bounds: " + index); + } + return myCh; + } + + @Override + public CharSequence subSequence(int start, int end) { + throw new RuntimeException("Method subSequence not implemented"); + } +}