linq在java中的开源介绍.ppt
LINQ for Java,Integrating Query,Sets and Transform operations in J2EEOlmo del Corral,LINQ for Java,The LINQ Project is a set of extensions to the.NET Framework for language-integrated query,set,and transform operations.Extends C#width native language syntax and class libraries for queries.We will show a way to do it in Java without making any change in the Java language.,Query Example,This is the way a C#3 Query look like.It sorts,group and proyect the already defined collection:IEnumerable people;Seems a SQL Query,but is just syntax sugar.So you can do the same without using it,var query2=from p in people where p.Age 20 orderby p.Age descending,p.Name group new p.Name,Senior=p.Age 30,FirstJob=p.Jobs0 by p.CanCode;,Without Query Expressions,Without the cosmetics,it starts feeling standard Java code.In the 5th line,Name=is omitted.The compiler do it for you.But it makes confusing.It is worth?,var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new p.Name,Senior=p.Age 30,FirstJob=p.Jobs0);,Without Anonymous Fields in Object Inicializators,A step further in code normalization.But,what new means?Its a Anonymous Type.Just like a Java Anonymous Class but only with Fields,PropertiesThe correct way of doing its by building your own class.,var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new Name=p.Name,Senior=p.Age 30,FirstJob=p.Jobs0);,Without Anonymous Types,Now we have PersonResume class with a well-known syntax!Again,what kind of constructor have braces instead of()?Its a Object Inizializator,this expression calls the default constructor and then iterates setting every involver property.In Java we should create a specific constructor:,class PersonResume string _name;bool _senior;Job _firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new PersonResume Name=p.Name,Senior=p.Age 30,FirstJob=p.Jobs0);,Without Objects Inicializators,PersonResume class has a constructor with every field.Much more Java-like!You said people is a IEnumerable,so Are Where,OrderBy,GroupBy methods of IEnumerable?Not really.Those Are Extensional Methods,act as it those where in IEnumerable,but in fact they are out.Confusing,right?Sun people prefer simplicity.A static method is a better approach.,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new PersonResume(p.Name,p.Age 30,p.Jobs0);,Without Extensional Methods,Select,OrderBy,GroupBy should be static methods in Java.Right!The only missing thing is the=operator.Some kind of pointer?No,its the syntax for Lambda Expressions.A very sort definition for methods.These have an expression body,but a function should have the return statement to be clear!.,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(people,p=p.Age 20);var qorderage=Linq.OrderByDescending(qwhere,p=p.Age);var qthenbt=Linq.ThenBy(qorderage,p=p.Name);var groupy=Linq.GroupBy(qthenbt,p=p.CanCode,p=new PersonResume(p.Name,p.Age 30,p.Jobs0);,With Explicit Typed Lambda Expressions,Now,were certain of the parameters type in lambda expression?But the=operator stills there!.This is so obscure!.If a function return a value,why there is no return statement?Well maybe using return lambda expressions we will address this problem.,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(people,(Person p)=p.Age 20);var qorderage=Linq.OrderByDescending(qwhere,(Person p)=p.Age);var qthenbt=Linq.ThenBy(qorderage,(Person p)=p.Name);var groupy=Linq.GroupBy(qthenbt,(Person p)=p.CanCode,(Person p)=new PersonResume(p.Name,p.Age 30,p.Jobs0);,With Explicit Body Lambda Expressions,Lambda expressions look simpler with this syntax!So,What really these lambda expressions are?.Just a Anonymous Method!Why use another syntax if C#2.0 has one for it?,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(people,(Person p)=return p.Age 20;);var qorderage=Linq.OrderByDescending(qwhere,(Person p,Person q)=return p.Age.CompareTo(q.Age););var qthenbt=Linq.ThenBy(qorderage,(Person p,Person q)=return p.Name.CompareTo(q.Name););var groupy=Linq.GroupBy(qthenbt,(Person p)=return p.CanCode;,(Person p)=return new PersonResume(p.Name,p.Age 30,p.Jobs0););,Without any kind of Lambda Expressions,Look how you can do the same query in C#2.0Width anonymous methods you can write in-line functions.Its a bit faster,but a lot of people dont understand how they work.You can improve clarity by declaring explicit methods:,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(people,delegate(Person p)return p.Age 20;);var qorderage=Linq.OrderByDescending(qwhere,delegate(Person p,Person q)return p.Age.CompareTo(q.Age););var qthenbt=Linq.ThenBy(qorderage,delegate(Person p,Person q)return p.Name.CompareTo(q.Name););var groupy=Linq.GroupBy(qthenbt,(Person p)=return p.CanCode;,delegate(Person p)return new PersonResume(p.Name,p.Age 30,p.Jobs0););,Without anonymous methods,Explicit methods are easier than in-line functions!Just write the method name to make a delegate?Sound simple!Well,the correct way is to instantiate the delegate manually:,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(Person p)return p.Age 20;int CompareAge(Person p,Person q)return p.Age.CompareTo(q.Age);int CompareName(Person p,Person q)return p.Name.CompareTo(q.Name);bool CanCode(Person p)return p.CanCode;PersonResume DoResume(Persona p)return new PersonResume(p.Name,p.Age 30,p.Jobs0);,var qwhere=Linq.Where(people,WhereCondition);var qorderage=Linq.OrderByDescending(qwhere,CompareAge);var qthenbt=Linq.ThenBy(qorderage,CompareName);var groupy=Linq.GroupBy(qthenbt,CanCode,DoResume);,Without delegate inference,Explicit methods are easier than in-line functions!Func?Only Func?Mmm Its a generic delegate,but type information flow automatically,same as varMaybe do it explicit is better for clarity,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(Person p)return p.Age 20;int CompareAge(Person p,Person q)return p.Age.CompareTo(q.Age);int CompareName(Person p,Person q)return p.Name.CompareTo(q.Name);bool CanCode(Person p)return p.CanCode;PersonResume DoResume(Persona p)return new PersonResume(p.Name,p.Age 30,p.Jobs0);,var qwhere=Linq.Where(people,new Func(WhereCondition);var qorderage=Linq.OrderByDescending(qwhere,new Func(CompareAge);var qthenbt=Linq.ThenBy(qorderage,new Func(CompareName);var groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);,Without type inference,Now type information are explicit on delegates(Func)and weve removed implicitly typed local variables(var)Ought,there is a lot of code here!Its because generics!Remove it!Are you sure?C#2.0 generics are grate for clarity,performance and type safety!Yeah!Go ahead!,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(Person p)return p.Age 20;int CompareAge(Person p,Person q)return p.Age.CompareTo(q.Age);int CompareName(Person p,Person q)return p.Name.CompareTo(q.Name);bool CanCode(Person p)return p.CanCode;PersonResume DoResume(Persona p)return new PersonResume(p.Name,p.Age 30,p.Jobs0);,IEnumerable qwhere=Linq.Where(people,new Func(WhereCondition);SortedSequence qorderage=Linq.OrderByDescending(qwhere,new Func(CompareAge);SortedSequence qthenbt=Linq.ThenBy(qorderage,new Func(CompareName);IEnumerable groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);,Without generics,Weve to renounce to C#2.0 generics to use LINQ on Java.(what Java 1.5 has cant be called generics)Well,were now in C#1.0.Again,you can do the same query!.Its time for the final jump to Java!.We will start with delegates,why use delegates instead of interfaces?,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(object p)return(Person)p).Age 20;int CompareAge(object p,object q)return(Person)p).Age.CompareTo(Person)q).Age);int CompareName(object p,object q)return(Person)p).Name.CompareTo(Person)q).Name);object CanCode(object p)return(Person)p).CanCode;object DoResume(object o)Person p=(Person)p;return new PersonResume(p.Name,p.Age 30,p.Jobs0);,IEnumerable qwhere=Linq.Where(people,new WhereFunc(WhereCondition);SortedSequence qorderage=Linq.OrderByDescending(qwhere,new Comparer(CompareAge);SortedSequence qthenbt=Linq.ThenBy(qorderage,new Comparer(CompareName);IEnumerable groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);,Without delegates,Interfaces are more object-oriented than delegates!Our work is almost complete!To target Java we have to remove Properties and Indexers,witch are less object oriented and dont really exist!,class WhereCondition:IWhere bool Where(Person p)return p.Age 20;class CompareByAge:IComparer int Compare(object p,object q)return(Person)p).Age.CompareTo(Person)q).Age);class CompareByName:IComparer int Compare(object p,object q)return(Person)p).Name.CompareTo(Person)q).Name);class CodeGetter:IGetSomething object GetSomething(object p)return(Person)p).CanCode;class ResumeGetter:IGetSomething object GetSomething(object o)Person p=(Person)p;return new PersonResume(p.Name,p.Age 30,p.Jobs0);,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;,IEnumerable qwhere=Linq.Where(people,new WhereCondition();SortedSequence qorderage=Linq.OrderByDescending(qwhere,new CompareByAge();SortedSequence qthenbt=Linq.ThenBy(qorderage,new CompareByName();IEnumerable groupy=Linq.GroupBy(qthenbt,new CodeGetter(),new ResumeGetter();,Without properties and indexers,Much simplier!Just fields and methods!Only one step more!A good object oriented design should use one file for each class!,class WhereCondition:IWhere bool Where(Person p)return p.getAge()20;class CompareByAge:IComparer int Compare(object p,object q)return(Person)p).getAge().CompareTo(Person)q).getAge();class CompareByName:IComparer int Compare(object p,object q)return(Person)p).getName().CompareTo(Person)q).getName();class CodeGetter:IGetSomething object GetSomething(object p)return(Person)p).getCanCode();class ResumeGetter:IGetSomething object GetSomething(object o)Person p=(Person)p;return new PersonResume(p.getName(),p.getAge()30,p.getJobs().elementAt(0);,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResume(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string getName()return _name;void setName(string value)_name=value;bool getSenior()return _senior;void setSenior(bool senior)_senior=value;Job getFirstJob()return _firstJob;void setFirstJob()_firstJob=value;,IEnumerable qwhere=Linq.Where(people,new WhereCondition();SortedSequence qorderage=Linq.OrderByDescending(qwhere,new CompareByAge();SortedSequence qthenbt=Linq.ThenBy(qorderage,new CompareByName();IEnumerable groupy=Linq.GroupBy(qthenbt,new CodeGetter(),new ResumeGetter();,One class,One file,The Wonderful world of Java!,package stuff;public class WhereCondition implements IWhere bool Where(Person p)return p.getAge()20;,package stuff;public class PersonResume string _name;bool _senior;Job _firstJob;public PersonResume(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string getName()return _name;void setName(string value)_name=value;bool getSenior()return _senior;void setSenior(bool senior)_senior=value;Job getFirstJob()return _firstJob;void setFirstJob()_firstJob=value;,IEnumerable qwhere=Linq.Where(people,new WhereCondition();SortedSequence qorderage=Linq.OrderByDescending(qwhere,new CompareByAge();SortedSequence qthenbt=Linq.ThenBy(qorderage,new CompareByName();IEnumerable groupy=Linq.GroupBy(qthenbt,new CodeGetter(),new ResumeGetter();,package stuff;public class CompareByAge implements IComparer int Compare(object p,object q)return(Person)p).getAge().CompareTo(Person)q).getAge();,package stuff;public class ResumeGetter implements IGetSomething object GetSomething(object o)Person p=(Person)p;return new PersonResume(p.getName(),p.getAge()30,p.getJobs().elementAt(0);,package stuff;public class CompareByName impleme