Last active
January 20, 2020 08:03
-
-
Save VenkataRaju/1212711d81f9714783891e071130c517 to your computer and use it in GitHub Desktop.
Access Counting CharSequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class AccessCountingCharSequence implements CharSequence | |
{ | |
private final CharSequence str; | |
private final List<Integer> accessCounts = new ArrayList<>(); | |
private final List<Integer> unmodifiableAccessCounts = Collections.unmodifiableList(accessCounts); | |
private int accessCount = 0; | |
public AccessCountingCharSequence(CharSequence str) | |
{ | |
this.str = str; | |
} | |
public List<Integer> accessCounts() | |
{ | |
return unmodifiableAccessCounts; | |
} | |
public void clearAccessCounts() | |
{ | |
accessCounts.clear(); | |
} | |
@Override | |
public int length() | |
{ | |
accessCounts.add(accessCount); | |
accessCount = 0; | |
return str.length(); | |
} | |
@Override | |
public char charAt(int index) | |
{ | |
var charAt = str.charAt(index); | |
accessCount++; | |
return charAt; | |
} | |
@Override | |
public CharSequence subSequence(int start, int end) | |
{ | |
return new AccessCountingCharSequence(str.subSequence(start, end)); | |
} | |
@Override | |
public String toString() | |
{ | |
return "CountingCharSequence: " + str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment