View Javadoc
1   package guru.mikelue.jdut.operation;
2   
3   import java.sql.Connection;
4   import java.sql.SQLException;
5   import java.util.ArrayList;
6   import java.util.List;
7   import java.util.function.UnaryOperator;
8   
9   import guru.mikelue.jdut.datagrain.DataGrain;
10  import guru.mikelue.jdut.datagrain.DataRow;
11  
12  /**
13   * Operator to execute code by fed {@link DataRow}.
14   */
15  @FunctionalInterface
16  public interface DataRowOperator {
17  	/**
18  	 * The operator for surrounding of {@link DataRowOperator}.
19  	 */
20  	public interface SurroundOperator extends UnaryOperator<DataRowOperator> {
21  		/**
22  		 * Converts this operator ot {@link UnaryOperator}.
23  		 *
24  		 * @return The unary operator
25  		 */
26  		default UnaryOperator<DataRowOperator> asUnaryOperator()
27  		{
28  			return operator -> surround(operator);
29  		}
30  
31  		/**
32  		 * Surrounds operator.
33  		 *
34  		 * @param surroundedOperator The oprator to be surrounded
35  		 *
36  		 * @return The final function
37  		 */
38  		public DataRowOperator/../guru/mikelue/jdut/operation/DataRowOperator.html#DataRowOperator">DataRowOperator surround(DataRowOperator surroundedOperator);
39  	}
40  
41  	/**
42  	 * Does nothing.
43  	 *
44  	 * @param conn The connection object
45  	 * @param dataRow The object of data row
46  	 *
47  	 * @return The same data row
48  	 */
49  	static DataRowuru/mikelue/jdut/datagrain/DataRow.html#DataRow">DataRow none(Connection conn, DataRow dataRow) { return dataRow; }
50  
51  	/**
52  	 * Converts this instance to {@link DataGrainOperator}.
53  	 *
54  	 * @return The operator for {@link DataGrain}
55  	 */
56  	default DataGrainOperator toDataGrainOperator()
57  	{
58  		return toDataRowsOperator().toDataGrainOperator();
59  	}
60  
61  	/**
62  	 * Converts this instance to {@link DataRowsOperator}.
63  	 *
64  	 * @return The operator for {@link List} of {@link DataRow}
65  	 */
66  	default DataRowsOperator toDataRowsOperator()
67  	{
68  		return (connection, dataRows) -> {
69  			List<DataRow> resultRows = new ArrayList<>(dataRows.size());
70  			for (DataRow dataRow: dataRows) {
71  				resultRows.add(
72  					operate(connection, dataRow)
73  				);
74  			}
75  
76  			return resultRows;
77  		};
78  	}
79  
80  	/**
81  	 * Surrounds this instance by {@link SurroundOperator}.
82  	 *
83  	 * @param surroundOperator The operator to surrounding self
84  	 *
85  	 * @return new operator of data row
86  	 */
87  	default DataRowOperator surroundedBy(SurroundOperator surroundOperator)
88  	{
89  		return surroundOperator.surround(this);
90  	}
91  
92  	/**
93  	 * Operates {@link DataRow} by {@link Connection}.
94  	 *
95  	 * @param connection The connection of database
96  	 * @param dataRow The data grain to be operated
97  	 *
98  	 * @return The processed data row
99  	 *
100 	 * @throws SQLException The SQL exception defined by JDBC
101 	 */
102 	public DataRowue/jdut/datagrain/DataRow.html#DataRow">DataRow operate(Connection connection, DataRow dataRow) throws SQLException;
103 }