All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.expleague.commons.seq.CharSeqArray Maven / Gradle / Ivy

Go to download

Utilities including math, charsequence based text processing, sequences etc.

There is a newer version: 1.4.9
Show newest version
package com.expleague.commons.seq;

import org.jetbrains.annotations.NotNull;

import java.util.function.IntSupplier;
import java.util.stream.BaseStream;
import java.util.stream.IntStream;

public class CharSeqArray extends CharSeq {
  public final char[] array;
  public final int start;
  public final int end;

  public CharSeqArray(final char[] array, final int start, final int end) {
    if (end < start)
      throw new ArrayIndexOutOfBoundsException();
    this.array = array;
    this.start = start;
    this.end = end;
  }

  public CharSeqArray(final char... chars) {
    this(chars, 0, chars.length);
  }

  public CharSeqArray(final Character[] chars) {
    this(new char[chars.length], 0, chars.length);
    for(int i = 0; i < chars.length; i++) {
      this.array[i]  = chars[i];
    }
  }

  @Override
  public int length() {
    return end - start;
  }

  @Override
  public char charAt(final int offset) {
    return array[start + offset];
  }

  @Override
  public CharSeq sub(final int start, final int end) {
    if (start == 0 && end == length())
      return this;
    return new CharSeqArray(array, start + this.start, end + this.start);
  }

  @NotNull
  public String toString() {
    return new String(array, start, end - start);
  }

  @Override
  public char[] toCharArray() {
    if(start == 0 && end == array.length) {
      return array;
    }
    return super.toCharArray();
  }

  @Override
  public void copyToArray(final int start, final char[] array, final int offset, final int length) {
    System.arraycopy(this.array, this.start + start, array, offset, length);
  }

  @Override
  public boolean equals(final Object object) {
    if (object == this) return true;
    if (object instanceof CharSeqArray) {
      final CharSeqArray other = (CharSeqArray) object;
      if (other.hashCode != 0 && this.hashCode != 0 && other.hashCode != this.hashCode) return false;
      
      final char[] otherArray = other.array;
      final char[] thisArray = this.array;
      final int otherStart = other.start;
      final int thisStart = this.start;
      final int otherEnd = other.end;
      final int thisEnd = this.end;

      if (otherArray == thisArray && otherStart == thisStart && otherEnd == thisEnd) {
        return true;
      }

      final int thisLength = thisEnd - thisStart;
      final int otherLength = otherEnd - otherStart;
      if (otherLength != thisLength) return false;

      for (int i = 0; i < thisLength; i++) {
        if (otherArray[otherStart + i] != thisArray[thisStart + i]) return false;
      }

      return true;
    }
    return super.equals(object);
  }

  protected int hashCode;
  public int hashCode() {
    if(hashCode != 0) {
      return hashCode;
    }
    int h = 0;
    for (int i = start; i < end; i++) {
      h = 31*h + array[i];
    }
    h = h == 0 ? 1 : h;
    return hashCode = h;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy