001package com.hfg.util.collection;
002
003
004
005import com.hfg.html.HTMLTag;
006import com.hfg.html.Span;
007
008import java.lang.reflect.Method;
009import java.util.HashSet;
010import java.util.Map;
011import java.util.Set;
012
013//------------------------------------------------------------------------------
014/**
015 Table containing sets of subject metadata.
016 <div>
017  @author J. Alex Taylor, hairyfatguy.com
018 </div>
019 */
020//------------------------------------------------------------------------------
021// com.hfg Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041public class DataTable extends AbstractSparseMatrix<String, DataColumn, Comparable>
042{
043   private Set<String> mFlags;
044
045   //##########################################################################
046   // CONSTRUCTORS
047   //##########################################################################
048
049   //--------------------------------------------------------------------------
050   public DataTable()
051   {
052      super();
053   }
054
055   //--------------------------------------------------------------------------
056   public DataTable(int inInitialRowCapacity, int inInitialColCapacity)
057   {
058      super(inInitialRowCapacity, inInitialColCapacity);
059   }
060
061   //##########################################################################
062   // PUBLIC METHODS
063   //##########################################################################
064
065   //--------------------------------------------------------------------------
066   public DataTable setFlag(String inValue)
067   {
068      if (null == mFlags)
069      {
070         mFlags = new HashSet<>(4);
071      }
072
073      mFlags.add(inValue);
074
075      return this;
076   }
077
078   //--------------------------------------------------------------------------
079   public boolean hasFlag(String inValue)
080   {
081      return mFlags != null && mFlags.contains(inValue);
082   }
083
084   //--------------------------------------------------------------------------
085   public void clearFlags()
086   {
087      mFlags = null;
088   }
089
090   //--------------------------------------------------------------------------
091   public void clearFlag(String inValue)
092   {
093      if (mFlags != null)
094      {
095         mFlags.remove(inValue);
096      }
097   }
098
099   //--------------------------------------------------------------------------
100   public DataTable add(DataTable inDataTable)
101   {
102      for (DataColumn dataColumn : inDataTable.getDataColumns())
103      {
104         addDataSet(dataColumn, inDataTable.getDataSet(dataColumn));
105      }
106
107      return this;
108   }
109
110   //--------------------------------------------------------------------------
111   public DataTable addDataSet(DataColumn inDataColumn, Map<String, ? extends Comparable> inDataMap)
112   {
113      for (String subjectId : inDataMap.keySet())
114      {
115         put(subjectId, inDataColumn, inDataMap.get(subjectId));
116      }
117
118      return this;
119   }
120
121   //--------------------------------------------------------------------------
122   public Map<DataColumn, Comparable> getRowData(String inRowKey)
123   {
124      return getRow(inRowKey);
125   }
126
127   //--------------------------------------------------------------------------
128   public Map<String, ? extends Comparable> getDataSet(DataColumn inDataColumn)
129   {
130      return getCol(inDataColumn);
131   }
132
133   //--------------------------------------------------------------------------
134   public Set<DataColumn> getDataColumns()
135   {
136      return colKeySet();
137   }
138
139   //--------------------------------------------------------------------------
140   public DataColumn getDataColumn(String inName)
141   {
142      DataColumn requestedCol = null;
143
144      Set<DataColumn> dataColumns = getDataColumns();
145      if (CollectionUtil.hasValues(dataColumns))
146      {
147         for (DataColumn col : dataColumns)
148         {
149            if (col.getTitle().equals(inName))
150            {
151               requestedCol = col;
152               break;
153            }
154         }
155      }
156
157      return requestedCol;
158   }
159
160   //--------------------------------------------------------------------------
161   @Override
162   public void removeCol(DataColumn inCol)
163   {
164      super.removeCol(inCol);
165   }
166
167   //--------------------------------------------------------------------------
168   @Override
169   public Set<String> rowKeySet()
170   {
171      return super.rowKeySet();
172   }
173
174   //--------------------------------------------------------------------------
175   /**
176    Uses a formatting string (like "%.1f%%") specified via the DataColumn to
177    render the object value for display.
178    * @param inId subject key
179    * @param inDataColumn the data column from which to retrieve the value
180    * @return the String-formatted value
181    */
182   public String getFormatted(String inId, DataColumn inDataColumn)
183   {
184      String formattedValue = "";
185      Object value = get(inId, inDataColumn);
186      if (value != null)
187      {
188         if (inDataColumn.getFormatString() != null)
189         {
190            formattedValue = String.format(inDataColumn.getFormatString(), value);
191         }
192         else
193         {
194            formattedValue = value.toString();
195         }
196      }
197
198      return formattedValue;
199   }
200
201   //--------------------------------------------------------------------------
202   /**
203    Uses a formatting string (like "%.1f%%") specified via the DataColumn to
204    render the object value for display.
205    * @param inId subject key
206    * @param inDataColumn the data column from which to retrieve the value
207    * @return the String-formatted value
208    */
209   public HTMLTag getHTMLFormatted(String inId, DataColumn inDataColumn)
210   {
211      Object value = get(inId, inDataColumn);
212
213      HTMLTag formattedValue;
214      try
215      {
216         Method method = value.getClass().getMethod("toHTMLTag");
217         formattedValue = (HTMLTag) method.invoke(value);
218      }
219      catch (Exception e)
220      {
221         // Ignore exceptions. Just use the string value.
222         formattedValue = new Span(getFormatted(inId, inDataColumn));
223      }
224
225      return formattedValue;
226   }
227}