001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.wsclient.internal;
021
022import java.text.SimpleDateFormat;
023import java.util.Date;
024import java.util.LinkedHashMap;
025import java.util.Map;
026
027/**
028 * Not an API. Please do not use this class, except maybe for unit tests.
029 */
030public class EncodingUtils {
031
032  private static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
033  private static final String DATE_FORMAT = "yyyy-MM-dd";
034
035  private EncodingUtils() {
036    // only static methods
037  }
038
039  public static Map<String, Object> toMap(String... array) {
040    if (array.length%2==1) {
041      throw new IllegalArgumentException("Not an even number of arguments");
042    }
043    Map<String, Object> map = new LinkedHashMap<String, Object>();
044    for (int i = 0; i < array.length; i += 2) {
045      Object value = array[i + 1];
046      if (value != null) {
047        map.put(array[i], array[i + 1]);
048      }
049    }
050    return map;
051  }
052
053  public static String toQueryParam(String[] strings) {
054    StringBuilder sb = new StringBuilder();
055    boolean first = true;
056    for (String string : strings) {
057      if (!first) {
058        sb.append(',');
059      }
060      sb.append(string);
061      first = false;
062    }
063    return sb.toString();
064  }
065
066  public static String toQueryParam(Date d, boolean includeTime) {
067    String format = includeTime ? DATETIME_FORMAT : DATE_FORMAT;
068    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
069    return dateFormat.format(d);
070  }
071}