|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
由于这些智能化家电的市场需求没有预期的高,Sun放弃了该项计划。就在Oak几近失败之时,随着互联网的发展,Sun看到了Oak在计算机网络上的广阔应用前景,于是改造了Oak,
importorg.apache.log4j.Logger;
importjava.util.List;
/**
*@authorPietroPolsinellippolsinelli@open-lab.com
*/
publicinterfacePage{
booleanisFirstPage();
booleanisLastPage();
booleanhasNextPage();
booleanhasPreviousPage();
intgetLastPageNumber();
ListgetThisPageElements();
LoggergetLogger();
intgetTotalNumberOfElements();
intgetThisPageFirstElementNumber();
intgetThisPageLastElementNumber();
intgetNextPageNumber();
intgetPreviousPageNumber();
intgetPageSize();
intgetPageNumber();
}
importnet.sf.hibernate.*;
importnet.sf.hibernate.impl.SQLQueryImpl;
importnet.sf.hibernate.persister.EntityPersister;
importorg.apache.log4j.Logger;
importorg.jblooming.PlatformRuntimeException;
importorg.jblooming.persistence.hibernate.HibernateFactory;
importorg.jblooming.tracer.Tracer;
importorg.jblooming.waf.settings.ApplicationState;
importjava.util.HashSet;
importjava.util.List;
importjava.util.Set;
importjava.sql.SQLException;
importjava.sql.Connection;
importjava.sql.ResultSet;
/**
*@authorPietroPolsinellippolsinelli@open-lab.com
*/
publicclassHibernatePageimplementsPage{
protectedListelements;
protectedintpageSize;
protectedintpageNumber;
protectedinttotalElements=0;
publicstaticSetjdbcClassesSupportingScrollCursors=newHashSet();
privateScrollableResultsscrollableResults;
privateHibernatePage(intpageNumber,intpageSize){
this.pageNumber=pageNumber;
this.pageSize=pageSize;
}
publicbooleanisFirstPage(){
returngetPageNumber()==0;
}
publicbooleanisLastPage(){
returngetPageNumber()>=getLastPageNumber();
}
publicbooleanhasNextPage(){
returnelements.size()>getPageSize();
}
publicbooleanhasPreviousPage(){
returngetPageNumber()>0;
}
publicintgetLastPageNumber(){
doubletotalResults=
newInteger(getTotalNumberOfElements()).doubleValue();
returnnewDouble(Math.floor(totalResults/getPageSize())).intValue();
}
publicListgetThisPageElements(){
/*
*SinceweretrievedonemorethanthespecifiedpageSizewhenthe
*classwasconstructed,wenowtrimitdowntothepageSizeifanext
*pageexists.
*/
returnhasNextPage()?elements.subList(0,getPageSize()):elements;
}
publicLoggergetLogger(){
returnTracer.hibernateLogger;
}
publicintgetTotalNumberOfElements(){
returntotalElements;
}
publicintgetThisPageFirstElementNumber(){
returngetPageNumber()*getPageSize()+1;
}
publicintgetThisPageLastElementNumber(){
intfullPage=getThisPageFirstElementNumber()+getPageSize()-1;
returngetTotalNumberOfElements()<fullPage?
getTotalNumberOfElements():
fullPage;
}
publicintgetNextPageNumber(){
returngetPageNumber()+1;
}
publicintgetPreviousPageNumber(){
returngetPageNumber()-1;
}
publicintgetPageSize(){
returnpageSize;
}
publicintgetPageNumber(){
returnpageNumber;
}
publicstaticHibernatePagegetHibernatePageInstance(Queryquery,intpageNumber,intpageSize){
returngetHibernatePageInstance(query,pageNumber,pageSize,ApplicationState.platformConfiguration.driver_class);
}
publicstaticHibernatePagegetHibernatePageInstance(Queryquery,
intpageNumber,
intpageSize,
StringdriverClass) {
if(jdbcClassesSupportingScrollCursors.contains(driverClass))
returnHibernatePage.getScrollPageInstanceWithTotalByScroll(query,pageNumber,pageSize);
else
returnHibernatePage.getScrollPageInstanceWithTotalByList(query,pageNumber,pageSize);
}
/**
*ConstructanewHibernatePage.HibernatePagenumbersarezero-basedsothe
*firstpageispage0.
*
*@paramquery theHibernateQuery
*@parampageNumberthepagenumber(zero-based);
* ifInteger.MAX_VALUEwillreturnthelastpageforthequery
*@parampageSize thenumberofresultstodisplayonthepage
*/
protectedstaticHibernatePagegetScrollPageInstanceWithTotalByScroll(Queryquery,intpageNumber,intpageSize){
HibernatePagesp=newHibernatePage(pageNumber,pageSize);
try{
sp.scrollableResults=query.scroll(ScrollMode.SCROLL_SENSITIVE);
sp.scrollableResults.last();
sp.totalElements=sp.scrollableResults.getRowNumber();
sp.determineElements(query);
}catch(HibernateExceptione){
sp.getLogger().error("FailedtocreateScrollPagebygetScrollPageInstanceWithTotalByScroll:"+e.getMessage());
thrownewPlatformRuntimeException(e);
}
returnsp;
}
/**
*ConstructanewHibernatePage.HibernatePagenumbersarezero-basedsothe
*firstpageispage0.
*
*@paramquery theHibernateQuery
*@parampageNumberthepagenumber(zero-based);
* ifInteger.MAX_VALUEwillreturnthelastpageforthequery
*@parampageSize thenumberofresultstodisplayonthepage
*/
protectedstaticHibernatePagegetScrollPageInstanceWithTotalByList(Queryquery,intpageNumber,intpageSize){
HibernatePagesp=newHibernatePage(pageNumber,pageSize);
try{
sp.scrollableResults=query.scroll(ScrollMode.FORWARD_ONLY);
sp.totalElements=sp.calculateTotalElementsByList(query);
sp.determineElements(query);
}catch(HibernateExceptione){
sp.getLogger().error("FailedtocreateScrollPagebygetScrollPageInstanceWithTotalByQuery:"+e.getMessage());
thrownewPlatformRuntimeException(e);
}
returnsp;
}
privatevoiddetermineElements(Queryquery)throwsHibernateException{
if(Integer.MAX_VALUE==this.pageNumber)
this.pageNumber=(getTotalNumberOfElements()/this.pageSize);
/**
*todo:thisnomoremakessense,asweknowthetotalnumberofelements
*
*WesetthemaxresultstoonemorethanthespecfiedpageSizeto
*determineifanymoreresultsexist(i.e.ifthereisanextpage
*todisplay).TheresultsetistrimmeddowntojustthepageSize
*beforebeingdisplayedlater(ingetThisPageElements()).
*/
elements=query.setFirstResult(this.pageNumber*this.pageSize).setMaxResults(this.pageSize+1).list();
}
privateintcalculateTotalElementsByList(Queryquery)throwsHibernateException{
returnquery.list().size();
}
}
packageorg.jblooming.page;
importorg.apache.log4j.Logger;
importorg.jblooming.tracer.Tracer;
importjava.util.List;
/**
*@authorPietroPolsinellippolsinelli@open-lab.com
*/
publicclassListPageimplementsPage{
privateListelements;
privateintpageSize;
privateintpageNumber;
/**
*ConstructanewListPage.ListPagenumbersarezero-based,sothe
*firstpageispage0.
*
*@parampageNumberthepagenumber(zero-based);
* ifInteger.MAX_VALUEwillreturnthelastpageforthequery
*@parampageSize thenumberofresultstodisplayonthepage
*/
publicListPage(Listelements,intpageNumber,intpageSize){
this.elements=elements;
this.pageSize=pageSize;
this.pageNumber=pageNumber;
if(Integer.MAX_VALUE==this.pageNumber)
this.pageNumber=(getTotalNumberOfElements()/this.pageSize);
}
publicbooleanisFirstPage(){
returngetPageNumber()==0;
}
publicbooleanisLastPage(){
returngetPageNumber()>=getLastPageNumber();
}
publicbooleanhasNextPage(){
return((getPageNumber()+1)*getPageSize())<
(getTotalNumberOfElements()+1);
}
publicbooleanhasPreviousPage(){
returngetPageNumber()>0;
}
publicintgetLastPageNumber(){
/*
*WeusetheMath.floor()methodbecausepagenumbersarezero-based
*(i.e.thefirstpageispage0).
*/
doubletotalResults=newInteger(getTotalNumberOfElements()).doubleValue();
returnnewDouble(Math.floor(totalResults/getPageSize())).intValue();
}
publicListgetThisPageElements(){
/*
*SinceweretrievedonemorethanthespecifiedpageSizewhenthe
*classwasconstructed,wenowtrimitdowntothepageSizeifanext
*pageexists.
*/
finalintstart=getPageNumber()*getPageSize();
returnelements.subList(Math.min(start,getTotalNumberOfElements()+1),
Math.min(start+getPageSize(),getTotalNumberOfElements()+1));
}
publicLoggergetLogger(){
returnTracer.hibernateLogger;
}
/**
*thisis0-based,differentlyfromlist.size();
*/
publicintgetTotalNumberOfElements(){
returnelements.size()-1;
}
publicintgetThisPageFirstElementNumber(){
returngetPageNumber()*getPageSize()+1;
}
publicintgetThisPageLastElementNumber(){
intfullPage=getThisPageFirstElementNumber()+getPageSize()-1;
returngetTotalNumberOfElements()<fullPage?
getTotalNumberOfElements():
fullPage;
}
publicintgetNextPageNumber(){
returngetPageNumber()+1;
}
publicintgetPreviousPageNumber(){
returngetPageNumber()-1;
}
publicintgetPageSize(){
returnpageSize;
}
publicintgetPageNumber(){
returnpageNumber;
}
}
主要缺点就是:速度比较慢,没有C和C++快 |
|