PageRenderTime 79ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/pride-chart/trunk/src/main/java/uk/ac/ebi/pride/chart/graphics/implementation/data/QuartilesDataSeries.java

http://pride-toolsuite.googlecode.com/
Java | 170 lines | 84 code | 17 blank | 69 comment | 12 complexity | 5edded0ae9ca84e91fce4592fce17bfe MD5 | raw file
  1. package uk.ac.ebi.pride.chart.graphics.implementation.data;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. /**
  7. * <p>A wrapper class for a JSONObject containing the chart intermediate series data</p>
  8. *
  9. * @author Antonio Fabregat
  10. * Date: 09-mar-2011
  11. * Time: 16:39:59
  12. */
  13. public class QuartilesDataSeries<T,U> {
  14. /**
  15. * Defines the key for the series type
  16. */
  17. private static final String TYPE_KEY = "type";
  18. /**
  19. * Defines the key for the series identifier
  20. */
  21. private static final String ID_KEY = "id";
  22. /**
  23. * Defines the key for the stored x axis values
  24. */
  25. private static final String X_AXIS_KEY = "XAxis";
  26. /**
  27. * Defines the key for the stored y axis values
  28. */
  29. private static final String Y_AXIS_KEY = "YAxis";
  30. /**
  31. * Contains an unordered collection of name/value pairs
  32. */
  33. private JSONObject data;
  34. /**
  35. * <p> Creates an instance of this DataSeries object, setting all fields as per description below.</p>
  36. *
  37. * @param data the JSONObject to be managed in this wrapper class
  38. */
  39. public QuartilesDataSeries(JSONObject data) {
  40. this.data = data;
  41. }
  42. /**
  43. * <p> Creates an instance of this DataSeries object, setting all fields as per description below.</p>
  44. *
  45. * @param type The quartiles type
  46. * @param identifier The series identifier
  47. * @param data a list of SeriesPair containing the chart intermediate data
  48. */
  49. public QuartilesDataSeries(QuartilesType type, String identifier, List<SeriesPair<T,U>> data) {
  50. this.data = new JSONObject();
  51. try {
  52. if(type!=null) this.data.put(TYPE_KEY, type.getType());
  53. this.data.put(ID_KEY, identifier);
  54. for (SeriesPair<T, U> sp : data) {
  55. this.data.append(X_AXIS_KEY, sp.getX());
  56. this.data.append(Y_AXIS_KEY, sp.getY());
  57. }
  58. } catch (JSONException e) {/*Nothing here*/}
  59. }
  60. /**
  61. * Returns a list of SeriesPair containing the chart intermediate data
  62. *
  63. * @param classT the T.class
  64. * @param classU the U.class
  65. * @return a list of SeriesPair containing the chart intermediate data
  66. */
  67. public List<SeriesPair<T,U>> getSeriesValues(Class<T> classT, Class<U> classU){
  68. List<SeriesPair<T,U>> valuesArray = new ArrayList<SeriesPair<T,U>>();
  69. try {
  70. PrideJSONArray<T> xAxis = new PrideJSONArray<T>(data.getJSONArray(X_AXIS_KEY));
  71. PrideJSONArray<U> yAxis = new PrideJSONArray<U>(data.getJSONArray(Y_AXIS_KEY));
  72. for(int i=0; i<xAxis.size(); i++){
  73. T x = xAxis.get(i, classT);
  74. U y = yAxis.get(i, classU);
  75. valuesArray.add(new SeriesPair<T,U>(x, y));
  76. }
  77. } catch (JSONException e) {/*Nothing here*/}
  78. return valuesArray;
  79. }
  80. /**
  81. * Returns the Series identifier
  82. *
  83. * @return the Series identifier
  84. */
  85. public String getIdentifier(){
  86. String identifier;
  87. try {
  88. identifier = (String) data.get(ID_KEY);
  89. } catch (JSONException e) {
  90. identifier = "";
  91. }
  92. return identifier;
  93. }
  94. /**
  95. * Returns the Series type label
  96. *
  97. * @return the Series type label
  98. */
  99. public String getTypeLabel(){
  100. String typeLabel;
  101. try {
  102. typeLabel = data.getString(TYPE_KEY);
  103. } catch (JSONException e) {
  104. typeLabel = "";
  105. }
  106. return typeLabel;
  107. }
  108. /**
  109. * Returns true if the series contains empty values in all the Y values
  110. *
  111. * @param classT the T.class
  112. * @param classU the U.class
  113. * @return true if the series contains empty values in all the Y values
  114. */
  115. public boolean isEmpty(Class<T> classT, Class<U> classU) {
  116. List<SeriesPair<T,U>> values = getSeriesValues(classT, classU);
  117. boolean isEmpty = true;
  118. for (SeriesPair<T, U> value : values) {
  119. U y = value.getY();
  120. if(String.class.equals(classU)) {
  121. isEmpty &= y.equals("");
  122. } else if (Double.class.equals(classU)) {
  123. isEmpty &= (Double) y == 0.0;
  124. } else if (Integer.class.equals(classU)) {
  125. isEmpty &= (Integer) y == 0.0;
  126. } else {
  127. //If the type is not supported, by default returns isEmpty<-true
  128. return true;
  129. }
  130. }
  131. return isEmpty;
  132. }
  133. public DataSeriesType getType(){
  134. return DataSeriesType.getSeriesType(getTypeLabel());
  135. }
  136. /**
  137. * Returns the JSONObject wrapped by the class
  138. *
  139. * @return the JSONObject wrapped by the class
  140. */
  141. public JSONObject getJSONObject(){
  142. return data;
  143. }
  144. /**
  145. * Returns the data contained in JSon Format
  146. *
  147. * @return the data contained in JSon Format
  148. */
  149. @Override
  150. public String toString() {
  151. return data.toString();
  152. }
  153. }