1. /*
  2. * @(#)Segment.java 1.12 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text;
  8. /**
  9. * A segment of a character array representing a fragment
  10. * of text. It should be treated as immutable even though
  11. * the array is directly accessable. This gives fast access
  12. * to fragments of text without the overhead of copying
  13. * around characters. This is effectively an unprotected
  14. * String.
  15. */
  16. public class Segment {
  17. /**
  18. * This is the array containing the text of
  19. * interest. This array should never be modified;
  20. * it is available only for efficiency.
  21. */
  22. public char[] array;
  23. /**
  24. * This is the offset into the array that
  25. * the desired text begins.
  26. */
  27. public int offset;
  28. /**
  29. * This is the number of array elements that
  30. * make up the text of interest.
  31. */
  32. public int count;
  33. /**
  34. * Creates a new segment.
  35. */
  36. public Segment() {
  37. array = null;
  38. offset = 0;
  39. count = 0;
  40. }
  41. /**
  42. * Creates a new segment referring to an existing array.
  43. *
  44. * @param array the array to refer to
  45. * @param offset the offset into the array
  46. * @param count the number of characters
  47. */
  48. public Segment(char[] array, int offset, int count) {
  49. this.array = array;
  50. this.offset = offset;
  51. this.count = count;
  52. }
  53. /**
  54. * Converts a segment into a String.
  55. *
  56. * @return the string
  57. */
  58. public String toString() {
  59. if (array != null) {
  60. return new String(array, offset, count);
  61. }
  62. return new String();
  63. }
  64. }