/src/main/java/com/googlecode/charts4j/parameters/LineChartLineStylesParameter.java

http://charts4j.googlecode.com/ · Java · 103 lines · 34 code · 13 blank · 56 comment · 1 complexity · 322948170e947e3a8947ace439634e7b MD5 · raw file

  1. /**
  2. *
  3. * The MIT License
  4. *
  5. * Copyright (c) 2011 the original author or authors.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. package com.googlecode.charts4j.parameters;
  24. import java.util.List;
  25. import com.googlecode.charts4j.LineStyle;
  26. import com.googlecode.charts4j.collect.Lists;
  27. /**
  28. * Class for building line style parameter string for the Google Chart API.
  29. *
  30. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  31. */
  32. final class LineChartLineStylesParameter extends AbstractParameter {
  33. /** The Google Chart API line style parameter. */
  34. private static final String URL_PARAMETER_KEY = "chls";
  35. /** The line styles. */
  36. private final List<LineStyleWrapper> lineStyles = Lists.newLinkedList();
  37. /**
  38. * Add a line style.
  39. *
  40. * @param lineStyle
  41. * the line style
  42. */
  43. void addLineStyle(final LineStyle lineStyle) {
  44. this.lineStyles.add(new LineStyleWrapper(lineStyle));
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. @Override
  50. public String getKey() {
  51. return URL_PARAMETER_KEY;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. @Override
  57. public String getValue() {
  58. final StringBuilder sb = new StringBuilder();
  59. int cnt = 0;
  60. for (LineStyleWrapper l : lineStyles) {
  61. sb.append(cnt++ > 0 ? "|" : "").append(l);
  62. }
  63. return !lineStyles.isEmpty() ? sb.toString() : "";
  64. }
  65. /**
  66. * The LineStyleWrapper.
  67. */
  68. private static final class LineStyleWrapper {
  69. /** The line style. */
  70. private final LineStyle lineStyle;
  71. /**
  72. * Instantiates a new line style wrapper.
  73. *
  74. * @param lineStyle
  75. * the line style
  76. */
  77. LineStyleWrapper(final LineStyle lineStyle) {
  78. this.lineStyle = lineStyle;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. @Override
  84. public String toString() {
  85. return lineStyle.getLineThickness() + "," + lineStyle.getLengthOfLineSegment() + "," + lineStyle.getLengthOfBlankSegment();
  86. }
  87. }
  88. }