package Interfaces;

/**
* Write a class that implements the CharSequence interface
* found in the java.lang package.
* Your implementation should return the string backwards.
* Select one of the sentences from this book to use as the data.
* Write a small main method to test your class.
* Make sure to call all four methods.
*/
public class ReversedCharSequence implements CharSequence {

private String charSequence;

public ReversedCharSequence (String charSequence) {
  System.out.println("String is " + charSequence);
  char[] strArray = charSequence.toCharArray();
  char[] reversedArray = strArray.clone();
  int j = strArray.length - 1;
  for (int i = 0; i < strArray.length; i++){
    reversedArray[j] = strArray[i];
    j--;
   }
  this.charSequence = new String(reversedArray);
  System.out.println("Reversed String to " + this.charSequence);
}

// Returns the length of this character sequence.
@Override
public int length() {
  return charSequence.length();
}

// Returns the char value at the specified index.
@Override
public char charAt(int i) {
  return charSequence.charAt(i);
}

// Returns a CharSequence that is a subsequence of this sequence.
@Override
public CharSequence subSequence(int i, int i1) {
  return charSequence.subSequence(i, i1);
}

// Returns a string containing the characters in this sequence 
// in the same order as this sequence
@Override
public String toString() {
  return charSequence;
}

public static void main (String[] args) {
  String test = "Select one of the sentences";
  ReversedCharSequence test2 = new ReversedCharSequence(test);
  System.out.println("length = " + test2.length());
  System.out.println("charAt 2 = " + test2.charAt(2));
  System.out.println("subSequence 1-6 = " + test2.subSequence(1,6));
  System.out.println("toString = " + test2.toString());
}
}