1. Function接口,提供两个方法:
apply方法:
可以自定义自己想实现的功能
1 @Nullable T apply(@Nullable F input);
1. 实例:
1 import com.google.common.base.Function; 2 import com.google.common.base.Joiner; 3 import org.junit.Test; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 7 /** 8 * author: 龚细军 9 * class-aim: 10 */ 11 public class Demo<F, T> { 12 13 private Logger logger = LoggerFactory.getLogger(Demo.class); 14 15 public void demoFunction(F input, Function<F, T> function) throws Exception { 16 17 logger.info(String.valueOf(function.apply(input))); 18 } 19 20 @Test 21 public void show() { 22 23 Function<F, T> functionTest = new Function<F, T>() { 24 @Override 25 public T apply(F input) { 26 return (T) Integer.valueOf(String.valueOf(input)); 27 } 28 }; 29 30 try { 31 demoFunction((F) "123", functionTest); 32 } catch (Exception e) { 33 logger.error(Joiner.on("").join("{}show发生异常", e)); 34 } 35 } 36 }
一般而言,使用这种方法,可以让我们避免做重复的事情,比如,我们在使用jdbc时,会出现大量重复的代码,我们可以采用Function<F,T>来进行函数编程,简化代码:
2 3 4 import com.google.common.base.Function; 5 import com.google.common.collect.Lists; 6 import com.qunar.fresh.bean.PageVistor; 7 import com.qunar.fresh.bean.dbBean; 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 import org.springframework.beans.factory.BeanFactory; 11 import org.springframework.context.support.FileSystemXmlApplicationContext; 12 13 import java.sql.*; 14 import java.util.List; 15 16 17 /** 18 * author: 龚细军 19 * class-aim: dao-dbDemo 20 */ 21 public class DbUtil<F, T> { 22 23 private static final String CONFIG_PROPERTIES = "classpath:Beans.xml"; 24 25 26 static Logger logger = LoggerFactory.getLogger(DbUtil.class); 27 28 private Connection connection = null; 29 private static dbBean dataBean; 30 private PreparedStatement preparedStatement = null; 31 private ResultSet resultSet = null; 32 private List<PageVistor> PageVisitors; 33 34 static { 35 try { 36 BeanFactory beanFactory = new FileSystemXmlApplicationContext(CONFIG_PROPERTIES); 37 38 dataBean = (dbBean) beanFactory.getBean("dbBean"); 39 Class.forName(dataBean.getDriver()); 40 } catch (ClassNotFoundException e) { 41 logger.info(String.format("sql exception0 : %s", e)); 42 } 43 } 44 45 46 private T execute(Function<F, T> function, String sql) { 47 48 T execResult = null; 49 50 try { 51 connection = DriverManager.getConnection(dataBean.getUrl(), 52 dataBean.getUsername(), dataBean.getPassword()); 53 preparedStatement = connection.prepareStatement(sql); 54 execResult = function.apply(null); 55 } catch (SQLException e) { 56 logger.info(String.format("sql exception1 : %s", e)); 57 } finally { 58 try { 59 if (!preparedStatement.isClosed()) 60 preparedStatement.close(); 61 if (!connection.isClosed()) 62 connection.close(); 63 } catch (SQLException e) { 64 logger.info(String.format("sql exception2 : %s", e)); 65 } 66 } 67 68 return execResult; 69 } 70 71 72 public List<PageVistor> Query(String sql) { 73 74 75 PageVisitors = Lists.newArrayList(); 76 77 Function<F, T> function = new Function<F, T>() { 78 79 @Override 80 public T apply(F input) { 81 82 83 try { 84 resultSet = preparedStatement.executeQuery(); 85 86 while (resultSet.next()) { 87 PageVisitors.add(new PageVistor(0, "", resultSet.getString(1) 88 , resultSet.getInt(2))); 89 } 90 } catch (SQLException e) { 91 logger.info(String.format("sql exception3 : %s", e)); 92 } finally { 93 try { 94 95 if (resultSet != null) resultSet.close(); 96 97 } catch (SQLException e) { 98 logger.info(String.format("sql exception4 : %s", e)); 99 } 100 } 101 102 return null; 103 } 104 }; 105 this.execute(function, sql); 106 107 return PageVisitors; 108 } 109 110 111 public void Update(String sql) { 112 113 Function<F, T> function = new Function<F, T>() { 114 115 @Override 116 public T apply(F input) { 117 118 try { 119 int var = preparedStatement.executeUpdate(); 120 if (var == 0) 121 logger.error("{} 更新数据失败"); 122 } catch (SQLException e) { 123 logger.info(String.format("sql exception3 : %s", e)); 124 } 125 return null; 126 } 127 }; 128 129 this.execute(function, sql); 130 } 131 }