001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 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.services;
021
022import javax.annotation.CheckForNull;
023
024import java.util.Date;
025import java.util.Set;
026
027/**
028 * Compatibility layer between GWT and plain Java.
029 * Well, this is bad, because code is not type-safe, so all unmarshallers also,
030 * but this allows to remove duplications between sonar-gwt-api and sonar-ws-client.
031 */
032public abstract class WSUtils {
033
034  private static volatile WSUtils instance = null;
035
036  public static void setInstance(WSUtils utils) {
037    instance = utils;
038  }
039
040  public static WSUtils getINSTANCE() {
041    return instance;
042  }
043
044  public abstract String format(Date date, String format);
045
046  public abstract String encodeUrl(String url);
047
048  /**
049   * @return value of specified field from specified JSON object,
050   *         or <code>null</code> if field does not exist
051   */
052  @CheckForNull
053  public abstract Object getField(Object json, String field);
054
055  /**
056   * @return value of a string field from specified JSON object,
057   *         or string representation of a numeric field,
058   *         or <code>null</code> if field does not exist
059   */
060  @CheckForNull
061  public abstract String getString(Object json, String field);
062
063  /**
064   * @return Boolean value of specified field from specified JSON object,
065   *         or <code>null</code> if field does not exist
066   */
067  @CheckForNull
068  public abstract Boolean getBoolean(Object json, String field);
069
070  /**
071   * @return Integer value of specified field from specified JSON object,
072   *         or <code>null</code> if field does not exist
073   */
074  @CheckForNull
075  public abstract Integer getInteger(Object json, String field);
076
077  /**
078   * @return Double value of specified field from specified JSON object,
079   *         or <code>null</code> if field does not exist
080   */
081  @CheckForNull
082  public abstract Double getDouble(Object json, String field);
083
084  /**
085   * @return Long value of specified field from specified JSON object,
086   *         or <code>null</code> if field does not exist
087   */
088  @CheckForNull
089  public abstract Long getLong(Object json, String field);
090
091  /**
092   * @return Date value of specified field from specified JSON object,
093   *         or <code>null</code> if field does not exist
094   */
095  @CheckForNull
096  public abstract Date getDateTime(Object json, String field);
097
098  /**
099   * @return size of specified JSON array
100   */
101  public abstract int getArraySize(Object array);
102
103  /**
104   * @return element from specified JSON array
105   */
106  public abstract Object getArrayElement(Object array, int i);
107
108  /**
109   * @return JSON object
110   */
111  public abstract Object parse(String jsonStr);
112
113  /**
114   * @return field names in specified JSON object
115   */
116  public abstract Set<String> getFields(Object json);
117
118}