You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/04/26 15:40:53 UTC

[GitHub] [commons-io] garydgregory commented on a change in pull request #91: Support sub sequences in CharSequenceReader

garydgregory commented on a change in pull request #91:
URL: https://github.com/apache/commons-io/pull/91#discussion_r415337219



##########
File path: src/main/java/org/apache/commons/io/input/CharSequenceReader.java
##########
@@ -37,22 +37,107 @@
     private int idx;
     private int mark;
 
+    /*
+     * end is an Integer instead of int because of backwards compatibility.
+     * When de-serializing a CharSequenceReader that was serialized before
+     * these two fields were added, they will be initialized to 0 and null
+     * respectively. If end was an int, it would be initialized to 0 as well.
+     * That would cause all de-serialized CharSequenceReaders to be empty.
+     */
+    private final int start;
+    private final Integer end;
+
     /**
      * Construct a new instance with the specified character sequence.
      *
      * @param charSequence The character sequence, may be {@code null}
      */
     public CharSequenceReader(final CharSequence charSequence) {
+        this(charSequence, 0);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start index is not strictly enforced to be within the bounds of the
+     * character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @throws IllegalArgumentException if the start index is negative
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start) {
+        this(charSequence, start, Integer.MAX_VALUE);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start and end indexes are not strictly enforced to be within the bounds
+     * of the character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read; if the character sequence
+     * grows smaller than the end, this instance will use the actual character sequence
+     * length.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @param end The start index in the character sequence, exclusive
+     * @throws IllegalArgumentException if the start index is negative, or if the end index is smaller than the start index
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start, int end) {

Review comment:
       Missing `final`

##########
File path: src/main/java/org/apache/commons/io/input/CharSequenceReader.java
##########
@@ -37,22 +37,107 @@
     private int idx;
     private int mark;
 
+    /*
+     * end is an Integer instead of int because of backwards compatibility.
+     * When de-serializing a CharSequenceReader that was serialized before
+     * these two fields were added, they will be initialized to 0 and null
+     * respectively. If end was an int, it would be initialized to 0 as well.
+     * That would cause all de-serialized CharSequenceReaders to be empty.
+     */
+    private final int start;
+    private final Integer end;
+
     /**
      * Construct a new instance with the specified character sequence.
      *
      * @param charSequence The character sequence, may be {@code null}
      */
     public CharSequenceReader(final CharSequence charSequence) {
+        this(charSequence, 0);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start index is not strictly enforced to be within the bounds of the
+     * character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @throws IllegalArgumentException if the start index is negative
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start) {

Review comment:
       `int start` -> `final int start`

##########
File path: src/main/java/org/apache/commons/io/input/CharSequenceReader.java
##########
@@ -37,22 +37,107 @@
     private int idx;
     private int mark;
 
+    /*
+     * end is an Integer instead of int because of backwards compatibility.
+     * When de-serializing a CharSequenceReader that was serialized before
+     * these two fields were added, they will be initialized to 0 and null
+     * respectively. If end was an int, it would be initialized to 0 as well.
+     * That would cause all de-serialized CharSequenceReaders to be empty.
+     */
+    private final int start;
+    private final Integer end;
+
     /**
      * Construct a new instance with the specified character sequence.
      *
      * @param charSequence The character sequence, may be {@code null}
      */
     public CharSequenceReader(final CharSequence charSequence) {
+        this(charSequence, 0);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start index is not strictly enforced to be within the bounds of the
+     * character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @throws IllegalArgumentException if the start index is negative
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start) {
+        this(charSequence, start, Integer.MAX_VALUE);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start and end indexes are not strictly enforced to be within the bounds
+     * of the character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read; if the character sequence
+     * grows smaller than the end, this instance will use the actual character sequence
+     * length.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @param end The start index in the character sequence, exclusive
+     * @throws IllegalArgumentException if the start index is negative, or if the end index is smaller than the start index
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start, int end) {
+        if (start < 0) {
+            throw new IllegalArgumentException(
+                    "Start index is less than zero: " + start);
+        }
+        if (end < start) {
+            throw new IllegalArgumentException(
+                    "End index is less than start " + start + ": " + end);
+        }
+        // Don't check the start and end indexes against the CharSequence,
+        // to let it grow and shrink without breaking existing behavior.
+
         this.charSequence = charSequence != null ? charSequence : "";
+        this.start = start;
+        this.end = end;
+
+        this.idx = start;
+        this.mark = start;
+    }
+
+    /**
+     * @return The start index in the character sequence, taking into account its length.

Review comment:
       Missing Javadoc: "Starts..."

##########
File path: src/main/java/org/apache/commons/io/input/CharSequenceReader.java
##########
@@ -37,22 +37,107 @@
     private int idx;
     private int mark;
 
+    /*
+     * end is an Integer instead of int because of backwards compatibility.
+     * When de-serializing a CharSequenceReader that was serialized before
+     * these two fields were added, they will be initialized to 0 and null
+     * respectively. If end was an int, it would be initialized to 0 as well.
+     * That would cause all de-serialized CharSequenceReaders to be empty.
+     */
+    private final int start;
+    private final Integer end;
+
     /**
      * Construct a new instance with the specified character sequence.
      *
      * @param charSequence The character sequence, may be {@code null}
      */
     public CharSequenceReader(final CharSequence charSequence) {
+        this(charSequence, 0);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start index is not strictly enforced to be within the bounds of the
+     * character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @throws IllegalArgumentException if the start index is negative
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start) {
+        this(charSequence, start, Integer.MAX_VALUE);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character sequence.
+     * <p>
+     * The start and end indexes are not strictly enforced to be within the bounds
+     * of the character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be thrown.
+     * Instead, if the character sequence grows smaller than the start index, this
+     * instance will act as if all characters have been read; if the character sequence
+     * grows smaller than the end, this instance will use the actual character sequence
+     * length.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @param end The start index in the character sequence, exclusive
+     * @throws IllegalArgumentException if the start index is negative, or if the end index is smaller than the start index
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start, int end) {
+        if (start < 0) {
+            throw new IllegalArgumentException(
+                    "Start index is less than zero: " + start);
+        }
+        if (end < start) {
+            throw new IllegalArgumentException(
+                    "End index is less than start " + start + ": " + end);
+        }
+        // Don't check the start and end indexes against the CharSequence,
+        // to let it grow and shrink without breaking existing behavior.
+
         this.charSequence = charSequence != null ? charSequence : "";
+        this.start = start;
+        this.end = end;
+
+        this.idx = start;
+        this.mark = start;
+    }
+
+    /**
+     * @return The start index in the character sequence, taking into account its length.
+     */
+    private int start() {
+        return Math.min(charSequence.length(), start);
+    }
+
+    /**
+     * @return The end index in the character sequence, taking into account its length.

Review comment:
       Missing Javadoc: "Ends..."




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org