com.hfg.util.collection.DataRow Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util.collection;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Map;
import java.util.Set;
import com.hfg.javascript.JsArray;
import com.hfg.javascript.JsObjMap;
import com.hfg.util.BooleanUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.io.StreamUtil;
import com.hfg.util.io.TSV;
//------------------------------------------------------------------------------
/**
Row from a DataTable.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class DataRow extends OrderedMap
{
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public DataRow()
{
}
//--------------------------------------------------------------------------
public DataRow(Map inData)
{
super();
putAll(inData);
}
//--------------------------------------------------------------------------
public DataRow(JsObjMap inData)
{
super();
for (String key : inData.keySet())
{
DataColumn col = new DataColumn(key);
Object value = inData.get(key);
if (value instanceof Comparable)
{
put(col, (Comparable) value);
}
}
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public JsObjMap toJSON()
{
JsObjMap json = new JsObjMap();
for (DataColumn col : getDataColumns())
{
json.put(col.getTitle(), get(col));
}
return json;
}
//--------------------------------------------------------------------------
public void toTSV(File inFile)
throws Exception
{
Writer writer = null;
try
{
writer = new FileWriter(inFile);
writer.write(TSV.write(this));
}
finally
{
StreamUtil.close(writer);
}
}
//--------------------------------------------------------------------------
public String getString(DataColumn inCol)
{
Object obj = get(inCol);
return (obj != null ? obj.toString() : null);
}
//--------------------------------------------------------------------------
public Boolean getBoolean(DataColumn inCol)
{
Object obj = get(inCol);
return (obj != null ? BooleanUtil.valueOf(obj) : null);
}
//--------------------------------------------------------------------------
public Double getDouble(DataColumn inCol)
{
Double result = null;
Object obj = get(inCol);
if (obj != null)
{
if (obj instanceof Double)
{
result = (Double) obj;
}
else if (obj instanceof Number)
{
result = ((Number) obj).doubleValue();
}
else
{
result = Double.valueOf(obj.toString());
}
}
return result;
}
//--------------------------------------------------------------------------
public Float getFloat(DataColumn inCol)
{
Float result = null;
Object obj = get(inCol);
if (obj != null)
{
if (obj instanceof Float)
{
result = (Float) obj;
}
else if (obj instanceof Number)
{
result = ((Number) obj).floatValue();
}
else
{
result = Float.valueOf(obj.toString());
}
}
return result;
}
//--------------------------------------------------------------------------
public Integer getInt(DataColumn inCol)
{
Integer result = null;
Object obj = get(inCol);
if (obj != null)
{
if (obj instanceof Integer)
{
result = (Integer) obj;
}
else if (obj instanceof Number)
{
result = ((Number) obj).intValue();
}
else
{
result = Integer.valueOf(obj.toString());
}
}
return result;
}
//--------------------------------------------------------------------------
public Long getLong(DataColumn inCol)
{
Long result = null;
Object obj = get(inCol);
if (obj != null)
{
if (obj instanceof Long)
{
result = (Long) obj;
}
else if (obj instanceof Number)
{
result = ((Number) obj).longValue();
}
else
{
result = Long.valueOf(obj.toString());
}
}
return result;
}
//--------------------------------------------------------------------------
public Set getDataColumns()
{
return keySet();
}
//--------------------------------------------------------------------------
public DataColumn getDataColumn(String inName)
{
DataColumn requestedCol = null;
Set dataColumns = getDataColumns();
if (CollectionUtil.hasValues(dataColumns))
{
for (DataColumn col : dataColumns)
{
if (col.getTitle().equalsIgnoreCase(inName))
{
requestedCol = col;
break;
}
}
}
return requestedCol;
}
}