PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 14 * The above copyright notice and this permission notice shall be included in
 15 * all copies or substantial portions of the Software.
 16
 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23 * THE SOFTWARE.
 24 */
 25
 26package com.googlecode.charts4j.parameters;
 27
 28import java.util.List;
 29
 30import com.googlecode.charts4j.LineStyle;
 31import com.googlecode.charts4j.collect.Lists;
 32
 33/**
 34 * Class for building line style parameter string for the Google Chart API.
 35 * 
 36 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 37 */
 38final class LineChartLineStylesParameter extends AbstractParameter {
 39
 40    /** The Google Chart API line style parameter. */
 41    private static final String          URL_PARAMETER_KEY = "chls";
 42
 43    /** The line styles. */
 44    private final List<LineStyleWrapper> lineStyles        = Lists.newLinkedList();
 45
 46    /**
 47     * Add a line style.
 48     *
 49     * @param lineStyle
 50     *            the line style
 51     */
 52    void addLineStyle(final LineStyle lineStyle) {
 53        this.lineStyles.add(new LineStyleWrapper(lineStyle));
 54    }
 55
 56    /**
 57     * {@inheritDoc}
 58     */
 59    @Override
 60    public String getKey() {
 61        return URL_PARAMETER_KEY;
 62    }
 63
 64    /**
 65     * {@inheritDoc}
 66     */
 67    @Override
 68    public String getValue() {
 69        final StringBuilder sb = new StringBuilder();
 70        int cnt = 0;
 71        for (LineStyleWrapper l : lineStyles) {
 72            sb.append(cnt++ > 0 ? "|" : "").append(l);
 73        }
 74        return !lineStyles.isEmpty() ? sb.toString() : "";
 75    }
 76
 77    /**
 78     * The LineStyleWrapper.
 79     */
 80    private static final class LineStyleWrapper {
 81
 82        /** The line style. */
 83        private final LineStyle lineStyle;
 84
 85        /**
 86         * Instantiates a new line style wrapper.
 87         *
 88         * @param lineStyle
 89         *            the line style
 90         */
 91        LineStyleWrapper(final LineStyle lineStyle) {
 92            this.lineStyle = lineStyle;
 93        }
 94
 95        /**
 96         * {@inheritDoc}
 97         */
 98        @Override
 99        public String toString() {
100            return lineStyle.getLineThickness() + "," + lineStyle.getLengthOfLineSegment() + "," + lineStyle.getLengthOfBlankSegment();
101        }
102    }
103}