/** * Compare 2 rows with each other using one value in the rows and also considering an ascending clause. * * @param r * The row to compare with * @param fieldnr * The indexe of the values to compare * @param sort_desc * true means and normal compare, false the reverse. * @return -1 if the row is smaller, 0 if they are equal and 1 if the row is larger. */ public int compare( Row r, int fieldnr, boolean sort_desc ) { int retval = 0; Value v1, v2; v1 = getValue( fieldnr ); v2 = r.getValue( fieldnr ); retval = v1.compare( v2 ); if ( sort_desc ) { retval = -retval; } return retval; }
/** * Return the XML representation of a row. * * @return The XML representation of this row */ @Override public String getXML() { StringBuilder xml = new StringBuilder(); xml.append( "<" ).append( XML_TAG ).append( ">" ); for ( int i = 0; i < size(); i++ ) { xml.append( getValue( i ).getXML() ); } xml.append( "</" ).append( XML_TAG ).append( ">" ); return xml.toString(); }
/** * Compare 2 complete rows of values with each other * * @param r * the row to compare with * @return -1 if the row is smaller, 0 if both rows are equal, 1 if the row is larger. */ public int compare( Row r, boolean caseInsensitive ) { int retval = 0; int i; int len = r.size(); Value v1, v2; for ( i = 0; i < len; i++ ) { v1 = getValue( i ); v2 = r.getValue( i ); retval = v1.compare( v2, caseInsensitive ); if ( retval != 0 ) { return retval; } } return 0; }
@Override public int hashCode() { int hash = 0; int i; for ( i = 0; i < size(); i++ ) { hash ^= getValue( i ).hashCode(); } return hash; }
/** * Convert the row to a String representation. * * @return the row as a String. */ @Override public String toString() { StringBuilder retval = new StringBuilder( 128 ); retval.append( '[' ); for ( int i = 0; i < size(); i++ ) { Value value = getValue( i ); if ( i != 0 ) { retval.append( ", " ); } if ( value != null ) { retval.append( value.getName() ).append( '=' ).append( value.toString( false ) ); } else { retval.append( "NULL" ); } } retval.append( ']' ); return retval.toString(); }
/** * Search the Value by name in the row, return the Values index. * * @param name * the value name to search for. * @return the index of the value with the given name, -1 is nothing was found. */ public int searchValueIndex( String name ) { if ( name == null ) { return -1; } for ( int i = 0; i < size(); i++ ) { Value v = getValue( i ); if ( v.getName().equalsIgnoreCase( name ) ) { return i; } } return -1; }
/** * Search the Value by name in the row. * * @param name * the value name to search for * @return the Value with the given name, null if nothing was found. */ public Value searchValue( String name ) { if ( name == null ) { return null; } for ( int i = 0; i < size(); i++ ) { Value v = getValue( i ); String nm = v.getName(); if ( nm != null && nm.equalsIgnoreCase( name ) ) { return v; } } return null; }
/** * Add all the values of row r to the Row. * * @param r * The row to be added to this row. */ public void addRow( Row r ) { if ( r == null ) { return; } int i; for ( i = 0; i < r.size(); i++ ) { Value v1 = r.getValue( i ); addValue( v1 ); } }
/** * Write a row of Values to a DataOutputStream, without saving the meta-data. * * @param dos * The DataOutputStream to write to * @return true if the row was written successfuly, false if something went wrong. */ public boolean writeData( DataOutputStream dos ) throws KettleFileException { // get all values in the row for ( int i = 0; i < size(); i++ ) { Value v = getValue( i ); v.writeData( dos ); } return true; }
private void writeObj( DataOutputStream dos ) throws IOException { // First handle the number of fields in a row dos.writeInt( size() ); // Write all values in the row for ( int i = 0; i < size(); i++ ) { getValue( i ).writeObj( dos ); } }
/** * Checks whether or not the row is empty A row is empty if all the values in the row are null A row is empty if there * are no values in the row. * * @return true if the row is considered empty, false if the row is not empty. */ public boolean isEmpty() { boolean empty = true; for ( int i = 0; i < size(); i++ ) { Value v = getValue( i ); if ( v != null && !v.isNull() ) { empty = false; break; } } return empty; }
/** * Get an array of the names of all the Values in the Row. * * @return an array of Strings: the names of all the Values in the Row. */ public String[] getFieldNames() { String[] retval = new String[size()]; for ( int i = 0; i < size(); i++ ) { retval[i] = getValue( i ).getName(); } return retval; }
/** * Merge the data of row r to this Row. That means: All fields in row r that do exist in this row (same name and same * type) and have non-empty values will have their values written into this row, if the value of that field is empty * in this row. * * @param r * The row to be merged with this row */ public void mergeData( Row r ) { if ( r == null ) { return; } for ( int x = 0; x < r.size(); x++ ) { Value other = r.getValue( x ); Value value = searchValue( other.getName() ); if ( value != null ) { value.merge( other ); } } }
/** * Merge the values of row r to this Row. Merge means: only the values that are not yet in the row are added * (comparing on the value name). * * @param r * The row to be merged with this row */ public void mergeRow( Row r ) { if ( r == null ) { return; } for ( int x = 0; x < r.size(); x++ ) { Value field = r.getValue( x ); if ( searchValue( field.getName() ) == null ) { addValue( field ); // Not in list yet: add } } }
/** * Creates a new row as a duplicate of the given row. The values are copied one by one into new values. * * @param r * The row to be copied. */ // Copy values in row! public Row( Row r ) { this(); for ( int i = 0; i < r.size(); i++ ) { addValue( new Value( r.getValue( i ) ) ); } setIgnore( r.isIgnored() ); }
/** * Get an array of strings showing the name of the values in the row padded to a maximum length, followed by the types * of the values. * * @param maxlen * The length to which the name will be padded. * @return an array of strings: the names and the types of the fieldnames in the row. */ public String[] getFieldNamesAndTypes( int maxlen ) { String[] retval = new String[size()]; for ( int i = 0; i < size(); i++ ) { Value v = getValue( i ); retval[i] = Const.rightPad( v.getName(), maxlen ) + " (" + v.getTypeDesc() + ")"; } return retval; }
/** * Read a number of Values without meta-data into a row. * * @param dis * The DataInputStream to read from * @param size * the number or values to read * @param meta * The description (name, type, length, precision) of the values to be read * @throws KettleFileException * if the row couldn't be created by reading from the data input stream. */ public Row( DataInputStream dis, int size, Row meta ) throws KettleFileException { try { // get all values in the row for ( int i = 0; i < size; i++ ) { addValue( new Value( meta.getValue( i ), dis ) ); } } catch ( KettleEOFException e ) { throw new KettleEOFException( BaseMessages.getString( PKG, "Row.EndOfFileReadingRow" ), e ); } catch ( Exception e ) { throw new KettleFileException( BaseMessages.getString( PKG, "Row.RowError" ), e ); } }