View Javadoc
1   package guru.mikelue.jdut.operation;
2   
3   import java.sql.Connection;
4   import java.sql.SQLException;
5   import java.util.function.BiConsumer;
6   import java.util.function.UnaryOperator;
7   
8   import guru.mikelue.jdut.datagrain.DataGrain;
9   import guru.mikelue.jdut.jdbc.JdbcFunction;
10  import guru.mikelue.jdut.jdbc.SQLExceptionConvert;
11  
12  /**
13   * Operator to execute code by fed {@link DataGrain}.
14   *
15   * @see <a target="_blank" href="https://github.com/mikelue/jdata-unit-test/wiki/Provided-data-operations">Provided operators</a>
16   */
17  @FunctionalInterface
18  public interface DataGrainOperator {
19  	/**
20  	 * The operator for surrounding of {@link DataGrainOperator}.
21  	 */
22  	@FunctionalInterface
23  	public interface SurroundOperator {
24  		/**
25  		 * Converts this lambda to {@link UnaryOperator}.
26  		 *
27  		 * @return The unary operator
28  		 */
29  		default UnaryOperator<DataGrainOperator> asUnaryOperator()
30  		{
31  			return operator -> surround(operator);
32  		}
33  
34  		/**
35  		 * Surrounds operator.
36  		 *
37  		 * @param surroundedOperator The oprator to be surrounded
38  		 *
39  		 * @return The final function
40  		 */
41  		public DataGrainOperator./guru/mikelue/jdut/operation/DataGrainOperator.html#DataGrainOperator">DataGrainOperator surround(DataGrainOperator surroundedOperator);
42  	}
43  
44  	/**
45  	 * Does nothing.
46  	 *
47  	 * @param conn The connection object
48  	 * @param dataGrain The object of data grain
49  	 *
50  	 * @return The same data grain
51  	 */
52  	static DataGrainu/mikelue/jdut/datagrain/DataGrain.html#DataGrain">DataGrain none(Connection conn, DataGrain dataGrain) { return dataGrain; }
53  
54  	/**
55  	 * Converts this lambda to {@link JdbcFunction}.
56  	 *
57  	 * @param dataGrain The data grain to be fed
58  	 *
59  	 * @return The JDBC function */
60  	default JdbcFunction<Connection, DataGrain> toJdbcFunction(DataGrain dataGrain)
61  	{
62  		return conn -> operate(conn, dataGrain);
63  	}
64  
65  	/**
66  	 * Converts this operator to {@link BiConsumer} with {@link SQLExceptionConvert} for thrown {@link SQLException}.
67  	 *
68  	 * @param <E> The type of runtime exception
69  	 * @param sqlExceptionConvert The conversion of SQLException
70  	 *
71  	 * @return A {@link BiConsumer} instance
72  	 */
73  	default <E extends RuntimeException> BiConsumer<Connection, DataGrain> asBiConsumer(SQLExceptionConvert<E> sqlExceptionConvert)
74  	{
75  		return (connection, dataGrain) -> {
76  			try {
77  				operate(connection, dataGrain);
78  			} catch (SQLException e) {
79  				throw sqlExceptionConvert.apply(e);
80  			}
81  		};
82  	}
83  	/**
84  	 * Converts this operator to {@link BiConsumer} with default {@link SQLExceptionConvert}.
85  	 *
86  	 * @return A {@link BiConsumer} instance
87  	 */
88  	default BiConsumer<Connection, DataGrain> asBiConsumer()
89  	{
90  		return asBiConsumer(SQLExceptionConvert::runtimeException);
91  	}
92  
93  	/**
94  	 * Surrounds this instance by {@link SurroundOperator}.
95  	 *
96  	 * @param surroundOperator The operator to surrounding staff
97  	 *
98  	 * @return new operator of data grain
99  	 */
100 	default DataGrainOperator surroundedBy(SurroundOperator surroundOperator)
101 	{
102 		return surroundOperator.surround(this);
103 	}
104 
105 	/**
106 	 * Operates {@link DataGrain} by {@link Connection}.
107 	 *
108 	 * @param connection The connection of database
109 	 * @param dataGrain The data grain to be operated
110 	 *
111 	 * @return The processed data grain
112 	 *
113 	 * @throws SQLException The SQL exception defined by JDBC
114 	 */
115 	public DataGrain/jdut/datagrain/DataGrain.html#DataGrain">DataGrain operate(Connection connection, DataGrain dataGrain) throws SQLException;
116 }