Builder.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model.clientstrings;
  16. import java.util.ArrayList;
  17. /**
  18. * @author Forsaiken, Zoey76
  19. */
  20. public abstract class Builder
  21. {
  22. public abstract String toString(final Object param);
  23. public abstract String toString(final Object... params);
  24. public abstract int getIndex();
  25. public static final Builder newBuilder(final String text)
  26. {
  27. final ArrayList<Builder> builders = new ArrayList<>();
  28. int index1 = 0, index2 = 0, paramId, subTextLen;
  29. final char[] array = text.toCharArray();
  30. final int arrayLength = array.length;
  31. char c, c2, c3;
  32. LOOP:
  33. for (; index1 < arrayLength; index1++)
  34. {
  35. c = array[index1];
  36. if ((c == '$') && (index1 < (arrayLength - 2)))
  37. {
  38. c2 = array[index1 + 1];
  39. if ((c2 == 'c') || (c2 == 's') || (c2 == 'p') || (c2 == 'C') || (c2 == 'S') || (c2 == 'P'))
  40. {
  41. c3 = array[index1 + 2];
  42. if (Character.isDigit(c3))
  43. {
  44. paramId = Character.getNumericValue(c3);
  45. subTextLen = index1 - index2;
  46. if (subTextLen != 0)
  47. {
  48. builders.add(new BuilderText(new String(array, index2, subTextLen)));
  49. }
  50. builders.add(new BuilderObject(paramId));
  51. index1 += 2;
  52. index2 = index1 + 1;
  53. continue LOOP;
  54. }
  55. }
  56. }
  57. }
  58. if (arrayLength >= index1)
  59. {
  60. subTextLen = index1 - index2;
  61. if (subTextLen != 0)
  62. {
  63. builders.add(new BuilderText(new String(array, index2, subTextLen)));
  64. }
  65. }
  66. if (builders.size() == 1)
  67. {
  68. return builders.get(0);
  69. }
  70. return new BuilderContainer(builders.toArray(new Builder[builders.size()]));
  71. }
  72. }