Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*.jar
*.war
*.ear
*.class
hs_err_pid*
.idea
*.iml
Expand All @@ -18,4 +17,5 @@ workbench.xmi
*.swp
.settings
.checkstyle
GoogleMapsAPI.properties
twitter4j.properties
1 change: 1 addition & 0 deletions EkaterinaVishnevskaya/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twitter4j.properties
38 changes: 38 additions & 0 deletions EkaterinaVishnevskaya/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>EkaterinaVishnevskaya</artifactId>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>[4.0,)</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.fizteh.fivt.students.EkaterinaVishnevskaya.CollectionQl2;

import java.time.LocalDate;
import java.util.ArrayList;


public class CollectionsQL {

/**
*
* @param <T>
* @param collection
* @return
*/
/*public static<T> Selector from(AbstractCollection<T> collection)
{
return new Selector<>(collection);
}*/

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList students = new ArrayList<Student>();
students.add(new Student("ivanov", LocalDate.parse("1986-08-06"), "494"));
students.add(new Student("sidorov", LocalDate.parse("1999-08-06"), "495"));
students.add(new Student("john", LocalDate.parse("1987-08-06"), "494"));
Query<Student,Statistics> q = new Query<Student, Statistics>(students);
Iterable<Statistics> it = q.where(Conditions.rlike(Student::getName, ".*ov")).orderBy(Comparators.desc(Student::getGroup)).groupBy(Student::getGroup).select(Statistics.class, Student::getGroup).having(s -> s.getCount() > 0).execute();
System.out.println(it);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.fizteh.fivt.students.EkaterinaVishnevskaya.CollectionQl2;

import java.util.Comparator;
import java.util.function.Function;

/**
* Created by V on 19.12.2015.
*/
public class Comparators {
public static <T extends Comparable<T>> Comparator<Student> asc(Function<Student, T> func){
Comparator<Student> comparator = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return func.apply(o1).compareTo(func.apply(o2));
}
};
return comparator;
}
public static <T extends Comparable<T>> Comparator<Student> desc(Function<Student, T> func){
Comparator<Student> comparator = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return func.apply(o1).compareTo(func.apply(o2));
}
};
return comparator.reversed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.fizteh.fivt.students.EkaterinaVishnevskaya.CollectionQl2;

import java.util.function.Function;
import java.util.function.Predicate;

/**
* Created by V on 19.12.2015.
*/
public class Conditions {
public static <T> Predicate<T> rlike(Function<T, String> expression, String regexp) {
Predicate<T> p = s -> expression.apply(s).endsWith(regexp);
return p;
}
public static <T> Predicate<T> like(Function<T, String> expression, String pattern) {
Predicate<T> p1 = s -> expression.apply(s).startsWith(pattern);
Predicate<T> p2 = s -> expression.apply(s).endsWith(pattern);
Predicate<T> p3 = p1.and(p2);
return p3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package ru.fizteh.fivt.students.EkaterinaVishnevskaya.CollectionQl2;

import java.lang.reflect.Constructor;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Created by V on 19.12.2015.
*/
public class Query<T, ResultType> {
public Query(AbstractCollection<T> collection)
{
result = new ArrayList<>();
actionsForInput = new ArrayList<Runnable>();
actionsForOutput = new ArrayList<Runnable>();
selectAction = null;
sequence = collection.stream();
selectResult = null;
}

/*private Query(AbstractCollection<T> collection, ArrayList<Query<?> > other)
{

}*/

public Query<T,ResultType> where(Predicate<T> predicate)
{
actionsForInput.add(() -> {
whereImpl(predicate);
});
return this;
}

public Query<T, ResultType> select(Class<ResultType> resultType, Function<T, ?>... functions)
{
selectAction = () -> {
try {
selectImpl(resultType, functions);
} catch (InstantiationException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}
};
return this;
}


private void selectImpl(Class<ResultType> resultType, Function<T, ?>... functions) throws InstantiationException
{
//ArrayList<Object> selected = new ArrayList<>();
Iterator<T> it = sequence.iterator();
//ArrayList<ResultType> result = new ArrayList<>();
while(it.hasNext())
{
ArrayList<Object> selected = new ArrayList<>();
for(Function<T, ?> f : functions)
{
selected.add(f.apply(it.next()));
}
Constructor<?>[] constructors = resultType.getConstructors();
ResultType t = null;
for(int i = 0; i < constructors.length; ++i)
{
if( t!= null) break;
try {
t = (ResultType) constructors[i].newInstance(selected.toArray());
} catch (Exception ex)
{
}
}
if( t== null) throw new InstantiationException();
result.add(t);
}
resultTypeStream = result.stream();
selectResult = result;
}

public Query<T, ResultType> orderBy(Comparator<T>... comparators)
{
actionsForInput.add(() -> {
orderByImpl(comparators);
});
return this;
}

public Query<T, ResultType> groupBy(Function<T, ?>... functors)
{
actionsForInput.add(() -> groupByImpl(functors));
return this;
}

//�� ����� ���� ������ ���� Iterable<Statistics>
public<ResultType> Iterable<ResultType> execute()
{
for( Runnable r : actionsForInput)
{
r.run();
}
selectAction.run();
for(Runnable r1 : actionsForOutput){
r1.run();
}

return (Iterable<ResultType>) selectResult;
}

private void whereImpl(Predicate<T> predicate)
{
sequence = sequence.filter(predicate).collect(Collectors.toList()).stream();
}

private void orderByImpl(Comparator<T>... comparators)
{
for(Comparator<T> comparator : comparators)
{
sequence = sequence.sorted().collect(Collectors.toList()).stream();
}
}

private void groupByImpl(Function<T, ?>... functors)
{
for(Function<T, ?> f : functors)
{
ArrayList<T> filtered = new ArrayList<>();
Map<T, List<T>> grouped = (Map<T, List<T>>) sequence.collect(Collectors.groupingBy(f));
//apply aggreagate smth like:
//grouped.forEach(aggregate);
grouped.forEach((T key, List<T> value) -> filtered.add(value.get(0)));
sequence = filtered.stream();
}
}

public Query<T,ResultType> having(Predicate<ResultType> predicate){
actionsForOutput.add(()->{
havingImpl(predicate);
});
return this;
}

public void havingImpl(Predicate<ResultType> predicate){
resultTypeStream= resultTypeStream.
filter(s -> predicate.test(s))
.collect(Collectors.toList())
.stream();
}

public Query<T,ResultType> selectDistinct(){
actionsForOutput.add(()->{
selectDistinctImpl();
});
return this;
}

public void selectDistinctImpl(){
resultTypeStream = resultTypeStream
.distinct()
.collect(Collectors.toList())
.stream();
}

public Query<T, ResultType> limit(int n){
actionsForOutput.add(()->{
limitImpl(n);
});
return this;
}

public void limitImpl(int count){
if(resultTypeStream.count() > count){
resultTypeStream=resultTypeStream.limit(count);
}
}

private Stream<ResultType> resultTypeStream;
private ArrayList<ResultType> result;
private ArrayList<Runnable> actionsForInput;
private Runnable selectAction;
private ArrayList<Runnable> actionsForOutput;
private Stream<T> sequence;
private Object selectResult;
}

Loading