Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class TimeoutString implements CharSequence {
  2. private final CharSequence child;
  3. private final long target;
  4.  
  5. public TimeoutString(CharSequence child, long timeout) {
  6. this.child = child;
  7. this.target = System.nanoTime() + timeout*1000000;
  8. }
  9.  
  10. private void checkTarget() {
  11. if (System.nanoTime() > target)
  12. throw new IllegalStateException("String operation timeout.");
  13. }
  14.  
  15. @Override
  16. public int length() {
  17. checkTarget();
  18. return child.length();
  19. }
  20.  
  21. @Override
  22. public char charAt(int i) {
  23. checkTarget();
  24. return child.charAt(i);
  25. }
  26.  
  27. @Override
  28. public CharSequence subSequence(int a, int b) {
  29. checkTarget();
  30. return child.subSequence(a, b);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement